• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1269
  • Last Modified:

Aligning Vehicle to terrain in OpenGL

Hi all,

Im writing a 3D game which is written in C++ OpenGL, and in short is a bunch of tanks driving around a terrain.

(more info @ http://www.extremesolutions.asheltie.com/Latest.html )

Ok my problem is that i want to have the vehicles alligned to the ground.

Now under any given tank, the ground may consist of ~ 20 vertices.

I can get the x,y,z info of each of these vertices, as well as the polygon normals.

What i need to do is have it so the tank is aligned across the vertices in a realistic fashion.

Im happy to treat the Tanks chassis as a single plane right now, if thats any help...

Hard code or Algorithms welcome :)

Thanks,

-Rob-

0
Excalibur81
Asked:
Excalibur81
  • 6
  • 5
1 Solution
 
sunnycoderCommented:
Hi Rob,

I am not into 3d graphics, but here are my two cents ...

what I could gather from your description was that you have an uneven terrain and you want tank to be placed on that terrain in such a way that it looks as if it is on an uneven terrain indeed... am I right?... If so, read further :o)

to get realistic alignment of tanks


              +----------------------------------+
              |        |                          |        |
              |____|                          |____|
              |                                            |
              |                                            |
              |                                            |
              |                                            |
              |                                            |
              |                                            |
              |                                            |
              |                                            |
              |                                            |
              |                                            |
              +----------------------------------+

assume that the outer big box is your tank chasis ... get terrain elevation at the boxes shown  ... similarly get terrain elevation at the lower left and right corners...

with respect to terrain, your tank can be inclined in two different ways (up or down and left or right) ...
to get the up/down alignment
take max of two front boxes ( F) and max of two rear boxes (R) and find F-R

to get right/left inclination, use the same process with two left boxes and two right boxes

NOTE::: The solution I suggested is purely intutive and I have no idea if it would really work

Cheers:o)
Sunny
0
 
Excalibur81Author Commented:
Thanks for the idea,

However, ive already implemented a similar idea to this (take a look at the latest news link :P )

The problem with this idea is....
What happens if say the height at the left of the tank is 20, and the height of the right is 30.

Now the tank will nicely incline it self with a small angle right?

Yes thats fine, but now imagine that in the middle of the tank is a higher piece of terrain, say 60 high.

The tank will dissappear into the mountain :)

So you see my quandry?!

I suspect the real answer will be an extension of your algorithm described above,


I posted a possible collision algorithm on my site, Have a read and see if you can come up with anything better,
http://www.extremesolutions.asheltie.com/Latest.html

Thanks,

-Rob-


0
 
sunnycoderCommented:
the scenario you described was on my mind when I posted... If such a scenario arises, you tank should ideally be stuck on top of the mountain since none of its wheels will ever reach ground :p

anyway... the way out of this dilemma seems to be this

             +----------------------------------+
             |                                            |
             |______________________|
             |                                            |
             |                                            |
             |                                            |
             |                                            |
             |                                            |
             |                                            |
             |                                            |
             |                                            |
             |                                            |
             |                                            |
             +----------------------------------+

apply same method with a small front strip in additionto the two boxes... that way such elevation wont go unnoticed (you will be taking the maximum value )
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
arudsonCommented:
A quick and dirty way to do this would be to setup an "oriented bounding box" (OBB) that encompasses the tank. Then, you could test whether any terrain vertices are inside the bounding box. If you are familiar with the plane equation, this should be pretty routine:

Basically the idea here is you want to find the distance between each landscape vertex, and all 6 sides of the OBB. The plane equation does just that, it gives you the distance from a point to a plane.

Plane equation: Ax + By + Cz = D
Where: A,B,C are are the xyz components of the plane normal, and x,y,z are the components of the point (vertex) location. D is the resulting distance from the point (vertex) to the plane. Look closely, and you'll see this is literally just a dot product of the point and the plane normal.

Calculate that for each side of the OBB, and if you get all negative distance (D) values, the vertex is on the inside of the plane. Otherwise the point is outside the OBB.

