Archived

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

bodoche

2D collision's detection , worms...

Recommended Posts

I try to create a worms like game and I am thinking about how to if a worms is on/under/above the ground and if the worms is on a slope. Do you have any tutorial that explains how to do collision''s detection of a man and the ground. (I hope my english is not too bad, I am french..)

Share this post


Link to post
Share on other sites
Here''s an idea. Create a heightmap, which is just a 2D array, of the map. Let''s say that height map contains byte values (0..255). So for example, areas where the height of terrain is heigher you would have a value closer to 255, and where the height is closer or below sea level it will have a value closer to 0.

When you create the terrain height map, the smaller the slope the smaller the change in values in the height map, which will make for a smooth transition. If you want a steep slope, the change in the values of the heightmap will be larger in the area where the slope is steeper.

I usually use Photoshop to create heightmaps and then save them as RAW files. Then just load them from your program. Another way is to use an algorithm to generate the heightmaps, but that''s an advanced topic and I suggest you stick to making the heightmap''s in a program and then simply loading them into your array that holds the heightmap for the current level.

Take a look at this as an example of a heightmap.

Share this post


Link to post
Share on other sites
worms isn''t 3D (unless a new one came out while i was sleeping)...
bodoche, i don''t know how you are storing your ground/dirt/whatever data, but basically:
check the spot directly under the character''s feet (the next pixel down, or whatever). if it is empty (no ground), make him fall and then check again. that will allow them to fall if the ground under them is broken.
check the pixel that is above the last one (ie the pixel that the character''s feet cover). if it is empty (no ground), good. otherwise, he has somehow sunken into the ground and you probably want to move him. you could just move him up a pixel and then check again, which should work for you. it''ll look weird if the character is completely overlapping the ground(he''ll levitate through the dirt to the top of the ground), but it will keep the worms from being embedded in the landscape.
the slope is the same in 2D as it is in 3D (except simpler). slope is calculated as rise over run, or (change in y)/(change in x). you can get this with two points under your characters. you will have to find the highest point of the dirt at two points under his feet (say, two pixels left and two pixels right of the center of the character''s feet). if these two points are (x1,y1) and (x2,y2), slope=(y2-y1)/(x2-x1).
what you do with that slope is up to you
sorry if this makes little or no sense, if you need clarification just say so!

--- krez (krezisback@aol.com)

Share this post


Link to post
Share on other sites
quote:
Original post by krez
worms isn''t 3D (unless a new one came out while i was sleeping)...



And the algorithm I''m suggesting is not 3D either. I think you were mistaken by the website since it gives how to RENDER a terrain in 3D. That''s different. What I''m suggesting is to use a heightmap for the worm''s movement. That way you can have some really cool worm effects if you use a heightmap. You can make the worm go up and down smoothly, and it will look as if there was really ground below him. If you make the heightmap match the background for the level, it will look as if the worm is really walking on top of objects, hills, rocks, etc...

Hope this helps...

Share this post


Link to post
Share on other sites
my bad, Gladiator...
i can only look at things quickly when i'm at work, as i do not want to become engrossed and forget that my boss might walk around the corner
as you might guess, he thinks it is more important to be productive than to surf the web.

--- krez (krezisback@aol.com)

Edited by - krez on November 16, 2001 4:54:10 PM

Share this post


Link to post
Share on other sites


I''d go for heightmaps as described above. An excellent solution.

Oh, and heightmaps can be used well in 2D platformers as well.


Tata

llyod

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
A simple heightmap won''t work in Worms, because you can dig tunnels and go underground etc.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
its quite simple, method i suggest 9since you need to store the entire map anyway). Simply store the entire ground area in a large area which details whether the position is valid or not. Your physic code shall check against this array. When you dig, just make the particular spots in the array 0 (or any other unused value). You can also use this array for drawing (since you can actually store the pixels color and the only time a pixel is possible to move through is if it is the transparent color you are using)

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
yeah a height map wouldn''t be useful. No minor modification would make it suitable. What you need is a simple black and white bitmap. Then you need a way of doing physics, that is hard. You''ll probably need to assign slope information to every pixel too. To calculate it you would need to look at the pixels in the surrounding area. You''d probably just want to store every possible combination of 5x5 pixels and associate it with a slope. Then every time the terrain is changed compare each pixel in the area to the maps and assign it the matching slope.

Share this post


Link to post
Share on other sites
thank you for all those informations. I thought about this during the weekend and I may have found a solution.
In fact there are two solutions.

First I want to know if my worm is merely under/above/on the ground.
I get my worm's position (x, y). and I check on a vertical segment if there are pixels transparent or not.

If I find a transparent pixel and just after a none transpent pixel I can calculate the difference(y') between my worm and the ground.

If I don't find any transparent pixel or any none transparent pixel this is just my worm is completely under the ground or in the air.

Second solution:
I want to find the slope for exemple if I want the worm rebound on the ground.

I use a circle C. I divide this circle into two part according to a vertical line.
I check all pixel which are on the circle. If I find a transparent pixel and just after a none transparent I store this pixel. I get two list of pixel detected, one list for the left part of the circle and one for the right part. I take the lower pixel of each list.
So I have two point, I am able to calculate a line y=ax+b and to do anything with it.

I hope you will understand what I mean.

Edited by - bodoche on November 18, 2001 11:14:47 AM

Share this post


Link to post
Share on other sites