Skip to main content
gamedev.net
🔒 Locked

Collision Questions

Started by Fredrik Hellman Jan 3, 2001 at 12:46 PM 3 replies 350+ views
Original Post
Fredrik Hellman
Fredrik Hellman
Hello! I read an article in this forum some minutes ago about 2D-collisions (from July 2000). It described how to detect if two objects were overlapping each other. But still there is one thing that is unclear for me: If I have detected that two objects have collided, and I want the two objects to bounce from each other, in two different directions. How can I check from what direction the two objects were coming from? I have an idea, but I don''t know if it is good: * I make a history list of the latest moves of the object. From that I can calculate a direction, in some way . Would this work, or is there a better way to do it? Fredrik Hellman mrhellman@telia.com
Fredrik Hellmanmrhellman@telia.com
Fredrik Hellman
Fredrik Hellman
I understand. Thank you very much!

If you have any source codes or links to source codes about this type of collisions, please reply.

Fredrik Hellman
mrhellman@telia.com
Fredrik Hellmanmrhellman@telia.com
evaclear
evaclear
Well here is some pseudo code for the collision.

float sprite_Xvelocity;
float sprite_Yvelocity;
float sprite_Xpos;
float sprite_Ypos;

while(true)
{

if(collision occurs on the X axis)
sprite_Xvelocity = -sprite_Xvelocity;

if(collision occurs on the Y axis)
sprite_Yvelocity = -sprite_Yvelocity;

sprite_Xpos = sprite_Xpos + sprite_Xvelocity;
sprite_Ypos = sprite_Ypos + sprite_Yvelocity;

}

That will get you a basic reverse collision going. You can get more indepth with it if you calculate the angle the sprite hits at and such but in a side scroller it is often not nessassary.

Joseph FernaldSoftware EngineerRed Storm Entertainment.------------------------The opinions expressed are that of the person postingand not that of Red Storm Entertainment.
Fredrik Hellman
Fredrik Hellman
Thank you!

Fredrik Hellman
mrhellman@telia.com
Fredrik Hellmanmrhellman@telia.com

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.