Jump to content
  • Advertisement
Sign in to follow this  
Thrust

First time 2d colision detection... Where to start?

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

After reading the articles here, using google, and even from help previously on this board I am still at a loss here. Let me explain my project. I am making a 2d side scroller, using SDL and C++. My map is 25x20 in size and my tiles are 32x32 pixels. My sprite is 29x40 pixels. Ok that explains what im doing. Now what I do not understand how to do is to check for colision with another player (which I havent added yet) and with the tiles that I chose to stop when I come in contact with. I havent added jumping yet, you can freely move around the map 2 pixels at a time. I think if you take your x and y position and divide them by 32 you get the tile your on. I dont have my map global, so that I can load it externally from another file (right now its just an array in my drawMap() function). Thats all I understand. I want to use the binding box approach but there is no good documentation I can find. I heard of a library from www.libsdl.org called bitmask 1.1 but the documentation on that is ,go figure, horriable. I am not good at learning new things inless they are explained out well. It took me a few months (not straight out, alot of stoping and trying other things like network code and such) just to get a working tilemap. What I want to know is if there is any good place to start learning this for a complete newbie in colision detection? Thank you for your help, and sorry if I bug or annoy any of you, I just learn best this way (AKA asking on the message boards).

Share this post


Link to post
Share on other sites
Advertisement
You might get some help from this article on simple platform games with collision detection. It comes with code:

http://jnrdev.kbs-design.net/

I personally didn't like the way it implemented the collision system (very simplistic), but it's good for ideas and good for beginners who have simple projects anyway.

Share this post


Link to post
Share on other sites
Collision detection, how to start...

You can basically have a second array the same size as your map (this only works because the map is small, it doesn't scale to well.)

When you move something, you go, and for every square that used to be taken up by the object but isn't: You Decrement the array index for that square. For every square that it is moving to, you increment the array index for that square.

Now to check collisions, you check array[index] & 0x7F (assuming bytes). if its greater then 0, you've got a collision. When that happens, back up the object you were moving, one step back. Before doing some collision handling, (ok, it was traveling 10m/s into a wall which absorbes 6% of the energy, he should be moving backwards at xm/s. You should also keep which objects are where, on tabs. (for eg. you can get two arrays, and keep pointers in one, and the rest of the coldet stuff in the other.)

From,
Nice coder



Share this post


Link to post
Share on other sites
Hey,
had same Problem a few weeks ago in 3D....

I hope you somewhere learnt in school in some math lesson something about geometry! If you want to do some 2D colision detection, you should have leanrt something about 2D geometry, in case 3D col detection -> 3D geometry.
The way you're doing it in 2D should be the same as in 3D:
Put some rectangles/boxes around your "object" and check whether they collide. One way to check whether they collide or not is to check if on edge of the rectangle/box crosses in it's way a line(2D)/plane(3D) or is inside of an other rectangle/box. In 2D it is quite simple:

one posibility:

if you only use rectangled boxes you can project the boxes down to the x-achses. Which means simple take the x-coords. You only got to check whether one of the x-coords of one object is betwen the x-coords of the other box. Do the same on the y-achses and if the same edge is in both projection between the two coords of the other box it should be in it.

You could also check the angle which the edge has to one of the sides and see whether it is < 90degree and whether the edge is closer to the other edge that it is inside the box(depending on the angle between the sides->sin/cos/tan check how long it is suposed to be to build and rectangled triangle with another of the edges).......

If you want to use all kind of boxes you could build a ray for each edge where on the ray the original pos of the edge is and the point it is going to move. check whether that ray colides whith a side of the box(which is in 2D a ray to).

edge posx = 1;
edge posy = 1;
edge movx = 2;
edge movy = 5;

ray of the edge: {x/y} -> 2d vector!!

r(x) = {posx/posy}+x*({movx/movy}-{posx/posy})

edge1 box {posx/posy}={5/10}
edge2 box {posx/posy}={7/0}

re(x)= e1->{posx/posy}+x*(e2->{posx/posy}-e1->{posx/posy})

r(x)=re(x)

solve it to x -> get the colision point check whether it has an x value smaler than 1 but greater than 0 on the ray of the edges and if that's all true the edge colides in it's move the box! Check it for each edge and each pair of edges.....


Hope it helps you....

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!