Jump to content
  • Advertisement
Sign in to follow this  
phil67rpg

collsion detection

This topic is 2627 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

well I fianlly found some collision detection code that I want to use.however it appears to only work with circles and I want to use two rectangles.I just want to tweak my code.any help would be appreciated.here is my code.
[font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"]

bool[/font][/font][/font][font="Consolas"][font="Consolas"] detectcollision([/font][/font][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"]int[/font][/font][/font][font="Consolas"][font="Consolas"] x1, [/font][/font][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"]int[/font][/font][/font][font="Consolas"][font="Consolas"] y1, [/font][/font][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"]int[/font][/font][/font][font="Consolas"][font="Consolas"] radis1, [/font][/font][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"]int[/font][/font][/font][font="Consolas"][font="Consolas"] x2, [/font][/font][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"]int[/font][/font][/font][font="Consolas"][font="Consolas"] y2, [/font][/font][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"]int[/font][/font][/font][font="Consolas"][font="Consolas"] radis2)

{

[/font][/font][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"]float[/font][/font][/font][font="Consolas"][font="Consolas"] xd, yd, distance;



xd = x2-x1;

yd = y2-y1;

distance = sqrt(xd*xd + yd*yd);



[/font][/font][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"]if[/font][/font][/font][font="Consolas"][font="Consolas"]( radis1 + radis2 >= distance)

[/font][/font][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"]return[/font][/font][/font][font="Consolas"][font="Consolas"] [/font][/font][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"]true[/font][/font][/font][font="Consolas"][font="Consolas"]; [/font][/font][font="Consolas"][color="#008000"][font="Consolas"][color="#008000"][font="Consolas"][color="#008000"]//collision

[/font][/font][/font][font="Consolas"][font="Consolas"]

[/font][/font][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"]return[/font][/font][/font][font="Consolas"][font="Consolas"] [/font][/font][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"]false[/font][/font][/font][font="Consolas"][font="Consolas"]; [/font][/font][font="Consolas"][color="#008000"][font="Consolas"][color="#008000"][font="Consolas"][color="#008000"]//no collision

[/font][/font][/font][font="Consolas"][font="Consolas"]};

thanks for all your help.

[/font][/font]

Share this post


Link to post
Share on other sites
Advertisement
If you want to use AABBs (Axis Aligned Bounding Box), there's a thread here that talks a bit about it (there's some useful links in that thread as well). If you want to use OBBs (Oriented Bounding Box), I would suggest using the Separating Axis Theorem. Note that the method used in the other thread that I linked to about AABBs is actually just using the SAT, though it's not explicitly stated, and because they're AABBs they're are some simplifications that can be applied to the theorem.

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!