bump icon indicating copy to clipboard operation
bump copied to clipboard

rectangleCollision (and possibly others) seem to break with constant acceleration mechanic

Open jbockmon opened this issue 6 years ago • 6 comments

I have a platfomer with a constant acceleration built in to simulate gravity:

let playerGravity = 0.2; playerSprite.vy += playerGravity;

collision detection for playerSprite and array of sprites that are allowed to collide with the playerSprite:

if( b.hitTestRectangle(playerSprite, collSprites[i])){ b.rectangleCollision(playerSprite, collSprites[i], false);

where:

b = new Bump(PIXI);

on "impact" where playerSprite lands on top of a tile with which it can collide first aligns the playerSprite to the tile, then after ~1 second @ 60fps, the playerSprite disappears. Movement is also hindered after collision to whole tiles before playerSprite disappears.

jbockmon avatar Jul 23 '17 23:07 jbockmon

Update: The disappearing issue appears to be a related to collisions with multiple rectangles at the same time.

For testing, I made only a single block collideable, and the r1 sprite no longer disappears.

However, even with a single tile only colliding, the r1 sprite is moved left until they are dumped off the tile.

jbockmon avatar Jul 24 '17 00:07 jbockmon

I'm new to Hexi, so this may be a dumb question - but why are you calling hitTestRectangle first? Surely this will only prevent things overlapping if they are already colliding. What if the player is not colliding with the tiles already, and moves fast enough to pass through?

JamesCraster avatar Sep 06 '17 09:09 JamesCraster

@jbockmon Have to seen the platformer example here?

https://github.com/kittykatattack/hexi/blob/master/examples/src/platforms.js

kittykatattack avatar Sep 06 '17 14:09 kittykatattack

@JamesCraster: you're correct, in this case there's no need to call hitTestRectangle - a simple rectangleCollision will do.

kittykatattack avatar Sep 06 '17 16:09 kittykatattack

@JamesCraster the algorithms for detecting collisions in hitTestRectangle and rectangleCollision are different. Furthermore. hitTestRectangle does not alter sprite vx or vy directly, nor does it alter its x or y coords. rectangleCollision, on the other hand, checks for collisions as well as handles sprite movement/correction to prevent collision.

When I relied on rectangleCollision alone, the algorithm it employs returns left/right collisions as well as bottom/top collisions when the lhs sprite spans two or more blocks/sprites. Because rectangleCollision directly modifies the vx and vy values, these left/right collisions would stop my lhs sprite from being able to move. I use hitTestRectangle to filter the unncessary left/right collisions out to prevent it from setting lhs.vx to 0.

jbockmon avatar Sep 06 '17 16:09 jbockmon

@kittykatattack I have not. I will have to check it out. Thanks!

jbockmon avatar Sep 06 '17 17:09 jbockmon