How to align an entity(not a car) to terrain?
Blitz3D Forums/Blitz3D Programming/How to align an entity(not a car) to terrain?| 
 | ||
| Hi guys, this probably been answered before somewhere or my approach is just way off, but I can't seem to get it right. I have a terrain with hills/mountains and want to align some free roaming enemy entities to it, but not completely upright, just a bit inclined depending on the terrain steepness so that it looks more natural. Unlike so:  I thought to do a basic align(straight upright) first without any adjustments like this but apparently that's not the way: Any help is greately appreciated. | 
| 
 | ||
| A couple of things are wrong with this function ... 1. PickedNx, Ny etc.. must have open/closed brackets to return the correct values. 2. By default NX, NY and NZ will be integers, they should be explicitly defined as floats using # as the values are between -1 and 1. You could change the function to this. The InterRate is the rate of interpolation between a completely upright vector and the normal vector of the landscape. Change this rate to whatever suits between 0.0 and 1.0. The AlignRate is one of the parameters of AlignToVector, values < 1.0 allow you to align the entity gradually rather than snapping it to the vector. 
Function AlignToTerrain(entity, InterRate# = 0.5, AlignRate# = 1.0 )
	Local Pick, Nx#, Ny#, Nz#
    Pick = LinePick (EntityX(entity),EntityY(entity),EntityZ(entity),0,-2,0)
    If Pick <> 0 Then
	
		;Straight up normal = 0,1,0
		Nx = PickedNX() * InterRate
		Ny = 1.0 + ( PickedNY() - 1.0 ) * InterRate
		Nz = PickedNZ() * InterRate
		
		AlignToVector entity,Nx,Ny,Nz,2, AlignRate
		
    EndIf	
	
End Function
You might find that getting and average of a few different positions to get the terrain normal may be better (e.g. check infront and behind as well as current position). Stevie | 
| 
 | ||
| Many, many thanks Stevie G. At first it didn't work still, but when I changed the InterRate value to 1.0 it worked great! | 
| 
 | ||
| Maybe I misunderstood you but if you're going to use an interrate of 1.0 then the new function is pointless, just make the following changes to your original: NX#=PickedNX() NY#=PickedNY() NZ#=PickedNZ() | 
| 
 | ||
| No it's alright for this particular model(a monster), but for others might be useful.. | 
| 
 | ||
| What Stevie G explains here, (defining if the variable is an integer or a float or a string) is really important to keep in mind if you want to avoid errors. I once spent hours to try to debug a code, and the error was simply that i had forgotten to define the variable as a float, so by default it was an integer. | 
| 
 | ||
| Uhuh, indeed was a very silly slip-up(probably more to come). Was hammering my head over why-oh-why it didn't work. |