SOOOoooo....

Since we're only concerned with the bottom of the tank (assuming your terrain has no caves) we will use the distance of the highest "breached" vertex from the bottom plane (the D value from the OBB's bottom-side plane equation). Raise the tank up by THAT value, and BOOM.. Your tank is now sitting ontop of your terrain. Of course, it won't be *aligned* to the terrain. That's a little more work, but this should at least spark some ideas in your mind about how that's accomplished.

Good luck!
0
 
Excalibur81Author Commented:
Arudson:

Thanks for your input !
I understand what you are saying, and i have already implemented an idea like this too.
But as you say,  
"Your tank is now sitting ontop of your terrain. Of course, it won't be *aligned* to the terrain. That's a little more work,"......

My original question was just how to align to terrain :)

Sunnycoder:

I see how this new front section would stop the tank from being able to drive 'through' a section of terrain,
But what about in the horizontal direction? The tank can still slide into the terrain (These are particularly "arcady" tanks :P )

I was thinking about your original idea, and then i thought about the center of gravity..

What if you extend the 4 boxes to all align at the center of the tank?

1 - Divide tank into 4 parts:
--------------
|       |       |
|------------|
|       |       |
--------------

2 - Get highest point in each sector

3 - Get highest point of 2 front sectors of tank, and same for to rear sectors.

4 - Determine angle from the difference in these heights, position tank at height = (some value relative to that point from the center line)

5 - Repeat 3 & 4 for left and right sectors

This way, the tank will be oriented against the terrain, and also with the center as the point at which orientaion takes place, making it the actual center of Gravity....

What do u think?
0
 
Excalibur81Author Commented:
Since i cant edit my post, let me correct a few things....

"But what about in the horizontal direction? The tank can still slide into the terrain ".....
I realise that puting a similar box across the sides of the tank will also solve this problem, but im merely suggesting a possible alternative....

Extension of steps in my alg...

4 - Determine angle from the difference in these heights, position tank at height = (some value relative to that point from the center line)

4a - Rotate tank about center point to this angle found at (4)

5 - Repeat 3, 4, & 4a for left and right sectors
0
 
sunnycoderCommented:
that's a good idea ... however, thinking about centre of gravity I have an idea too

your tank touches the ground on left and right throughout its length ...

find the highest elevation in the left and right strip and their respective position with respect to the centre of gravity

next find the ground highest elevation under tank chasis... If this is not included in the left and right strips and is high enough to touch the tank, use it for calculating alignment otherwise reject it...

your realistic alignment will be function of these values ...

you can assign some weight to elevation depending on its distance from COG. You are right in your algorithm about taking angles and stuff, but I feel that left and right strips with entire plane will give a better alignment. What do you say?
0
 
Excalibur81Author Commented:
hmmmm, thats a good idea.
I think your algorithm will give a more accurate alignment...

I only just finished uni exams yesterday (so haven't had much time to actually code this :P), so im going to spend today implementing both your algorithm and mine, and see which works best/runs fastest

I'll get back to you when i manage to sort out an answer.

Thanks for the dedication to my problem!

-Rob-
0
 
Excalibur81Author Commented:
Well thanks!

I tried both yours and my alg, and actually i had a few probs coding each, what with trying to determine if a particular height was more important than another, with respect to its distance from the C of G....

In any case, i have a solution that works *ok* for now, ill upload it to my web site, so u can see if u are interested.

(bare in mind, that the physics does not yet allow for the fact that a tank cannot scale a vertical cliff :D )

I went with my one for now, (i.e the 4 sectors) but i will definitly advance to your one later on,  Thanks heaps!

-Rob-
0
 
sunnycoderCommented:
thats great ...
post back if you need more suggestions... If you are satisfied with the answers to current question then pls close it

Good luck
0
 
sunnycoderCommented:
oops .. sorry, I mistook you to be a new user... just checked your profile ... you are familiar with closing questions and do not seem to abandon questions ... take your time but make sure that you do close it ;o)
0
 
sunnycoderCommented:
what a timing :-D
thanks
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 6
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now