PDA

View Full Version : How would you do something like this?



FusionWolf
26-08-2005, 06:59 PM
Hi,

If you have a 2-D grayscale heightmap where black is lowest area and white is the highest. Then there's a giant ball which is on halfway one of those "hills" there. So, the ball should get moving to the downhill straight to the black area there but where does ball "know" which direction to move? Somehow it has to be calculated from the pixels surrounding the ball but how? Which kind of "range" checking you would do in situation like this? And what if the ball is moving (by some other force than gravity) and the downhill gets under the ball to the right side for example. How you would calculate how it affects to the current speed and direction of the ball? Of course ball should change its direction littlebit to the right and speed may increase a little, but how much? How to calculate that? Color of the background pixels defines that I know, but......

Rahakasvi
26-08-2005, 07:16 PM
It's hard to explain (even harder with words). I would definitely do it with vectors. Try to do it first in 2d. So the heightmap is 1-D greyscale bitmap. And the ball is circle.

So if you have values 0 and value 128 (grey) you can easily calculate the vector between those coordinates (0,0) and (1,128)

From the vector you can create supportvector (dunno if it is the right word) of the terrain.

Then you can determine movement vector by adding gravity vector and terrains supportvector together. With 3D, it is bit complicated math of course :)

FusionWolf
26-08-2005, 08:32 PM
It's hard to explain (even harder with words). I would definitely do it with vectors. Try to do it first in 2d. So the heightmap is 1-D greyscale bitmap. And the ball is circle.

So if you have values 0 and value 128 (grey) you can easily calculate the vector between those coordinates (0,0) and (1,128)

From the vector you can create supportvector (dunno if it is the right word) of the terrain.

Then you can determine movement vector by adding gravity vector and terrains supportvector together. With 3D, it is bit complicated math of course :)

Okey, I understand what you mean and I was pondering something similar, but still the main problem is wondering me and did not came clear with your post. Maybe if I try to ask same question with other words.

The background (height map) is 2D so there's only X and Y -axis. The color of the pixels tells how hight current point of the map is. If value is 255 then that's the highest point possible and color is white. If value is 0 then that's the lowest point and color is black.

So, if that ball is "standing" somewhere between the highest and the lowest areas (halfway of the hill I may say) then it should get going to the downhill. Toward to the black area. Okey so, I now that black area is there, but that ball doesn't. There should be somekind of pixel checking system which scans the nearby area and after we know which way to move we come to those vectors but before that we have to decide which way to start moving and those background pixels tells that. I just don't know how I could check those pixels and which pixels needs to be checked? I mean how far I should check from the ball and how "wide" the range of checking should be? And which kind checking algorithm should be to prevent eating too much CPU power. I think I should do somekind bounding box for the sphere and check pixels inside that and calculate somekind of average value of each scanline. Am I correct?

Or am I going entirely to the wrong direction with my theory? Should this be completely something else? :roll:

Rahakasvi
27-08-2005, 10:07 AM
I don't think the ball has to know where the black area is.

It only has to know what colors are beneath it. So it scans ever pixel underneath it, and determine what is the lowest part (darkest gray) from it's point of view and move towards that. Or should it be like, even if there is a valley of slightly dark gray (like 260) which is surrounded by brighter colors, and still be able to find it's way to the dark areas?

Anyway that is not good method if you are trying to create realistic ball movements. Creating realistic ball method, I think you only need to know the point(s) ball is touching the ground. Then (using basic psychics) you should be able to calculate vectors to tell ball where to move.

JSoftware
27-08-2005, 11:43 AM
i would make a pretty hack which for each step checked where the lowest pixel was besides it. then you know the direction. You add that direction up as a vector. this vector represents the speed in each direction and is added to the position of the ball at each step. you also introduce a friction variable you can multiply with the speed at each step

i haven't implemented this but i think it should be pretty simple to implement. good luck

Regards
Jeppe Johansen

Clootie
27-08-2005, 02:02 PM
If "ball" size is comparable to "pixel" size then you can just take 4 pixels surrounding ball and calculate gradients from them.
For example:
dx = Height(x-1) - Height (x+1)
dy = Height(y-1) - Height (y+1)
Now vector N = (dx, dy) gives you something like normal to surface. From this point you can use this normal to calculate "surface reaction" force Freaction = N * (N dotproduct Fgravity)

But except surface reactiion force you also [possibly] have friction force which will stop your ball with time at local suface minimum.

After applying above mentioned forces over time your ball will have kinetic energy (speed*mass ?) while it's moving - so it will be able to roll over small hills.
------
These are the basics - you should check them and probably correct at some points. :D

WILL
27-08-2005, 06:33 PM
Perhaps tux can help a bit with this one? He has a great book on physics for games. It's bound to have something for this. :)

FusionWolf
27-08-2005, 08:33 PM
Perhaps tux can help a bit with this one? He has a great book on physics for games. It's bound to have something for this. :)

What's the name of that book?

FusionWolf
07-09-2005, 01:19 PM
If "ball" size is comparable to "pixel" size then you can just take 4 pixels surrounding ball and calculate gradients from them.
For example: ....

<--- clip --->


Clootie my man. Once again you kicked me to the right direction. After hours and hours of studying Pythagoras and Newtons laws I finally managed to do what I wanted to. Thank you very much (once again :wink: ).

Here's little sample I made. It doesn't have any fancy graphics and it's very unoptimized and very rude. It uses combined Win32API and Delphi VCL for graphics and math. It also loads height map from the bitmap file but hey, it's finally working (kind of).

http://koti.mbnet.fi/fwolf/misc/roll-on.zip Size ~300KB

It's zipped with the folder, so just unzip it to the root where you want it to and it will make the folder for you.

Edit: By the way. One can modify the bitmap anyway one wants to. Just keep it grayscaled and it will be fine.