nearest cube

Blitz3D Forums/Blitz3D Programming/nearest cube

cash(Posted 2008) [#1]
I have studied the forums for some simple code for this but cannot really find a good example for what I need. Theres some pretty clever stuff there but maybe too much for my simple requirements.

I am NOT looking to do waypoint code so dont need anything too scaleable.

Suppose I have six cubes placed around my level.

When a trigger action occurs I want to move my player to the nearest cube. Its as simple as that. Only needs to calculate on X and Z axis. Y is not important.

Any examples would be great. Thanks in advance.


big10p(Posted 2008) [#2]
Just do an EntityDistance on each cube and choose the nearest.

P.S. There are quicker ways of doing this, but as it's only 6 cubes, it's not really worth bothering with.


cash(Posted 2008) [#3]
Any ideas on the easiest way to choose the nearest. I assume theres some maths I can use.


big10p(Posted 2008) [#4]
No maths needed. Just have a variable that holds the nearest cube found so far, then check each cube's distance, in turn. If it's less than the current nearest, make it the nearest one found.


markcw(Posted 2008) [#5]
What about something like this? Press keys 1 to 6.

Type CUBE
 Field ent,x#,y#,z#
End Type

;setup scene
Graphics3D 640,480,0,2
SetBuffer BackBuffer()
camera=CreateCamera()
PositionEntity camera,0,20,0
light=CreateLight()
RotateEntity light,90,0,0

For id=1 To 7
 temp.CUBE=New CUBE
 temp\ent=CreateCube()
 temp\x#=Rand(-20,20)
 temp\z#=Rand(-20,20)
 PositionEntity temp\ent,temp\x#,EntityY(temp\ent),temp\z#
Next

player.CUBE=Last cube
player\x#=0
player\z#=0
PositionEntity player\ent,player\x#,EntityY(player\ent),player\z#

;main
While Not KeyDown(1)

 do=0
 If KeyDown(2) Then do=1 
 If KeyDown(3) Then do=2
 If KeyDown(4) Then do=3
 If KeyDown(5) Then do=4
 If KeyDown(6) Then do=5
 If KeyDown(7) Then do=6

 PointEntity camera,player\ent

 If do=1
  temp.CUBE=First cube
  PointEntity player\ent,temp\ent
  MoveEntity player\ent,0,0,0.1
 ElseIf do=2
  temp.CUBE=First cube
  temp.CUBE=After temp
  PointEntity player\ent,temp\ent
  MoveEntity player\ent,0,0,0.1
 ElseIf do=3
  temp.CUBE=First cube
  For id=1 To 2 : temp.CUBE=After temp : Next
  PointEntity player\ent,temp\ent
  MoveEntity player\ent,0,0,0.1
 ElseIf do=4
  temp.CUBE=First cube
  For id=1 To 3 : temp.CUBE=After temp : Next
  PointEntity player\ent,temp\ent
  MoveEntity player\ent,0,0,0.1
 ElseIf do=5
  temp.CUBE=First cube
  For id=1 To 4 : temp.CUBE=After temp : Next
  PointEntity player\ent,temp\ent
  MoveEntity player\ent,0,0,0.1
 ElseIf do=6
  temp.CUBE=First cube
  For id=1 To 5 : temp.CUBE=After temp : Next
  PointEntity player\ent,temp\ent
  MoveEntity player\ent,0,0,0.1
 EndIf

 RenderWorld

 Flip
Wend



cash(Posted 2008) [#6]
guys thanks for the help. I think I should be able to sort it now.