iphone - Is there a way to stop an object from passing through another object, but only if it approaches from one side? -
i making pong style game , trying ball bounce off of 1 side of paddle , bounce different direction off of side of paddle. have:
if (cgrectintersectsrect(cgrectinset(_ball.frame, -1, -1), _paddle1.frame) || cgrectintersectsrect(cgrectinset(_ball.frame, -1, -1), _paddle2.frame)) { ballvelocity.y *= -1; }
where paddle1
bottom paddle , paddle2
top paddle. right now, ball bounce off top , bottom of paddle stuck inside paddle if enters either paddle side or @ major angle. want bounce off side , change direction on x-axis.
rather trying fix "stuck ball" problem changing velocity, move ball outside of paddle when has intersected in addition changing velocity. this:
cgrect frame = _ball.frame; // change reflect top , bottom bool intersectstoppaddle = cgrectintersectsrect(cgrectinset(_ball.frame, -1, -1), _paddle1.frame); bool intersectsbottompaddle = cgrectintersectsrect(cgrectinset(_ball.frame, -1, -1), _paddle2.frame); if (intersectstoppaddle || intersectsbottompaddle) { if (intersectstoppaddle) // move ball down frame.origin.y += (_paddle1.frame.origin.y - _ball.frame.origin.y); else // bottom paddle, move frame.origin.y -= (_paddle2.frame.origin.y - _ball.frame..origin.y); ballvelocity.y *= -1; _ball.frame = frame; }
Comments
Post a Comment