
SuperStrict

'Import hitoro.newtonb3d

' TEMP:
Import "hitoro.mod/newtonb3d.mod/newtonb3d.bmx"

Import "hitoro.mod/newtonb3d.mod/examples/helpers_general.bmx"
Import "hitoro.mod/newtonb3d.mod/examples/helpers_newton.bmx"
Import "hitoro.mod/newtonb3d.mod/examples/helpers_b3d.bmx"

Graphics3D 1024, 768, 0, 60

Local update:Int = 30

SeedRnd MilliSecs ()

Local world:TNWorld = CreateNewtonWorld ()

Local cam:TCamera = CreateCamera ()

	CameraClsColor cam, 32, 64, 128
	MoveEntity cam, 0.0, 10.0, -25.0

AmbientLight 255.0, 255.0, 255.0

Local sun:TLight = CreateLight ()

	LightRange sun, 2000.0
	MoveEntity sun, -1000.0, 1000.0, -500.0

Local ground:TNewtonGround = CreateNewtonGround (world, 100.0, 1.0, 100.0)

	EntityColor ground.mesh, 0, 64, 0

PointEntity sun, ground.mesh

Local tcube:TMesh = CreateCube ()

	TurnEntity tcube, 1.0, 2.0, 3.0 ' Start at wonky angle so we get visible results!

'Print EntityPitch (tcube)
'Print EntityYaw (tcube)
'Print EntityRoll (tcube)

Local cube:TNewtonCube = CreateNewtonCubeMesh (world, tcube, 1.0, False)

	cube.ToggleShadow True

ResetNewtonWorld world

Local checker:TMesh = CreateCube ()

	ScaleMesh checker, 1.2, 1.2, 1.2
	EntityColor checker, 128, 0, 0
	EntityAlpha checker, 0.5

Repeat

	If KeyDown (KEY_LEFT) Then TurnEntity cam, 0.0, 1.0, 0.0, True
	If KeyDown (KEY_RIGHT) Then TurnEntity cam, 0.0, -1.0, 0.0, True
	
	If KeyDown (KEY_UP) Then TurnEntity cam, 1.0, 0.0, 0.0
	If KeyDown (KEY_DOWN) Then TurnEntity cam, -1.0, 0.0, 0.0

	If KeyDown (KEY_A) Then MoveEntity cam, 0.0, 0.0, 0.1 + KeyDown (KEY_LSHIFT) * 0.5
	If KeyDown (KEY_Z) Then MoveEntity cam, 0.0, 0.0, -(0.1 + KeyDown (KEY_LSHIFT) * 0.5)

	If KeyDown (KEY_SPACE)
		EntityAlpha checker, 0.5
	Else
		EntityAlpha checker, 0.0
	EndIf
	
	PointEntity cam, cube.mesh
		
	UpdateNewtonWorld world, update

	PositionEntity checker, EntityX (cube.mesh), EntityY (cube.mesh), EntityZ (cube.mesh)
	RotateEntity checker, EntityPitch (cube.mesh), EntityYaw (cube.mesh), EntityRoll (cube.mesh)

	RenderWorld
	
	BeginMax2D
	
		ShadowText "Hold SPACE to show a cube manually-aligned from", 20, 20
		ShadowText "white Newton cube's EntityX/EntityPitch, etc...", 20, 40
	
		ShadowText "EntityX: " + EntityX (cube.mesh), 20, 80
		ShadowText "EntityY: " + EntityY (cube.mesh), 20, 100
		ShadowText "EntityZ: " + EntityZ (cube.mesh), 20, 120

		ShadowText "EntityPitch: " + EntityPitch (cube.mesh), 20, 160
		ShadowText "EntityYaw: " + EntityYaw (cube.mesh), 20, 180
		ShadowText "EntityRoll: " + EntityRoll (cube.mesh), 20, 200

	EndMax2D
	
	Flip
	
Until KeyHit (KEY_ESCAPE) Or AppTerminate ()

End

Function ShadowText:Int (msg:String, x:Float, y:Float)

	Local red:Int, green:Int, blue:Int
	
	GetColor red, green, blue
	
	SetColor 0, 0, 0
	DrawText msg, x + 1, y + 1

	SetColor 255, 255, 255
	DrawText msg, x, y

	SetColor red, green, blue

End Function
