NSIntersectsRect detection, buggy..

fakeOne
Unregistered
 
Post: #1
Hi.

I'm making a variation of pong in objc/cocoa and I need to make it so I can detect if the ballRect and the paddleRect intersect. This is something similiar to what I have so far..:

Code:
if (NSIntersectsRect(paddleRect, ballRect)) {
              // bounce off!
        dx = -dx; // dx, being the ballRect.origin.x
}

This works, but it's a bit buggy. The ball sometimes gets glitched to the paddle for a second. I also googled and found someone else having a similiar problem as me ( http://lists.apple.com/archives/Cocoa-de...01525.html ).

And I also tried doing dy = -dy (being ballRect.origin.y) too, but it just turned out to be a bit more buggy.

So my question is, would there be a better way of detecting if the rects intersect? Or some better way of doing this, so the ball doesn't glitch?
Quote this message in a reply
Sage
Posts: 1,403
Joined: 2005.07
Post: #2
If you dont mind adding energy into the system, you should move the ball so its not intersecting the rectangles for example, If it comes in and collides on the left you push it outward in the right direction until no part of it touches the paddle.
A better solution, but definitly pointless to implement for pong would be subdivide time until you narrow down the time and point of collision to some specified precision, although it can be fun to implement so I mention it Smile

Sir, e^iπ + 1 = 0, hence God exists; reply!
Quote this message in a reply
Member
Posts: 469
Joined: 2002.10
Post: #3
The problem is how you update your locations upon collisions. When you collide, you need to back up to a point where the ball is not colliding before applying reflections and rotations to the ball's movement vector. You can do this by interpolating back to a fractional movement step or you can just jump back a whole step. Either way, it'll keep the ball from getting "stuck" to the the paddle.

---Kelvin--
15.4" MacBook Pro revA
1.83GHz/2GB/250GB
Quote this message in a reply
fakeOne
Unregistered
 
Post: #4
kelvin Wrote:The problem is how you update your locations upon collisions. When you collide, you need to back up to a point where the ball is not colliding before applying reflections and rotations to the ball's movement vector. You can do this by interpolating back to a fractional movement step or you can just jump back a whole step. Either way, it'll keep the ball from getting "stuck" to the the paddle.

Thanks! I'd have never thought of doing that. Now, I got the paddles to work without them getting "stuck" Smile.
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Unity trial is buggy stevejohnson 3 2,467 Nov 18, 2006 11:47 AM
Last Post: bronxbomber92