#### Archived

This topic is now archived and is closed to further replies.

# Simple collision detection in 2D game

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

## Recommended Posts

I have a simple 2D game where the camera is above and you can walk around and rotate player character. There are quads around the screen. The problem is, how to make proper collision detection. I have thought something like this, but I don''t know how to implement it. How to check when player is inside those triangles. http://yartz.d2g.com/collision_detection.jpg This might not be the best method, but if you just can help me with it. Or maybe explain better methods.

##### Share on other sites
If you didn''t understand... I have to know in what direction the player is coming to the wall. So I can stop movement nicely in y- or x-axis. I can tell when the player tries to go inside the wall and stop his movement, but then he''s stuck in the wall until he changes direction away from wall. I need to stop movement only in y- or x-axis. It makes him "slide" nicely along the wall, just like in Quake or anything else..

I hope you can help me with this. This shouldn''t be very hard issue, but it is for me :O

##### Share on other sites
Here''s an idea. You know the direction the character is trying to move . That direction is a vector. You also know the angle of the wall. Turn the wall with which the character collided into a vector. So, if the wall is formed between points A and B, make a vector like this:

wall_vector = MakeUnitVector(B - A);

This vector does need to have length 1, which is why I made it a unit vector.

Also, lets say the direction the character is walking is called Walk_dir;

Walk_dir = direction vector of character walking;

To make the character slide along the wall, just project Walk_dir into wall_vector by taking the dot product. So, the final walk direction, which will be parallel to the wall, is:

New_walk_dir = wall_vector * DotProduct(wall_vector, Walk_dir);

That should solve your problem! As for checking to see when the player is inside a triangle, I''m unable to view your page for some reason, but the following page describes a variety of point-in-polygon techniques:

www.acm.org/pubs/tog/editors/erich/ptinpoly/

Graham Rhodes
Senior Scientist
Applied Research Associates, Inc.

##### Share on other sites
I''m not very good at math. I didn''t understand anything

Maybe this link works better, http://yartz.daug.net/collision_detection.jpg

##### Share on other sites
quote:
Original post by YartZ
I''m not very good at math. I didn''t understand anything

Heh heh! Well I basically gave you the answer, . You do have to understand some basics of vectors (what they basically are and how to do dot products) and grasp how to represent the edges of a room (triangle) as vectors.

You can learn about vectors and dot products in this gamedev article:

www.gamedev.net/reference/programming/features/vecmatprimer/

And you can learn some things about representing rooms/walls as vectors in Jeff Lander''s excellent article available here (this requires a free registration with gamasutra):

www.gamasutra.com/features/20000210/lander_01.htm

quote:
Original post by YartZ http://yartz.daug.net/collision_detection.jpg

Yes, this works, thank you . I understood what you were asking anyway.

I hope these articles help. I''m afraid you''re own your own to learn the basics of vectors and dot products, .

Graham Rhodes
Senior Scientist
Applied Research Associates, Inc.

1. 1
Rutin
26
2. 2
3. 3
JoeJ
20
4. 4
5. 5

• 10
• 9
• 9
• 46
• 41
• ### Forum Statistics

• Total Topics
631751
• Total Posts
3002072
×