# SDL side collision

This topic is 3047 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi i have only just started writing a game for the first time and have wrote a simple physics engine that lets a ball bounce off a floor or any given object. It uses the SDL_Collide lib for detection and basically what i want to do is also detect weather its colliding with the top of a surface or hitting the side of it(a wall) and if its a wall then to halt the acceleration so it can not go threw it. I had the idea of making rectangles around the sides of the level but i have no idea how to do this automatically if even possible so it would be much to complex to build these surfaces while building the level... if theres a simpler way i'd like to know how. (note: i'm using C so those gigantic classes are out of the question ;)

##### Share on other sites
I don't know SDL so I can't help you there. The simplist is bouncing off an axis aligned box, i.e. the sides of the monitor. Your ball moves DX and DY per unit of time or per frame. Say that's 10 and 10 and opposing corners are (0,0) and (800,600). Say you're at (795,400). So you update and the new position is (805,410). That's outside the boundaries so you need to bounce off the wall and correct the position. The correct X is 800-(805-800)=795. You also need to correct the velocity. Since it's the X that overshot the boundary it's DX=-DX.

That's a special, very simplified case. More generally you're reflecting a vector across a vector. Above you're reflecting (10,10) across (-1,0) to get (-10,10). (-1,0) is a normal (n), i.e. perpendicular, to the surface. (10,10) is your velocity vector (v). The formula is v-2*((v.a)/(a.a))*a = (10,10)-2*(-10)*(-1,0) = (10,10)-(20,0) = (-10,10). That's your new velocity vector. You have to apply the old velocity vector up to the point of impact then the new one from there forward. With that example it was follow the old velocity vector for the first half of the time and the new one for the second half.

• 16
• 11
• 10
• 9
• 49
• ### Forum Statistics

• Total Topics
631394
• Total Posts
2999748
×