noobs & PointEntity

BlitzMax Modules Forums/MiniB3D/noobs & PointEntity

The forum is locked. These are the original blitzbasic.com forums, kept so they can still be read. No new topics, replies or edits can be made here. To join in, head over to the Blitz3D+ forums.

In this code you never call a CameraPick, so EntityPickata is always Null.

Maybe this code helps you (its a bit buggy due to fast production) ;)

Import sidesign.minib3d

Type TPlanet
	Global PlanetList:TList = New TList
	Global ChoosenPlanet:TPlanet = Null
	Global Flyto:Byte = False
	
	Field Mesh:TMesh
	Field Pivot:TPivot
	Field Radius
	
	Method New()
		PlanetList.Addlast(Self)
	End Method
	
	Function Create:TPlanet(X:Int,Y:Int,Z:Int,Radius:Float)
		Local P:TPlanet = New TPlanet
		P.Mesh = CreateSphere()
		P.Pivot = CreatePivot(P.Mesh)
		ScaleEntity P.Mesh,Radius,Radius,Radius
		PositionEntity P.Mesh,X,Y,Z
		EntityColor P.Mesh,Rnd(255),Rnd(255),Rnd(255)
		EntityPickMode P.Mesh,2
		P.Radius = Radius
		Return P
	End Function
	
	Function IsPicked:TPlanet(Camera:TCamera)
		CameraPick Camera,MouseX(),MouseY()
		Local ent:TMesh = TMesh(PickedEntity())
		If ent <> Null Then
			For Local P:TPlanet = EachIn PlanetList
				If P.Mesh = ent Then
					ChoosenPlanet = P
					If Flyto = False Then Flyto = True
				End If
			Next
		End If
	End Function 
	
	Method MoveTo(Cam:TCamera)
		smoothcam(cam,pivot,mesh,400)
		MoveEntity Cam ,.2,0.0,0.0
	End Method
	
	Function Update(Cam:TCamera)
		If ChoosenPlanet <> Null Then
			smoothcam(cam,choosenplanet.pivot,choosenplanet.mesh,400)
			If EntityDistance(Cam,ChoosenPlanet.Mesh) < ChoosenPlanet.Radius*2 Then 
						FlyTo = False
						ChoosenPlanet = Null
			EndIf
			If Flyto = True Then ChoosenPlanet.MoveTo(Cam)
		End If
	End Function
	
End Type

Function smoothcam(cam:TCamera,pivot:TEntity,target:TEntity,camspeed:Float = 5.0)

Local curx#=EntityX(cam)
Local curz#=EntityZ(cam)
Local destx#=EntityX(pivot,True)
Local destz#=EntityZ(pivot,True)

curx#=curx#+((destx#-curx#)/camspeed)
curz#=curz#+((destz#-curz#)/camspeed)
Local cury:Float = EntityY(pivot, True)

PositionEntity cam,curx#,cury#,curz#

PointEntity cam,target
End Function




Graphics3D 800,600,32,2

Local Cam:TCamera = CreateCamera()
CameraRange Cam,1.0,5000
Local Pivot = CreatePivot()
PositionEntity Pivot,0,0,4
EntityParent Pivot,Cam

PositionEntity cam,0,0,-1500

For Local X = -1000 To 1000 Step 300
	TPlanet.Create(X,0,0,Rnd(10,190))
Next

Local mxs#=0
Local mys#=0
Local move#=20.5
MouseXSpeed() ' flush
MouseYSpeed() ' flush


While Not KeyHit(KEY_ESCAPE)

	If MouseDown(2) Then 
			mxs#=mxs#+(MouseXSpeed()/5.0)
			mys#=mys#+(MouseYSpeed()/5.0)
		
			RotateEntity cam , mys# , - mxs# , 0
			
			MoveMouse 400,300
			MouseXSpeed() ' flush
			MouseYSpeed() ' flush
	End If
		
	If KeyDown(KEY_UP)=True Then 
			MoveEntity cam,0,0,move# ' move camera forward
			TPlanet.FlyTo = False
			TPlanet.ChoosenPlanet = Null
	EndIf
	
	If KeyDown(KEY_DOWN)=True Then 
			MoveEntity cam,0,0,-move# ' move camera back
					TPlanet.FlyTo = False
			TPlanet.ChoosenPlanet = Null
	EndIf
	

	If KeyDown(KEY_LEFT)=True Then 
			MoveEntity cam,-move#,0,0 ' move camera left
					TPlanet.FlyTo = False
			TPlanet.ChoosenPlanet = Null
	EndIf
	
	If KeyDown(KEY_RIGHT) = True Then 
			MoveEntity cam , move# , 0 , 0 ' move camera right
					TPlanet.FlyTo = False
			TPlanet.ChoosenPlanet = Null
	EndIf
	
	
	

	If MouseHit(1) Then TPlanet.IsPicked(Cam)
	TPlanet.Update(Cam)
	RenderWorld
	
	Flip
Wend

Function MouseXSpeed()
		Global oldmx
		Local mxs=MouseX()-oldmx
		oldmx=MouseX()
		Return mxs
	End Function
	
	Function MouseYSpeed()
		Global oldmy
		Local mys=MouseY()-oldmy
		oldmy=MouseY()
		Return mys
	End Function
	
	Function MouseZSpeed()	
		Global oldmz
		Local mzs=MouseZ()-oldmz
		oldmz = MouseZ()
		Return mzs
	End Function