[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y ] [Search | Free Show | Home]

Why wont this shitty Javascript collision detection system work?

This is a blue board which means that it's for everybody (Safe For Work content only). If you see any adult content, please report it.

Thread replies: 17
Thread images: 1

File: 1461849911612.png (52KB, 657x527px) Image search: [Google]
1461849911612.png
52KB, 657x527px
Why wont this shitty Javascript collision detection system work?

function colCheck(shapeA, shapeB) {

// get the vectors to check against
var vX = (shapeA.x + (shapeA.width / 2)) - (shapeB.x + (shapeB.width / 2)),
vY = (shapeA.y + (shapeA.height / 2)) - (shapeB.y + (shapeB.height / 2)),
// add the half widths and half heights of the objects
hWidths = (shapeA.width / 2) + (shapeB.width / 2),
hHeights = (shapeA.height / 2) + (shapeB.height / 2),
colDir = null;

// if the x and y vector are less than the half width or half height, they we must be inside the object, causing a collision
if (Math.abs(vX) < hWidths && Math.abs(vY) < hHeights) { // figures out on which side we are colliding (top, bottom, left, or right)
//var oX = hWidths - Math.abs(vX), oY = hHeights - Math.abs(vY); if (oX >= oY) {
if (vY > 0) {
colDir = "t";
shapeA.y += oY;
} else {
colDir = "b";
shapeA.y -= oY;
}
} else {
if (vX > 0) {
colDir = "l";
shapeA.x += oX;
} else {
colDir = "r";
shapeA.x -= oX;
}
}
}
return colDir;
}



Chrome dev tools says it's an illegal return statement
>>
>>56272368
post your shapea and shape b too
>>
>>56272368
>repeating arithmetic operations instead of storing the result in a variable
>variables instead of consts
>longass lines with comments behind instead of above
>code in comments
>writing comments of no value like "add half the height"
>returning results as strings instead of something like public consts or other enum-likes
how was le code bootcamp? :)))
>>
>>56272395
 player = {
x : width/2,
y : height - 100,
width : 5,
height : 20,
speed : 3,
velX : 0,
velY : 0,
jumping : false,
grounded : false
};

box = {
x: 0,
y: 0,
width: 10,
height: 100
};
>>
>>56272408
wait the box width is 10 and your player width is 5 so if you have 2 players you are more than likely going to collide or overflow outside the box?
>>
>>56272436
if your x is 2.5 for each player you will be in the box if its less than that you will overflow out if its more you will collide
>>
>>56272446
and please use
https://developer.mozilla.org/en-US/docs/Web/API/Canvas_API/Tutorial/Drawing_shapes

and not your own object
>>
>>56272458
also please use javascript object prototype and create a .getx() and a .gety() to get x and y co-ordinates otherwise anyone can change the x and y variables when instancing or when ever
>>
>>56272488
wait so how do you instance your shape? if x is a function, how do you move the shape across the x and y axis?
>>
Pajeet-tier code quality.
>>
>>56272654
How would you improve it?
>>
>>56272654
>>56272407
>>56272458
OP here: im just following this JS tutorial to get a quick grasp of how games are developed with JS.

http://www.somethinghitme.com/2013/01/09/creating-a-canvas-platformer-tutorial-part-one/

Please link me a better tutorial if you know of any
>>
>>56273362
just read a fkn book
>>
>>56273362
Everyone on reddit said it's a good tutorial but 4chan thinks it's bad
>>
>>56273848
>reddit

It is a decent tutorial for beginners but nothing that is good comes to you easy. Only hard work or talent will get you good results.
>>
>>56274375
Thanks for nothing
>>
>>56274392
https://shodan.me/books/

Have fun plebbit monkey.
Thread posts: 17
Thread images: 1


[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y] [Search | Top | Home]

I'm aware that Imgur.com will stop allowing adult images since 15th of May. I'm taking actions to backup as much data as possible.
Read more on this topic here - https://archived.moe/talk/thread/1694/


If you need a post removed click on it's [Report] button and follow the instruction.
DMCA Content Takedown via dmca.com
All images are hosted on imgur.com.
If you like this website please support us by donating with Bitcoins at 16mKtbZiwW52BLkibtCr8jUg2KVUMTxVQ5
All trademarks and copyrights on this page are owned by their respective parties.
Images uploaded are the responsibility of the Poster. Comments are owned by the Poster.
This is a 4chan archive - all of the content originated from that site.
This means that RandomArchive shows their content, archived.
If you need information for a Poster - contact them.