# 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.

### Newcomers DBPro Corner / Tilting Object on a Matrix

Message
Posted: 14th Dec 2002 06:28
ok... here's what Im trying to do. I have a car, driving around on a pre-made matrix. As the car drives around it though, I want it to tilt correctly so the wheels are on the ground. Know what I mean? Like in the Tank demo that comes with DB where the tank tilts to match the landscape. I tried looking in the code for that demo, but it's all gibberishy.... the variables mainly. Anyway, can someone help? Thanx!
Posted: 14th Dec 2002 16:23
I tried to do the same thing a while ago, I tried using sine and cosine to tilt my car at the right angle, it worked ok but often it went through the matrix because it tilted too much, and I only got it tilting forward, not side to side. I only have the demo of DB so if you could post the tank code in then maybe I could have a look at it, but in the meantime here is what I know about it:

This is triganometry, if you dont know how its done.

In any right angled triangle if know any 1 angle and any 1 side then you can find out what the other 2 angles and 1 side are using sine, cosine and tangent. You find these commands in DB and on any scientific calculator.

(the hypotinuse is the longest side in a right angled triangle, the one opposite to the right angle)

/|
/ |
hyp / |
/ | opp
/ |
ang -------

the sine of the angle = the opposite side to the angle/the hypotinuse
the cosine of the angle = the adjacent side to the angle/the hypotinuse
the tangent of the angle = the opposite side to the angle/the adjacent side to the angle

you can find the angle if you know the sine, cosine or tangent using the arcs.

angle = arcsine of opp/hyp

These are usefull for rotation in programming, DB has commands which do it for you though.

newxvalue, newyvalue and newxzvalue calculate the amount of units you have to go if you want to go in a certain direction for a certain amount of units, if you are at an angle that isnt 0,90,180 or 270 then you are going to move along the y axis some and the z axis some. This is where triganometry is important.

The maths equivilent of newx = newxvalue(x,a,d) is

newx = x+sin(a)*d

it is actually quicker to type but not every one may want to learn maths. Basically what it is doing is finding the sine of the angle, this equals "the adjacent(amount you have to move along the x axis)/the hypotinuse(the distance you want to move)" or rearranged "sin(ang)*hyp=xdist". You add that onto the original x value and there you have it.

What I did in the program was found the ground height at the front and back of the car, subtracted them to find the differernce(the opposite side) and then divided it by the car length(the hypotinuse) and then found the sine of that to get the angle to tilt the car by. Mine wasnt a very successful program, but Im sure that it is possible to do it using a similar method.

If this dis#dn't help you then I hope it helps someone else, you probably know everything about trig already dont you, oh well, glad to try an help.

Posted: 14th Dec 2002 16:24
Yup the diagram really mucked up, just add a few spaces at the beginning of the lines to line up the triangle and itll make sense.

Posted: 14th Dec 2002 19:25
*still confused* Well, here's a small snippet of code from the Tank demo, the part that supposedly does this:

rem Calculate four X+Z coordinates for each corner
foot#=110
ta#=wrapvalue(a#-45)
frontleftx#=newxvalue(x#,ta#,foot#) : frontleftz#=newzvalue(z#,ta#,foot#)
ta#=wrapvalue(a#+45)
frontrightx#=newxvalue(x#,ta#,foot#) : frontrightz#=newzvalue(z#,ta#,foot#)
ta#=wrapvalue(a#+225)
backleftx#=newxvalue(x#,ta#,foot#) : backleftz#=newzvalue(z#,ta#,foot#)
ta#=wrapvalue(a#+135)
backrightx#=newxvalue(x#,ta#,foot#) : backrightz#=newzvalue(z#,ta#,foot#)

rem Calculate degree of tilting from corner heights
frontlefth#=get ground height(1,frontleftx#-mx#,frontleftz#-mz#)
frontrighth#=get ground height(1,frontrightx#-mx#,frontrightz#-mz#)
backlefth#=get ground height(1,backleftx#-mx#,backleftz#-mz#)
backrighth#=get ground height(1,backrightx#-mx#,backrightz#-mz#)
across#=((frontrighth#-frontlefth#)+(backrighth#-backlefth#))/2.0
length#=((backlefth#-frontlefth#)+(backrighth#-frontrighth#))/2.0

rem Update tank model
h#=get ground height(1,x#-mx#,z#-mz#)
trackh#=(frontlefth#+frontrighth#+backlefth#+backrighth#)/4.0
if trackh#>h# then h#=trackh#
position object 1,x#,h#,z#
yrotate object 1,a#
rotate object 2,wrapvalue(length#/4.0),0,wrapvalue(across#/4.0)

Hopefully that will come out right when I post this. But anyway, there it is, if you can make sense out of it.

Posted: 14th Dec 2002 20:45
What I make of it is that the first section finds the coordinates of all 4 corners of the tank by using newxvalue and newzvalue commands. Then in finds the height of all the corners (im not sure what the variable mx# does so if you could find out it would probably help) by using get ground height. Then it calculates the difference on both sides of the tank for both the front and the back to find out how the tank will tip sideways, it averages the front and back by adding then and dividing by 2 so that there is only 1 figure that it used to tip the tank (or else the tank would have to bend in the middle). It then repeats this for the backwards and forwards tip in exactly the same way. Next it finds the ground height of the middle of the tank so it can be placed at around the right height on the matrix. It then finds the average height of all the four corners and if it is higher than the middle of the tank then the tank is raised to that height (so that if the tank is in a groove in the terrain then the tracks wont go through the matrix). Then it positions and rotates the tank around the y-axis and rotates it useing the values it found earlier(length# and across#) and divides them by four to make the tipping less (notice how they didnt use sine and cosine, so they dont get exact angles, just estimates that arent scaled properly).
Hope I helped.

Posted: 15th Dec 2002 01:43
Hmm, yes, that was the big problem I had looking at the code, was that I couldnt quite figure out where each variable was coming from. I havent the faintest clue what mx# does, even after staring at the code for 20 minutes. Like I said, it's all gibberishy

Thanx for the help though... having it explained out makes it pretty easy to apply that technique to my own program.

Posted: 15th Dec 2002 02:36
it may be just a temporary variable

Watch the bouncing cursor - now in 3d
Posted: 15th Dec 2002 15:31

http://www.lethalfrog.com/DarkBasicPro/

There is a function called AlignVehicle, this will do what you want.

Posted: 16th Dec 2002 06:04
Pretty nice coding!

I also happened to find your "under-the-sea" terrain demo, which is also quite impressive ... how did you manage to simulate the moving reflection of the sun over the bottom of the sea? Can it be done with DBPro?
Posted: 18th Dec 2002 01:56
Lethalfrog?