Align Entity to Normal

Blitz3D Forums/Blitz3D Programming/Align Entity to Normal

boomboom(Posted 2006) [#1]
Hi, I want to align an entity to a face's normal.

For example, I have a terrain grid with some randomly placed bushes on it. I need to align the bush so that its correctly on the same angle as the polygon underneth it.

If I have said it in a really confusing way then heres a beautiful picture:



ATM they are all like bush A. I want them like bush B

My initial plan is to do a TurnEntity onto the face normal of the polygon under the bush (on the terrain object) Any ideas how to go about getting that data and use it correctly?


boomboom(Posted 2006) [#2]
oh, and I am not actually using a terrain object. Just a Mesh for the floor (which hopefully should make it easyer)


Ross C(Posted 2006) [#3]
Do a linepick down from the entity's location. Then, align to vector using the normals of the linepick. PickedNX() etc


puki(Posted 2006) [#4]
Mmm, I wonder if that will work they way he wants.


Ross C(Posted 2006) [#5]
This should do the trick. You MUST give your terrain mesh a pickmode. Make sure it's set to polygon detection :o)

;linepick example by RossC

Graphics3D 800,600
SetBuffer BackBuffer()


Global camera = CreateCamera()
Global light = CreateLight()

Global player = CreateCone()
EntityType player,1
EntityRadius player,1
EntityColor player,50,70,255
PositionEntity player,0,0.5,0

Global cam_piv = CreatePivot(player)
PositionEntity cam_piv,0,2,-8

Global tex=CreateTexture(64,64,1+8)
SetBuffer TextureBuffer(tex)
For loop=0 To 3000
	Color Rnd(190,255),Rnd(190,255),Rnd(190,255)
	Rect Int(Rnd(0,63)),Int(Rnd(0,63)),1,1
Next
Color 255,255,255

SetBuffer BackBuffer()

Global plane=CreatePlane()
EntityType plane,2
EntityColor plane,200,100,120
EntityTexture plane,tex
EntityPickMode plane,2


Global sphere=CreateSphere(10)
ScaleEntity sphere,10,3,10
EntityPickMode sphere,2
EntityType sphere,2
PositionEntity sphere,-2,0,19

Collisions 1,2,2,2; set up collisions between the ground and the player

Global jump_force#
Global jump_flag=0; set jump flag to 'no jump'
Global gravity#=0.9; set gravity value

While Not KeyHit(1)

	; this part is the linepick. The tform part, is to make sure the linepick is done from the players
	; actual rotation, straigh down.
	TFormVector 0,-10,0,player,0
	; the values from the tform then get used in the linepick command. if you just substitute your
	; variable for the player into here, everything will be cool!
	LinePick(EntityX(player,True),EntityY(player,True),EntityZ(player,True),TFormedX(),TFormedY(),TFormedZ()); do a linepick straight down form the players location
	; aligns the player to the scenery, whatever has has a pickmode set.
	AlignToVector player,PickedNX(),PickedNY(),PickedNZ(),2,0.1
	
	If KeyDown(200) Then MoveEntity player,0,0,0.1
	If KeyDown(208) Then MoveEntity player,0,0,-0.1
	If KeyDown(203) Then TurnEntity player,0,1,0
	If KeyDown(205) Then TurnEntity player,0,-1,0	
	
	update_camera()
	
	UpdateWorld
	RenderWorld
	Text 0,0," Press Space to jump. Arrow keys to move!"
	;DebugLog(" pickedy="+PickedY())
	Flip
Wend
End

Function update_camera()
	PointEntity camera,cam_piv
	MoveEntity camera,0,0,EntityDistance#(camera,cam_piv)/40.0
	PointEntity camera,player
End Function



t3K|Mac(Posted 2006) [#6]
plants normally align to the sun, not to the underground ;)


puki(Posted 2006) [#7]
The back of my hand will be aligning to the back of your head if we have any more of your cheek.


boomboom(Posted 2006) [#8]
Thanks man, I had a little play, I will disect it and apply it later on :)