[2 days left] Whatâ€™s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
Solved

# Aligning Vehicle to terrain in OpenGL

Posted on 2003-11-04
Medium Priority
1,264 Views
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.

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
Question by:Excalibur81
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 6
• 5

LVL 45

Expert Comment

ID: 9685912
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

Author Comment

ID: 9690352
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

LVL 45

Expert Comment

ID: 9694073
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

Expert Comment

ID: 9695318
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

Author Comment

ID: 9696900
Arudson:

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 )

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

Author Comment

ID: 9696934
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

LVL 45

Accepted Solution

sunnycoder earned 800 total points
ID: 9700295
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

Author Comment

ID: 9703757
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

Author Comment

ID: 9712607
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

LVL 45

Expert Comment

ID: 9712621
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

LVL 45

Expert Comment

ID: 9712634
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

LVL 45

Expert Comment

ID: 9712636
what a timing :-D
thanks
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Artificial Intelligence comes in many forms, and for game developers, Path-Finding is an important ability for making an NPC (Non-Playable Character) maneuver through terrain.  A* is a particularly easy way to approach it.  Iâ€™ll start with the algorâ€¦
As game developers, we quickly learn that Artificial Intelligence (AI) doesnâ€™t need to be so tough.  To reference Space Ghost: â€œMoltar, I have a giant brain that is able to reduce any complex machine into a simple yes or no answer. (http://www.youtuâ€¦
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a qâ€¦
Is your data getting by on basic protection measures? In todayâ€™s climate of debilitating malware and ransomwareâ€”like WannaCryâ€”that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.â€¦
###### Suggested Courses
Course of the Month14 days, 2 hours left to enroll