Aligning a shadow to a mesh

Blitz3D Forums/Blitz3D Programming/Aligning a shadow to a mesh

Vorderman(Posted 2007) [#1]
I need to align a shadow sprite below a car mesh onto a track mesh.

Currently I'm using the code below.

"temppivot" is just a temporary b3d pivot.
"mesh" is the car
"shadow" is a b3d sprite for the shadow

I'm performing a vertical linepick straight down from the car onto the track. If it hits the track, the following code is performed.

The problem is that the track mesh is not flat, it has cambers and hills in all directions, and I *must* point the shadow sprite in the same direction as the car (it's a proper car shaped shadow, not a round blob) so I'm rotating it around the Y axis with the line - RotateEntity shadow,0,EntityYaw#(mesh,True),0

It ALMOST works but on hills as the car turns to run across the hill (so the hill is sloping upwards from left to right relative to the car) the shadow does not follow its yaw correctly, always being rotated off by a few degrees.

When the car points straight up or down the hill it works OK.

Can anyone help me with this? There must be a simple solution.

		PositionEntity temppivot,PickedX#(),PickedY#(),PickedZ#(),True
		dist# = EntityDistance#(temppivot,mesh)
		alpha# = 1.0 - ((1.0 / 10.0) * dist#)
		If (alpha# > 1.0) alpha# = 1.0
		If (alpha# < 0.0) alpha# = 0.0
		PositionEntity shadow,PickedX#(),PickedY#(),PickedZ#(),True
		RotateEntity shadow,0,EntityYaw#(mesh,True),0
		AlignToVector shadow,-PickedNX#(),-PickedNY#(),-PickedNZ#(),1,1
		AlignToVector shadow,-PickedNX#(),-PickedNY#(),-PickedNZ#(),3,1
		EntityAlpha shadow,alpha#
		ScaleSprite shadow,((alpha#) * 3.5),((alpha#) * 3.5)
		MoveEntity shadow,0,0,-0.05;-0.025




Stevie G(Posted 2007) [#2]
Once you've rotated the shadow to the same as the mesh just use a single align to vector on the y axis but not on -ve normals.

AlignToVector shadow, PickedNX#(), PickedNY#(),PickedNZ#(),2,1


By using both x and z axis' you're effectively re-doing the rotateentity.


Vorderman(Posted 2007) [#3]
Thanks Stevie, I'll give that a try tonight!


Vorderman(Posted 2007) [#4]
OK, that seems to work, cheers.

It was returning some odd results when the car was upside down, but I just found that I had set the car body to be pickable, no idea why...

Turned that off and it's fine now.


Stevie G(Posted 2007) [#5]
Is this you back on the stunt car game? About time!!


Vorderman(Posted 2007) [#6]
Yep, I've got the programming bug again!

Currently have an 8-car AI race working, and the AI is competent enough that I can't beat them, despite them driving using the same controls and physics the player has.
Watching one opposite-lock his way around the track is oddly hypnotic.

There's something very satisfying about teaching your AI to perform a task, then watching as they do it better than you can.

Just need to finish the front end, which is what put me off ages ago...I hate front ends :(