math - 2D Continuous Collision Detection -
i'm trying implement simple continuous collision detection pong game i'm not sure i'm implementing or understand right. afair continuous collision detection used fast moving objects may pass through object circumventing normal collision detection.
so tried because fast moving object have ball need position of ball, move speed, , position of object comparing to.
from figured best example if ball's move speed indicated moving left, compare it's left-most bound right-most bound of other object. step through adding move speed left-most bound of ball , compare make sure it's greater other objects right bound. show there no left right collision.
i have working, unfortunately, ball starts bouncing while acts if hits paddle when nothing there.
i'm bit lost, appreciated!
static bool checkcontinuouscollision(pactor ball, prect ballrect, pactor other, prect otherrect) { pvector ballmovespeed; int ballxlimit; int ballylimit; ballmovespeed = ball.movespeed; // moving left if ( sgn(ball.movespeed.x) < 0 ) { ballxlimit = std.math.abs(ballmovespeed.x) / 2; ( int = 0; <= ballxlimit; i++ ) { if ( ballrect.left < otherrect.right && otherrect.left < ballrect.left) { return true; } ballrect.left -= i; } } //we moving right if ( sgn(ball.movespeed.x) > 0) { ballxlimit = std.math.abs(ballmovespeed.x) / 2; ( int = 0; < ballxlimit; ++ ) { if ( ballrect.right > otherrect.left && ballrect.right < otherrect.right ) { return true; } ballrect.right += i; } } // not moving if ( sgn(ball.movespeed.x) == 0) { return false; } }
you seem checking collision of 1 dimension, i.e x dimension of ball versus other.
what want compare whether 2 objects collide in 2d space. can done adjusting each objects bounding rectangle , checking whether rectangles overlap. in loop can adjust ball rectangle accordingly
Comments
Post a Comment