Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

DarkBASIC Discussion / Get Ground Height and Tilt from Object Terrains: X, 3ds

Author
Message
Latch
17
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 9th Sep 2008 01:59 Edited at: 20th Oct 2008 17:19
Here's some information on getting the ground height from loaded direct X or 3DS object in DarkBASIC Classic. There is an attached file with a few examples that should help bring some of this information to light.

CONTENTS
* Introduction
* What will we actually be doing to get ground height?
* An insect has feelers - so let's follow it's lead
--------Example1.dba (DarkBASIC Classic Collision)

* Using multiple feelers to find vehicle tilt
* Using Slope
--------Example2.dba (DarkBASIC Classic Collision)

* Get Ground Height using Sparky's DLL
--------Example3.dba

* Finding a Vehicles Tilt using Sparky's DLL
--------Example4.dba

* A bit about Math to Calculate Ground Height

INTRODUCTION
DarkBASIC Classic has a useful object one can use to build a landscape/terrain. In DarkBASIC, this is referred to as a Matrix. There are a series of commands that can be applied to a matrix to change it's shape and it's textures so you can fairly quickly and easily start building your world. What really makes using a matrix nice is a handy command called GET GROUND HEIGHT(). With this command you can find the Y position (the height of the matrix's peaks or valleys) at any place on the matrix. This will help you position your objects or characters to be set, walk, or roll on top of the terrain no matter how bumpy it may be.

At some point, one wants to have more control over the design and look of their landscapes that they may not have when using only the built in matrix commands. They wish to build a landscape in a modeling program and load it into DarkBASIC Classic as an X file or a 3DS file. But when it comes to loading in and using a terrain that you have built in a 3D modeling program, there is no equivalent of the matrix GET GROUND HEIGHT() command. The question always comes up: "How do I get the ground height for my terrain like I can from a DBC Matrix?" Throughout the forums this has been asked and addressed many times. At the time of writing this, I had noticed this question resurfacing; especially in regards to tilting a vehicle according to the contours of the terrain. I've decided to address this issue yet again and hopefully provide a few options that aren't too complicated and easy to reproduce and understand. I'll rely on the reader to analyze the attached code to develop a better understanding of the methods I propose.

WHAT WILL WE ACTUALLY BE DOING TO GET THE GROUND HEIGHT?
Quite simply, we will use collision detection with an object and the landscape. There are mathematical ways to return the height at any given point, and I may touch on them, but for the bulk of this reference, we will concentrate on using collision detection to get our ground height.

We will talk about using some of DarkBASIC Classic's built in collision detection as well as using a custom DLL commonly referred to on these forums as Sparky's Collision DLL. Though it may seem straight forward enough, when it comes to using the built in collision commands, there aren't any that immediately lend themselves to returning a 3d point in space except for MAKE OBJECT COLLISION BOX or MAKE STATIC COLLISION BOX. The problem here is that a terrain is rarely only flat and/or square and returning the subtleties in height difference on an incline would be very difficult. Don't worry, we can use other collision commands and figure out the collision point ourselves.

AN INSECT HAS FEELERS - SO LET'S FOLLOW IT'S LEAD!
The first problem to tackle is how to set up the collision test. For the terrain, we will set it to polygon collision (SET OBJECT COLLISION TO POLYGONS) so that it is possible to detect collision anywhere on it. But for our character or main object that is strolling around on the terrain, we won't test it against the terrain, but rather, we will use a "feeler" object to test a point for collision before we move the main object to that position. The reason for this is to help prevent a jittering or a jumping up and down motion of the main object that can occur (and will become more clear as to why) as we explore this method. For our intents and purposes, we will make the feeler object out of a plain that is 1 unit wide and 60 units tall (make object plain feeler,1,60). This gives us a 30 unit spread above and below the terrain. This helps to make sure there is a very likely intersection with the terrain. We can make it smaller (and that can speed up the height test), but for now, 60 units will do fine. We will SET OBJECT COLLISION TO BOXES for the feeler object.

In order to get the height of the collision, we have to move the feeler up when there is collision and continue until there is no more collision, and down when there is no collision until there is collision. Remember, we have 30 units to play with on either end of the feeler, but since we are always trying to keep the feeler just above the terrain (raise it until there is no more collision), we only have to worry about the bottom 30 units. That is to say, once we have raised the feeler to a point where there is no more collision from a point of collision, we take the y position of the feeler and subtract 30 from it. That is the height of the terrain at that point. The accuracy is based on how much you move the feeler up each increment. If you move it up and down .1 units, then the accuracy will be within .1 units. If you move it up and down 10 units, then the accuracy will be within 10 units.

I mentioned a jumping or jittery effect that can occur with this kind of collision testing. Well, if we were moving the main object like the feeler, it would be trying to go up and down as it figured out the ground height using it's own collision. That is why we have a feeler object. Whenever we are about to move our main object to a new position, we send the feeler object out first, have it do it's up and down test to find the height of the terrain, and then we move the main object to that position at the proper height the feeler has found. The goal is not to have any testing going on exactly where the main object is at any given time, but to test where the main object will be moved and return that height before it even gets there. That's the whole premise of this method. And to keep the collision test to a minimum, there never is a test unless the main object is about to be moved.

Please review the code in Example1.dba to get a better understanding of this explanation.

USING MULTIPLE FEELERS TO FIND VEHICLE TILT
Just a reminder, I will use "object" or "vehicle" interchangably to mean the same thing: the main object you are trying to move or position on top of the landscape.

Once we are able to find the ground height, finding the the tilt of a vehicle is just a matter of finding the ground height at the front, back, left side, and right side of the vehicle. The different elevations form different angles and these are the angles at which to tilt the vehicle. These angles can be found by using the slope of the vehicle in both of two directions:
1. The vehicle's relative z direction
2. The vehicle's ralative x direction

If you're 1000 years old like me, you might have to dust the cobwebs out of your brain and try to remember what the slope is. If you're just starting to or recently studied this then you may have an advantage. If you've never heard of it, then this may not make much sense. And, in order for this to work properly, I suggest using a pivot object that the vehicle will be glued to. The pivot object will be positioned to where ever the vehicle is desired to be positioned. The use of a pivot object allows the main object to be tilted on different axes without the complications that can arise when an object's orientation is changed. The pivot object always retains the proper orientation so it can be positinoed without a problem. As the true orientation of the main obejct which is glued to the pivot object only changes relative to a zero degree orientation, the tilts don't get screwed up. That's all I'll say about that. For now, just trust that a pivot object can save some headaches. I actually broke one of my own rules in the example that goes with this. I used 1 of the feelers as the pivot object. It works out ok, but that means the object is positioned directly without waiting for the return value from the feeler - the object is positioned as the feeler is positioned. It'll be ok for this, but you may want to experiment later and set up an independent pivot object that is not a feeler. In any case, here we go.

Your main object, vehicle, character, whatever, has a z size (back to front) and an x size (right to left). The trick is to position feelers at these points where ever the vehicle/object is about to move. Once the feelers are in place, the heights of all of the feelers have to be tested and returned. From these heights, we can calculate the tilt of the object.

USING SLOPE
If the object is on an incline, the front will be higher than the back - i.e. the Y value in the front will be greater than the y value in the back. If we take the difference between these y values, and divide them by the z size, we have the slope of the vehicle along it's z axis. This slope has a relationship to a right triangle called tangent. You might be asking yourself "What right triangle?" A right triangle is formed between the way the object is tilted and it's position as if it were level with the ground. If we take the arctangent of the slope, then we get the angle this slope suggests. Now, the z axis can be rotated in one of two ways:
* around the x axis, if the motion is meant to be up and down
* around the y axis, if the motion is meant to be side to side
Well, we know we are dealing with the front and the back of the object so the only rotation that makes sense is up and down which is around the x axis. That means the front and back tilt is an X angle.

We apply the exact same process using the y heights on the right and left side of the object. This time we divide the difference by the x size. That means we will ultimately be rotating the x axis around the y axis or the z axis. Again, it doesn't make sense to rotate it around the y axis because that would be side to side. We are looking for an up and down rotation (tilt) so that has to be around the z axis. That means the right and left tilt is a Z angle.

We then xrotate and zrotate the object. I use CUREVEANGLE in the example to make a smooth transition from the objects previous angles to the the new tiled angles. That's it! Your vehicle should tilt according to the calculated slopes.

Please review the code in Example2.dba to get a better understanding of this explanation.

GET GROUND HEIGHT USING SPARKY'S DLL
For those of you with the enhancement pack to DB (version 1.20 has this included), you will be able to use DLLs and call the functions in them. There is an excellent collision DLL referred to as Sparky's Collision DLL. This DLL is ideal for getting the information we need to find the "ground height." There is a version for DBC and one for DBPro. We are, of course, interested in the DBC version. You can get a copy of it from here:

Sparky's DLL


The idea is simple: you set up your terrain for collision detection using the command set from the DLL. You then cast a ray from above the terrain to below the terrain at an X and Z position you specify. You then use the Y value that is returned from the intersection point and voila! You have your ground height.

FINDING A VEHICLE'S TILT WITH SPARKY'S DLL
Using the method of determining ground height can be applied to find the angles at which a vehicle may tilt on uneven terrain. Here is the outline:
1. Create a pivot object that will be positioned around the terrain
2. Create your vehicle object that will be glued to the pivot object. The vehicle can also be a limb to the pivot object but for now, let's just glue the vehicle to the pivot.
3. Cast a ray from the center position of the pivot down through the terrain and get the y intersect value
4. Cast a ray from the front of the vehicle down through the terrain and get y
5. Cast a ray from the back of the vehicle down through the terrain and get y
6. Cast a ray from the left of the vehicle down through the terrain and get y
7. Cast a ray from the right of the vehicle down through the terrain and get y

Just like in the previous example where we used feelers and found the slopes of the vehicle, we will do the same but using the DLL commands and ray casting to find collision points. Use the center ray cast to set the height. Use the arctangent of the difference between the back and front heights divided by the z length of the vehicle to figure out the X angle tilt. Use the arctangent of the difference between the left and right of the vehicle divided by the x width of the vehicle to figure out the Z angle tilt. Then xrotate and zrotate the vehicle (not the pivot object) according to those angles.

Please review Example3.dba for a demonstration of getting the ground height using Sparky's Collision DLL, and Example4.dba for a demonstration on how to get the vehicle tilt.

In both the DLL and DarkBASIC collision tilt examples, there is a generic function to calculate tilt that can be used in most situations where you want to calculate the tilt of an object on a terrain. This should get you up and running with your project fairly quickly.

A BIT ABOUT MATH TO CALCULATE GROUND HEIGHT
I hadn't planned on going much into this and may do so at a later date, but I think it's important to at least touch on a concept or two. I'll start with slope.

We've already seen that the tilt of a vehicle can be calculated using the slope of various points around our vehicle. We could use a similar method to get the ground height at any point on our terrain without using collision. If our terrain is made up of nice and neat sqaures where each "tile" is made up of 4 points, we can find the slope of any or all of the edges that make up the tile. If the tile is divided into two triangles (which would most likely be the case) we would have to know which edge belongs to which triangle and over which triangle we are trying to find the ground height. With that information, we can use the current position and find it's height using the angles we would have calculated using the slope, and some trig.

A more standard way, is to use the the normal of a face; a face being one of the triangles I mentioned make up a tile on our terrain. The normal is a vector that points perpendicular to the face. By using the dot product we can use the normal, the point we want to check, and a starting point to figure out the height.

Unless you have a parsing function, if you load a terrain into DarkBASIC Classic, you're not going to have an easy time finding the normals. Even if you use MAKE MEMBLOCK FROM MESH to convert your terrain to a memblock, the normals that are stored there are vertex normals and they are indexed to the vertices. This means the vertices can be in any order in the memblock and it can be tricky to try and figure out which face you are above. This is the same trouble you'd run into if you were trying the slope method height calculation.

To solve this, you could go through every face index and figure out an order for your vertices. From there you could average the vertex normals to get the face normal - or you could recalculate the face normal using the crossproduct, as the vertex normals may be an average of several faces surround the particular vertex and the average of them may not give you your face normal.

Or better yet, take matters into your own hands and sort the vertices from bottom to top or top to bottom of your terrain. From there calculate the face normals, and then use the dot product to calculate the height at any given point.

I'll stop there for now giving you a bit of meat to chew on.

Enjoy your day.

Attachments

Login to view attachments
Sixty Squares
17
Years of Service
User Offline
Joined: 7th Jun 2006
Location: Somewhere in the world
Posted: 9th Sep 2008 02:11 Edited at: 9th Sep 2008 02:11
Very useful tutorial . If only you'd posted this a few days sooner it would've saved me some trouble . However, I'm sure this will be quite useful for tilting objects on a .X level I have... Thanks!

Latch
17
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 9th Sep 2008 02:19
You're welcome and thanks! I know as a tutorial it's a bit sparse as of yet, but I think the examples carry the real info.

Enjoy your day.
Latch
17
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 14th Sep 2008 02:36
Sorry for the double post but Code Eater alerted me to a missing file in the download. I've updated the download in the top post.

Enjoy your day.
Stig Design Stig Magne
18
Years of Service
User Offline
Joined: 23rd Mar 2006
Location: Norway
Posted: 2nd Oct 2008 06:38
Realy Nice Thank`s

**StigDesign** cheap 3D Modell Pack`s at www.stigdesign.piczo.com (for Home&Comersial use)soon other aplication`s
Latch
17
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 19th Oct 2008 21:52
update...

Enjoy your day.
Dark Dragon
16
Years of Service
User Offline
Joined: 22nd Jun 2007
Location: In the ring, Kickin\' *donkeybutt*.
Posted: 26th Oct 2008 04:13
WoW. nIcE tUts,Latch!

very,veryy useful and helpful. will use and learn from them.

Your signature has been erased by a mod - Please reduce it to 600x120 maximum size
BN2 Productions
20
Years of Service
User Offline
Joined: 22nd Jan 2004
Location:
Posted: 26th Oct 2008 08:55
as far as rotating it, I recently had an idea. Could you find the 3 closest vertexes on the terrain object to the player then use those as two vectors and use the cross product to find the normal to the face? This could then be used to be turned into rotation values. It is more math based, but has anyone tried it? If not I will try it out.

Ever notice how in Microsoft word, the word "microsoft" is auto corrected to be "Microsoft" but "macintosh" just gets the dumb red underline?
Latch
17
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 26th Oct 2008 20:36 Edited at: 6th Nov 2008 20:15
Quote: "Could you find the 3 closest vertexes on the terrain object to the player then use those as two vectors and use the cross product to find the normal to the face?"


Yes. But it's not the 3 closest, it's the 3 vertices of the current triangle you are over. If you can find the normal to the face you are currently over, you can get the ground height. If you can get the ground height you can find the tilt in the same manner as using the slope for multiple ground heights at x and z positions relative to your position.

Quote: "It is more math based, but has anyone tried it?"

Yes and it is very fast.

I like this line of thinking, and it is a basis for finding the terrain height. However, you have to know ahead of time the size of each tile in the terrain and you have to know at which vertex the current y height is. In a terrain file, the vertices could be laid out in any order. Maybe groups of quads (4 vertices) for example. If we look at the terrain from overhead, we'll consider +z as up and +x as to the right. Vertex 1 could be bottom left, vertex 2 could be above that, vertex 3 could be up and to the right, and vertex 4 could be to the right of vertex 1. Each quad's set of vertices could be formed in a clockwise pattern. This could also mean that the vertices are doubled. The next quad could start at the previous vertex 4 but be vertex 5 and vertex 6 would be previous vertex 3 (occupying the same 3d space). Makes thing a little trickier. DBC may convert the quads in one of two ways:



The orientation of the triangles (faces) within the tile is important as choosing the wrong one could yield a completely different normal. This is all solvable. But it gets harder if the tiles of the terrains aren't square. If, for example, you had a terrain of squares in Anim8or and you made several smoothing passes. The resulting vertices and faces are not 100% evenly distributed so it can make things very difficult. In Blender, if you set proportional on when moving points on a grid, the resulting triangles are in both orientations. In these cases, using Sparky's DLL solves the problem (I believe Sparky's ray casting is using the face normal and dot product to see if there is an intersection with the ray and the face or calculated plane) at high speed or you could use the DBC collision test though slower.

Enjoy your day.
TheComet
16
Years of Service
User Offline
Joined: 18th Oct 2007
Location: I`m under ur bridge eating ur goatz.
Posted: 29th Dec 2008 15:10
Hey Latch! I just wan`t to thank you for this awesome tutorial! Look what if brought to me: http://www.youtube.com/watch?v=N53EM67DYbg

TheComet

Peachy, and the Chaos of the Gems

Latch
17
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 30th Dec 2008 00:51
That's great! That must be your multiplayer tank battle program.

Sooner or later, I might add interior sliding collision to this reference...

Enjoy your day.
Caleb1994
15
Years of Service
User Offline
Joined: 10th Oct 2008
Location: The Internet you idiot!
Posted: 30th Dec 2008 01:17
ya that looks great comet! what did you make the level with? i like it
TheComet
16
Years of Service
User Offline
Joined: 18th Oct 2007
Location: I`m under ur bridge eating ur goatz.
Posted: 30th Dec 2008 20:07
Quote: "Sooner or later, I might add interior sliding collision to this reference..."


Could you please? I am having great problems with that...

Quote: "ya that looks great comet! what did you make the level with? i like it "


With AC3D.

TheComet

Peachy, and the Chaos of the Gems

Caleb1994
15
Years of Service
User Offline
Joined: 10th Oct 2008
Location: The Internet you idiot!
Posted: 31st Dec 2008 00:15
oh i like the randomness of it lol
Latch
17
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 12th Feb 2009 03:30
I've been considering making an advanced collision thread which would include the information contained in this thread and a few other tidbits like interior sliding collision using sparky's and other DBC commands - not necessarily using static collision boxes or object collision boxes.

I want to get an idea of how many people would actually be interested in this. It seems that the DBC board is getting less and less activity and I don't want to put time or effort into something that will be at most, glanced over.

Enjoy your day.
Caleb1994
15
Years of Service
User Offline
Joined: 10th Oct 2008
Location: The Internet you idiot!
Posted: 12th Feb 2009 05:50
I would
Anto
18
Years of Service
User Offline
Joined: 30th Oct 2005
Location: Brisbane, AUS
Posted: 20th Feb 2009 07:45
Yes I could be!! I working on this WipEout style game for years on and off. As usually I don't really have enough time that I could have liked to do programming, I spend like 2 hours a week on my game.. not enough... This sliding collision might help!!! originally the track was made using Matrix, but it was a huge slow down. I used your get ground height that you just post out, It helps lot and it is what I need.. I've redesigned the tracks and it is much better detailed and more flexable to design the way I want it to be. So sliding collision will be what i need next to stop my ship going off the track!

keep up your awesome work latch

Cheers Anto.
Stig Design Stig Magne
18
Years of Service
User Offline
Joined: 23rd Mar 2006
Location: Norway
Posted: 26th Feb 2009 07:28
mee to as i have newer get the sparky's collision to work so
the only thing i hvae gotten to work is the sliding collision exampel that followed DBC and work from that one
and i have noticed that if i whant to make a driving game it can quickly getting blocky as the static collision box is fine for cube`d/box like House`s/Bulding`s but what about fence on the side in a road turn that one i have worked hard on and nott get to work good so all collision and Matrix Related Tutorial`s Will be given credit to on my games i need to use thoes code`s

The reason for my Sliding collision and on road Turn`s and Following matrix/3D Modelled land Skape is that i have tried several times on creating a game like Driver/Test Drive/NeedForSpeed series but with my uppgrade`s and 100%Self Created non a Copy of non of thoes but my werry own car game with my idea and design

and Latch Thank`s for your poasting`s on the DBC forum`s i realy like it same is for TDK`t Tutorial`s it have realy helped mee maked me realise that i realy have hobby about game making Programmin/3DModelling/2DTexture making youst chekk my web page and youl see
Cheer`s

**StigDesign** cheap 3D Modell Pack`s at www.stigdesign.piczo.com (for Home&Comersial use)soon other aplication`s
Latch
17
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 9th May 2009 18:27
Sorry guys,

There seemed to be so little interest in this that I haven't posted anything on interior collision/sliding collision. I still may; I actually have a bunch of notes written up, but my attention with DBC is now on lighting, effects, and 3d animation - in an attempt to create more "realistic" environments.

But collision isn't completely off of my radar.

Enjoy your day.
Libervurto
17
Years of Service
User Offline
Joined: 30th Jun 2006
Location: On Toast
Posted: 11th May 2009 00:53
This looks pretty neat Latch. I've bookmarked it for when I get back into 3D stuff. Haha I've just realised its been about 2 years since I made anything in 3D! There is just so much to learn about coding!
Unless you include that 3D drawing with 2D commands thing you helped me with.

There should be some way of publishing these tutorials on the forums as references without any comments, there are tons of good tutorials out there but they get lost amongst the garbage, if TGC added a reference section sort of like a library I would be most pleased
hmm now I mention it wasn't there a DBC wiki page?

Riddle: The more you take, the more you leave behind. What are they? Answer
Stig Design Stig Magne
18
Years of Service
User Offline
Joined: 23rd Mar 2006
Location: Norway
Posted: 17th May 2009 12:29
(OBese87 ) i remember aswell but after i chekked it. it whass gone


**StigDesign** cheap 3D Modell Pack`s at www.stigdesign.piczo.com (for Home&Comersial use)soon other aplication`s
Libervurto
17
Years of Service
User Offline
Joined: 30th Jun 2006
Location: On Toast
Posted: 23rd May 2009 04:16 Edited at: 23rd May 2009 04:23
@Stig
really, that's a shame.

I corrected your sig. I don't mean to be patronising, I know English isn't your first language. Correct English makes you look better, especially if you're trying to sell something.
Quote: "**StigDesign** cheap 3D model packs for home & commercial use at http://www.stigdesign.piczo.com, other applications coming soon."


Or you could even have
Quote: "StigDesign - cheap 3D model packs for home and commercial use. Other applications coming soon."


There is an eight letter word. You can insert a letter into it or remove a letter from it without changing its meaning. Answer
Latch
17
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 10th Jul 2009 16:47
Quote: "There should be some way of publishing these tutorials on the forums as references without any comments"


I guess the code base could be used for that purpose.

Enjoy your day.
Dark Dragon
16
Years of Service
User Offline
Joined: 22nd Jun 2007
Location: In the ring, Kickin\' *donkeybutt*.
Posted: 13th Jul 2009 00:41
Yeah. This tut should go on some.........tut board.
Wait, Why dont we have any of those?
That be cool, to have a DB and DBpro tut board(and not even have the tuts as stickies here, on the regular board.).

Your signature has been erased by a mod because it was too big.CHANGE IT OR DIE!!!!!
Dark Dragon
16
Years of Service
User Offline
Joined: 22nd Jun 2007
Location: In the ring, Kickin\' *donkeybutt*.
Posted: 19th Aug 2009 00:53
Awsome! I found it again, Thanks Latch!

*Bump*

(\__/) HHAHAHAHAHAH!
(O.o ) / WORLD DOMINATION!!!!!!!!!!
(> < )
Libervurto
17
Years of Service
User Offline
Joined: 30th Jun 2006
Location: On Toast
Posted: 19th Aug 2009 04:20
Quote: "I guess the code base could be used for that purpose."

The code base is great but it's just for code really. Tutorial base ftw!
that's actually my first ever ftw

TGC Forum - converting error messages into sarcasm since 2002.

Login to post a reply

Server time is: 2024-05-09 05:27:48
Your offset time is: 2024-05-09 05:27:48