Door Open

Blitz3D Forums/Blitz3D Beginners Area/Door Open

Hey Rook, try this out. Use the spacebar to open and shut the door. I've commented it, so hopefully it will be of some use to you :)

Global wall_left=CreateCube()
ScaleEntity wall_left,4,4,0.1
PositionEntity wall_left,-6,0,0

Global wall_right=CreateCube()
ScaleEntity wall_right,4,4,0.1
PositionEntity wall_right,6,0,0

Global roof_bar=CreateCube()
ScaleEntity roof_bar,2,1,0.1
PositionEntity roof_bar,0,3,0

Global hinge=CreatePivot()
PositionEntity hinge,-2,0,0

Global door=CreateCube(hinge)
ScaleEntity door,2,3,0.2
PositionEntity door,2,-1,0
EntityColor door,128,0,0

Global door_handle=CreateSphere(8,hinge)
PositionEntity door_handle,3.5,-1,-0.3
ScaleEntity door_handle,0.2,0.2,0.2

;********* end of create scene ************

Global door_open=0; 0 = door not moving, 1 = door is shutting, 2 = door is opening
Global door_status=0

While Not KeyHit(1)

	If KeyHit(57) Then; if the space bar pressed
		If door_status=0 Then; if the door is SHUT then
			door_open=1; set the door is opening
		ElseIf door_status=1 Then; if the door is OPEN then
			door_open=2; set the door to shuting
		End If
	End If

	If door_open=1 Then; if the door is opening
		open_door(); open it
	ElseIf door_open=2 Then; if the door is shutting
		close_door(); shut it
	End If
	UpdateWorld
	RenderWorld
	Flip
Wend
End

Function open_door()
	RotateEntity hinge,EntityPitch(hinge),EntityYaw(hinge)+0.4,EntityRoll(hinge); increase the hinge rotation
	If EntityYaw(hinge)>93 Then; if the door angle is greater than 93, then the door is open
		door_status=1; set door status to 1 (open)
		door_open=0; set door to NOT moving
	End If
End Function

Function close_door()
	RotateEntity hinge,EntityPitch(hinge),EntityYaw(hinge)-0.4,EntityRoll(hinge); decrease the hinge rotation
	If EntityYaw(hinge)=<0 Then; if door angle is 0 or less, then the door is shut
		door_status=0; set door status to 0 (shut)
		door_open=0; set door to NOT moving
	End If
End Function