Am I missing something?!

Blitz3D Forums/Blitz3D Programming/Am I missing something?!

Grrr, I don't know how long I've been working at this (it's become a bit of a hack job), but it's starting to drive me nuts.

I have two functions, one is an intelligent sight while the other displays a radar:

Function sight(entity,camera,range#=3000)
	
	TFormVector 0,0,range,entity,0
	x#=EntityX(entity)
	y#=EntityY(entity)
	z#=EntityZ(entity)
	pkd=LinePick(x,y,z,TFormedX#(),TFormedY#(),TFormedZ#())
	dist=Sqr((PickedX#()-x)^2+(PickedY#()-y)^2+(PickedZ#()-z)^2)

	CameraProject camera,PickedX#(),PickedY#(),PickedZ#()
	sightx#=ProjectedX()
	sighty#=ProjectedY()

	If (sightx<>0 Or sighty<>0) And pkd<>0
	
		DrawImage sight1,sightx-ImageWidth(sight1)/2,sighty-ImageHeight(sight1)/2
		SetFont def_font
		Color 0,255,0
		Text sightx,sighty+36,dist,True,True
		Color 0,0,0

	EndIf

End Function

Function radar(entity)

	
	DrawImage radar,0,768-ImageHeight(radar)
		
	xb#=ImageWidth(radar)/2-2.5
	yb#=768-ImageHeight(radar)/2-2.5
	
	Color 0,255,0	
	For v.vehicle=Each vehicle
		If EntityDistance(entity,v\model)<=66 And v\active=False			 
			TFormPoint EntityX(v\model),EntityY(v\model),EntityZ(v\model),0,entity
						
			Oval xb+TFormedX(),yb-TFormedZ(),5,5,1	
		EndIf
	Next
	Color 0,0,0
	
End Function


They work fine, until I board one of the vehicles. In the sight function, the sight dissapears at (range/# of vehicles) units. In the radar function, the coordinates of the blips representing each vehicle are equivelent to (coordinate^# of vehicles). In addition, I don't see why the cutoff distance for drawing the blips is 66, each quadrant of the radar is 100x100 pixels.

I hope you can help me with just this information, the full vehicle system is quite long.

I haven't looked at your code, but the common error is a variable that supposed to be a floating point, but is never declared one. Or, a variable that is declared as a floating point, but the calculation contains integers.

Looking at your code, i don't see anything that would suggest that, in your two functions anyway. Is there a chance the problem could lie outside these functions? Can you post a working example?

On the first function you don't have code to determine what to do when the line of sight does not pick something. Will the pick always return something? If not, remember that pickedx,y,z will return the last successful pick.

Also, if the entity which you pass to the function is a child then make sure x, y, z refer to the global coords rather than local.

In the second function you are disregarding anything which is further than 66 units away which is why the cutoff is >66. Is there something I'm missing?

Other than that you'll need to post the code as it's difficult to see what the issue is.

Shouldn't it be:
pkd=LinePick(x,y,z,TFormedX#()-x,TFormedY#()-y,TFormedZ#()-z)

And later on:
onscreen = CameraProject(camera,PickedX#(),PickedY#(),PickedZ#())
	sightx#=ProjectedX()
	sighty#=ProjectedY()

	If onscreen
And I think you should change 'dst' to a float.


Shouldn't it be:

pkd=LinePick(x,y,z,TFormedX#()-x,TFormedY#()-y,TFormedZ#()-z)



Nope.


And later on:

onscreen = CameraProject(camera,PickedX#(),PickedY#(),PickedZ#())
sightx#=ProjectedX()
sighty#=ProjectedY()

If onscreen

And I think you should change 'dst' to a float.



Eh? Cameraproject does not return anything. If the entity is offscreen then the coords are 0,0.

Please wait for an update: there may be a major flaw in my code which is confusing distance calculations. This could take a while to fix.

Ah, TFormVector .. I thought it said TFormPoint. I thought CameraProject should return true or false, from the manual:

Projects the world coordinates x,y,z on to the 2D screen. Returns true if the coordinates are on-screen.



Well, I've ripped apart my code to make a working demo of these two functions with the vehicle system. It may still have some unnecessary code fragments, but I'm about to leave on a vacation and I'm not certain if I'll have good internet access (my ethernet connecter is broken so I have to use wi-fi). It's still VERY long.

Graphics3D 1024,768,32,1

Global mode=1
Global visibility=3000
Global counter

Color 0,255,0

Global sight=CreateImage(44,44)
	SetBuffer ImageBuffer(sight)
		Oval 21,21,2,2,0
		Oval 18,18,8,8,0

Global radar=CreateImage(200,200)
	SetBuffer ImageBuffer(radar)
		Color 0,50,0
		Oval 0,0,200,200
		For a=-1 To 1
			Color 0,255,0
			Line 100+a,0,100+a,200
			Line 0,100+a,200,100+a
			Color 0,255,100
		Next
		Oval 75,75,50,50,0
		Oval 50,50,100,100,0
		Oval 25,25,150,150,0

Global player
Global cam
Global terrain

Global type_player=1
Global type_terrain=2

Dim box(200)

Type vehicle
	
	Field model
	Field active
	Field speed#
	Field opmode
	Field prompt$

End Type




SetBuffer BackBuffer()



light=CreateLight()
	LightRange light,10000
	PositionEntity light,0,100,0


Global def_font=LoadFont("arial")
Global main_font=LoadFont("arial",24,True)

		
Collisions type_player,type_player,2,2	
Collisions type_player,type_terrain,2,3


setup()
Color 0,0,0

While Not KeyDown(1)

	If mode=1
		loop1(1)
	ElseIf mode=2
		loop2(1)
	ElseIf mode=3
		loop3(1)
	EndIf

UpdateWorld

	If mode=1
		loop1(2)
	ElseIf mode=2
		loop2(2)
	ElseIf mode=3
		loop3(2)
	EndIf

RenderWorld

If mode=1 loop1(3)
If mode=2 loop2(3)
If mode=3 loop3(3)

Flip

Wend	

;---------------------------------------------------------------------------------------------------

ClearWorld


Cls

End

Function setup()
	
	player=CreateCone()
	PositionEntity player,0,2,0
	EntityRadius player,1
	EntityType player,type_player	


	cam=CreateCamera()
	ScaleEntity cam,.13,.13,.1
	CameraRange cam,1,visibility
	CameraFogMode cam,1
	CameraFogColor cam,125,125,250
	CameraFogRange cam,1,visibility
	CameraClsColor cam,150,150,255
	PositionEntity cam,EntityX#(player),EntityY#(player),EntityZ#(player)
	RotateEntity cam,EntityPitch#(player),EntityYaw#(player),EntityRoll#(player)

	ufo.vehicle=New vehicle
		ufo\model=CreateSphere()
		ScaleEntity ufo\model,2,2,2
		PositionEntity ufo\model,-25,2,10
		EntityType ufo\model,type_player
		EntityRadius ufo\model,2
		EntityPickMode ufo\model,2
		ufo\opmode=2
		ufo\prompt="Press Enter to fly this thing."
	
	car.vehicle=New vehicle
		car\model=CreateCube()
		ScaleEntity car\model,3,3,3
		PositionEntity car\model,0,5,10
		EntityType car\model,type_player
		EntityRadius car\model,3
		EntityPickMode car\model,2
		car\opmode=3
		car\prompt="Press Enter to drive this thing."

	terrain=CreatePlane()
	EntityColor terrain,0,200,0
	EntityType terrain,type_terrain
	EntityPickMode terrain,2
	
	For a=1 To 200
		box(a)=CreateCube()
		EntityColor box(a),50,50,50
		PositionEntity box(a),Rand(-300,300),0,Rand(-300,300)
		ScaleEntity box(a),Rand(1,10),Rand(1,10),Rand(1,10)
		EntityType box(a),type_terrain
		EntityPickMode box(a),2
	Next

End Function



Function loop1(id)
	
	
	Select id
		
		Case 1
		
			ShowEntity player
			
			If KeyDown(200) MoveEntity player,0,0,0.2
			If KeyDown(208) MoveEntity player,0,0,-0.2
			If KeyDown(203) TurnEntity player,0,3,0
			If KeyDown(205) TurnEntity player,0,-3,0

			
			TranslateEntity player,0,-0.4,0
			
			
			For v.vehicle=Each vehicle
				TranslateEntity v\model,0,-.4,0		
			Next
		
			;------------------------------------------------------------------
		
		Case 2


			PositionEntity cam,EntityX#(player),EntityY#(player),EntityZ#(player)
			RotateEntity cam,pitch1#,EntityYaw#(player),EntityRoll#(player)
			MoveEntity cam,0,1.5,-4

		Case 3
				hud(player)

				SetFont(main_font)
				For vhcle.vehicle=Each vehicle
					If EntityDistance(player,vhcle\model)<=5
						Text 726,0,vhcle\prompt,False,False
						If KeyHit(28)
							mode=vhcle\opmode
							vhcle\active=True
							
							counter=0
						EndIf	
					EndIf
				Next
				FlushKeys()
			

	End Select
End Function


Function loop2(id)
For craft.vehicle=Each vehicle
If craft\opmode=2 And craft\active=True

	Select id
		
		Case 1
			
			If KeyHit(28)
				mode=1
				craft\active=False
				craft\speed=0
				PositionEntity player,EntityX(craft\model),EntityY(craft\model)+1.5,EntityZ(craft\model)
				RotateEntity player,0,EntityYaw(craft\model),0
			EndIf
			
			
			HideEntity player

				
			If KeyDown(200) TurnEntity craft\model,5,0,0
			If KeyDown(208) TurnEntity craft\model,-5,0,0
			If KeyDown(203) TurnEntity craft\model,0,0,5
			If KeyDown(205) TurnEntity craft\model,0,0,-5
			If KeyDown(201) craft\speed=craft\speed+.06
			If KeyDown(209) craft\speed=craft\speed-.06
			
			MoveEntity craft\model,0,0,craft\speed
			;------------------------------------------------------------------
	
		Case 2

			PositionEntity cam,EntityX#(craft\model),EntityY#(craft\model),EntityZ#(craft\model)
			RotateEntity cam,EntityPitch#(craft\model),EntityYaw#(craft\model),EntityRoll#(craft\model)
			MoveEntity cam,0,2.5,-12

		Case 3
			hud(craft\model)
	End Select	

Else

	TranslateEntity craft\model,0,-0.4,0
	
EndIf
Next

End Function



Function loop3(id)
For car.vehicle=Each vehicle
If car\opmode=3 And car\active=True
	Select id
	
		Case 1

			If KeyHit(28)
				mode=1
				car\active=False
				car\speed=0
				PositionEntity player,EntityX(car\model),EntityY(car\model)+5,EntityZ(car\model)
				RotateEntity player,0,EntityYaw(car\model),0
			EndIf
			
			HideEntity player
			
			
			If KeyDown(200) MoveEntity car\model,0,0,.7
			If KeyDown(208) MoveEntity car\model,0,0,-.7
			If KeyDown(203) TurnEntity car\model,0,2,0
			If KeyDown(205) TurnEntity car\model,0,-2,0
			
			TranslateEntity car\model,0,-.4,0	
			;------------------------------------------------------------------
			
		Case 2

			PositionEntity cam,EntityX#(car\model),EntityY#(car\model),EntityZ#(car\model)
			RotateEntity cam,EntityPitch#(car\model),EntityYaw#(car\model),EntityRoll#(car\model)
			MoveEntity cam,0,5,-12

		Case 3
			hud(car\model)
	End Select

Else		

	TranslateEntity car\model,0,-0.4,0

EndIf
Next	
End Function

Function hud(entity)

		sight(entity,cam,visibility)
		radar(entity)
	
End Function

Function sight(entity,camera,range#=3000)
	
	TFormVector 0,0,range,entity,0
	x#=EntityX(entity)
	y#=EntityY(entity)
	z#=EntityZ(entity)
	pkd=LinePick(x,y,z,TFormedX#(),TFormedY#(),TFormedZ#())
	dist=Sqr((PickedX#()-x)^2+(PickedY#()-y)^2+(PickedZ#()-z)^2)

	CameraProject camera,PickedX#(),PickedY#(),PickedZ#()
	sightx#=ProjectedX()
	sighty#=ProjectedY()

	If (sightx<>0 Or sighty<>0) And pkd<>0
	
		DrawImage sight,sightx-ImageWidth(sight)/2,sighty-ImageHeight(sight)/2
		SetFont def_font
		Color 0,255,0
		Text sightx,sighty+12,dist,True,True

	EndIf

	Color 0,0,0

End Function

Function radar(entity)

	
	DrawImage radar,0,768-ImageHeight(radar)
		
	xb#=ImageWidth(radar)/2-2.5
	yb#=768-ImageHeight(radar)/2-2.5
	
	Color 0,255,0
	For v.vehicle=Each vehicle
		If EntityDistance(entity,v\model)<=100 And v\active=False			 
			TFormPoint EntityX(v\model),EntityY(v\model),EntityZ(v\model),0,entity
						
			Oval xb+TFormedX(),yb-TFormedZ(),5,5
		EndIf
	Next
	
	Color 0,0,0
	
End Function


Some code I ripped out fixed the sight, but the radar has an unusual problem: when you "walk" around, it workes fine. In the sphere shaped aircraft, distances are shown as half of what they're supposed to be, while in the cube they are 1/3 their correct distance. This looks a lot like the "mode" is somehow interfering.

On my way back, any suggestions?

Yes. The tform commands "correctly" take into consideration the entity scale. The cone works fine as you don't scale it. If you use scalemesh instead of scaleentity this works fine. The only alternative is to store the axis scales and apply them to the tformed results.

Just bringing this to your attention, but if you want to put a blip on the radar where you are, after you are parented to something else, use the global thing on the EntityX,Y,Z's

EntityX(omgmodel,1)


Thanks Stevie, that solved everything! Maybe I should stop using ScaleEntity to scale meshes :)