Jump to content
  • Advertisement
Sign in to follow this  
philipptr

Collision and pushing things away

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

(Im making a 2d Sidescroller (SDL+Opengl)) At the moment i use some quite easy piece of code to check whether 2 rectangles are stuck into each other or not (if (x1>x2 && x1<x2width and so on...) It works okay and since I dont want to use the stencil buffer I dont know how to make it more precise while keeping the speed quite good. So my actual problem is calculating the new position of the objects. So I want that the players ship pushes away the enemy. Now I have a piece of code which checks whether the difference of the X values of the mid points of the objects is bigger or the difference of the Y values. If the X difference is bigger it pushes horizontaly. If the Y difference is bigger it pushes vertically. I thought this would work quite well but it doesnt. the enemys bounce over and under the players ship but only sometimes do what they should. So has someone an idea for a better solution?

Share this post


Link to post
Share on other sites
Advertisement
Do your problems occur when objects are moving too quickly? If so, you'll want to look up swept collision tests. I don't have a link handy but I believe there's an article on this somewhere in GameDev (or google it).

Share this post


Link to post
Share on other sites
In addition to bitmap's suggestion about swept tests, I'll also add that circles are a little easier to work with than rectangles. So you might consider as an option switching to circles for your collision detection and response.

Share this post


Link to post
Share on other sites
im sure its not because the objects are too fast. actually its more a problem that they easyer bounce over or under the ship than being pushed horizontally if fly against an object from the left/right (i guess that comes through using rectangles instead of squares)

will check circles out. thought that would be much more coomplex than rectangles O_o

Share this post


Link to post
Share on other sites
Circles are indeed much simpler. You calculate the distance between two points, and check to see if that distance is below a certain distance - if it is, then the circles have collided.
Of course, calculating the distance is simple - just use Pythagoras!
Assuming you are player_x and player_y and the object is at object_x and object_y, you can just do:
dist_between_points = sqr((object_x-player_x)2+(object_y-player_y)2)
if (dist_between_points < min_distance) then
// We're touching!
else
// We're not touching.
end if
Now, square-roots are quite expensive operations, so it's better if you remove them and compare against the min_distance2 instead (which can be precalculated for even more speed!)

To move them away from eachother is easy again, when using circles. Take the offset in X and offset in Y between the objects as a vector and normalise it so that it has a length of 1. (You can do this by using Pythagoras again to calculate the total length of the vector then dividing the X and Y components by the length). Then multiply the X and Y components by your min_distance again to push it away, add it to your player_x and player_y and set the object_x and object_y to these new values.

Hope this helps, and is clear enough!

Share this post


Link to post
Share on other sites
thanks very much. i hadnt tgot that idea with the distance so yeah it was very clear and helpfull :)
also the help with the pushing was very nice :) thanks again.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!