I have a query regarding the sizes of Graphics. I’m trying to code collision detection, my code is as follows:
const collided = (objectBounds, bounds) => {
return (
objectBounds.x + objectBounds.width > bounds.x &&
bounds.x + bounds.width > objectBounds.x &&
objectBounds.y + objectBounds.height > bounds.y &&
bounds.y + bounds.height > objectBounds.y
);
};
const restrictPosToObjectBounds = (objectBounds, bounds, oldBounds) => {
if (collided(objectBounds, bounds)) return oldPos;
else
return {
x: bounds.x,
y: bounds.y
};
};
And then I’ll call it like this:
const objectPosition = object.getLocalBounds()
const oldPosition = object.getLocalBounds()
restrictPosToObjectBounds (
objectPosition,
{
...getNewPos(oldPosition),
width:oldPosition.width,
height:oldPosition.height,
},
oldPosition
)
Which kinda works, but it there would be a big gap around the object I’m colliding with, leaving me unable to properly collide with it
If I change my collide function to this (dividing the width and heights by half)
const collided = (objectBounds, bounds) => {
return (
objectBounds.x + objectBounds.width / 2 > bounds.x &&
bounds.x + bounds.width / 2 > objectBounds.x &&
objectBounds.y + objectBounds.height / 2 > bounds.y &&
bounds.y + bounds.height / 2 > objectBounds.y
);
};
Then it works properly, any idea why? Is the scale of a Graphic rendered differently or something, how do it get its accurate width and height?