Phil Jones' pitch and roll example

Blitz3D Forums/Blitz3D Programming/Phil Jones' pitch and roll example

Chad(Posted 2007) [#1]
I'm using his Pitch and Roll example from the code archives and I was wondering, if instead of using a terrain if I could use a different entity?

I would like to use this technique on a plane that I have set up with waves and stuff. I'd like it to roll with the waves and etc and I really like his example.

So, I guess my question is, how can I reconfigure this small bit of code so it works with my plane instead of a terrain?

anglenew# = yaw -90  ; what must be added to yaw to calculate front of vehicle's global x,z co-ords
xnew# = x+Cos(anglenew)*carlength
znew# = z+Sin(anglenew)*carlength
frontheight# = TerrainY(terrain,xnew,y,znew); get the height at new x,y,z
PositionEntity pfront,xnew,frontheight,znew ; a visual marker only - not neccessary for calculations

anglenew# = yaw +90 ; what must be added to yaw to calculate back of vehicle's global x,z co-ords
xnew# = x+Cos(anglenew)*carlength
znew# = z+Sin(anglenew)*carlength
backheight = TerrainY(terrain,xnew,y,znew)
PositionEntity pback,xnew,backheight,znew

anglenew# = yaw  ; what must be added to yaw to calculate midleft of vehicle's global x,z co-ords
xnew# = x+Cos(anglenew)*carwidth
znew# = z+Sin(anglenew)*carwidth
Leftheight = TerrainY(terrain,xnew,y,znew)
PositionEntity pleft,xnew,leftheight,znew

anglenew# = yaw + 180 ; what must be added to yaw to calculate midright of vehicle's global x,z co-ords
xnew# = x+Cos(anglenew)*carwidth
znew# = z+Sin(anglenew)*carwidth
rightheight = TerrainY(terrain,xnew,y,znew)
PositionEntity pright,xnew,rightheight,znew

pitchx# = carlength*2 ; need complete length of car for x
pitchy# = (backheight - frontheight); the height dif between the front and rear of car
pitch = ATan2(pitchx,pitchy); atan2 will return the angle from 0,0 to x,y

rollx# = carwidth*2
rolly# = (rightheight - leftheight) ; the height dif between the left and right of car
roll# = ATan2(rollx,rolly); atan2 will return the angle from 0,0 to x,y

RotateEntity car, pitch-90,yaw,roll-90 ; rotate car with offsets to adjust Atan2's calculation

th=(frontheight+backheight+leftheight+rightheight)/4; get average of terrain heights for cars overall height
PositionEntity car,x,th,z ; adjust car height 
MoveEntity car,0,0,speed

PositionEntity pop,x,th,z ; this is just a central axis 'pole' to help see rotation angles 
RotateEntity pop,pitch-90,yaw,roll-90



Stevie G(Posted 2007) [#2]
Ok, first set the mesh to entitypickmode 2 and do something like this ...

anglenew# = yaw -90  						; what must be added to yaw to calculate front of vehicle's global x,z co-ords
xnew# = x+Cos(anglenew)*carlength
znew# = z+Sin(anglenew)*carlength
LinePick xnew, EntityY( car) + 10 , znew, 0,-10,0
PositionEntity pfront,xnew,PickedY(),znew ; a visual marker only - not neccessary for calculations


You'll have to play with the +-10 thing and it's just the first part so you get the idea. The same could be done with collisions and is probably faster.

Stevie


Vorderman(Posted 2007) [#3]
If your plane is a pickable polygon mesh then you can use a single linepick (much quicker than using lots of linepicks) from the centre of your entity, then obtain the picked normals (use PICKEDNX#(), PICKEDNY#() and PICKEDNZ#() ) then use 2 ALIGNTOVECTOR commands to correctly orient the object.

Like this -
ground = LinePick( EntityX#(v\bodymesh,True),EntityY#(v\bodymesh,True) + 1.0,EntityZ#(v\bodymesh,True) , 0,-2.0,0 , 0.0)
		If ground
			nx# = PickedNX#()
			ny# = PickedNY#()
			nz# = PickedNZ#()
			AlignToVector v\shadowmesh,nx#,ny#,nz#,1,1
			AlignToVector v\shadowmesh,nx#,ny#,nz#,3,1


Note that this will result in you entity pointing straight upwards along the polygon's normal so you will need to orient the entity's mesh correctly in your 3D package for it to look visually correct. If you can't do that, then make your primary object a pivot, use the above code to orient this pivot, and have your visual mesh as a child of the pivot that is correctly pre-rotated so that it faces forwards and not upwards (probably ROTATEENTITY child,90,0,0).


Chad(Posted 2007) [#4]
Thanks guys, Stevie, you said it could be done with collisions? How so? The linepick method really slows down my program and makes it very choppy. I'd really like to try this.

I'm pretty sure that the plane isn't a pickable polygon mesh, so I can't really use your suggestion Vorderman but thanks anyways. I am willing to show some of my code incase it is if your interested in seeing if it is.


Stevie G(Posted 2007) [#5]
Chad, look at the driver demo in the 3d samples folder - this does much the same thing but with collisions.

If this implementation isn't what you want show us the code without the need for external media and I'll see what I can do.

BTW, Vordermans method will work as you can simply set the plane to be pickable. Only issue with one linepick is that it's less accurate on more detailed uneven ground.

Stevie


Vorderman(Posted 2007) [#6]
Only issue with one linepick is that it's less accurate on more detailed uneven ground


That's right, it will be aligning your entity to a single normal rather than taking an average of the 4 wheels, so it will be less accurate if (for example) you put the entity over the top of a small lump or hanging over the edge of an incline.

If the entity is constantly moving or the groundmesh is reasonably smooth then it shouldn't be too much of an issue.

I use a smoothing system on mine - I use the above code to obtain the "target" normal on each program loop, and then smoothly move the actual orientation of the entity towards this target rather than using an instant snap.

I imagine for a boat on water a fairly gentle (ie. slower) smoothing would give a nice effect of mass as it cuts into the water before bobbing upright. For entities over ground you need a quicker smoothing otherwise they dig into the floor or appear to be made of jelly.


Chad(Posted 2007) [#7]
Woah, that's all way over my head.. Maybe I should just use a sin/cosine/tan equation to create what I'm looking for.

Then again, this linepick does sound interesting, so I tried what Stevie suggested, here's an external-less media code of what I'm ussing. It was in the water competition awhile back but a little different since I started toying with it. btw props to fredborg for create code.