parententity + positionentity O_o

Blitz3D Forums/Blitz3D Beginners Area/parententity + positionentity O_o

ingenium(Posted 2007) [#1]
I want a group of cubes (parented to another bigger cube) to move first in a way and, when they reach a certain x value, to invert their direction...
and I want the same thing when they reach a -x value...
Well this code makes weird things so... if you can help me...
Graphics3D 800,600,32,2
SetBuffer BackBuffer()

Global cam = CreateCamera()
PositionEntity cam,0,0,0

Global lux = CreateLight()
PositionEntity lux,0,10,0

Global cube = CreateCube()
PositionEntity cube,0,0,10

Type mov
 Field obj
 Field x#
 Field y#
 Field z#
End Type
Global xinc#=-1.3
Global yinc#= 1
Global direction#=.01

For a = 0 To 19
 mov1.mov = New mov
  mov1\obj = CreateCube()
  EntityColor mov1\obj,120,120,120
  ScaleEntity mov1\obj,.1,.1,.1
  If xinc > 2 Then yinc = yinc - 1 : xinc = -1.3
  mov1\y = yinc
  mov1\x = 0.1 + xinc
  xinc = xinc + 1.0
  mov1\z = -3
  EntityParent mov1\obj,cube
  PositionEntity mov1\obj,mov1\x,mov1\y,mov1\z
Next

While Not KeyDown(1)

If KeyDown(203) Then TurnEntity cube,0,-1,0
If KeyDown(205) Then TurnEntity cube,0,1,0
If KeyDown(200) Then MoveEntity cam,0,0,.1
If KeyDown(208) Then MoveEntity cam,0,0,-.1
MoveEntity cam,0,0,0
TurnEntity cube,0,0,0

For mov1.mov = Each mov
 PositionEntity mov1\obj,mov1\x,mov1\y,mov1\z
 If mov1\x > 3.9 Or mov1\x < -3.9 Then direction =  -direction
 mov1\x = mov1\x + direction
Next

RenderWorld()
UpdateWorld()
Flip
Wend
End



b32(Posted 2007) [#2]
The problem is, that the first cube flips the direction, then a second cube flips it again and so on, resulting in the cubes to go back and forth.
The easiest solution is to replace the line
"If mov1\x > 3.0 Or etc"
with this:
If mov1\x > 3.9 Then direction = -0.01
If mov1\x < -3.9 Then direction = 0.01

Another approach, with a different result, is to make "direction" a field of the "mov" type. That way, each cube has it's own direction.


ingenium(Posted 2007) [#3]
ok... i tried it but... try to make direction higher... like 0.1... and instead of 3.9 and -3.9 write 1.9 and -1.9... look what happens... that's odd... how can I avoid that?


b32(Posted 2007) [#4]
The offset occurs because the new direction is set, before all the cubes have moved.
The way to solve this is to put this before the "For mov1.mov = Each mov" loop:
newdirection# = direction

And when the new direction is set, use:
If mov1\x > 3.9 Then newdirection = -0.01
If mov1\x < -3.9 Then newdirection = 0.01

Finally, after the loop use:
direction = newdirection


ingenium(Posted 2007) [#5]
Doesn't work...

Graphics3D 800,600,32,2
SetBuffer BackBuffer()

Global cam = CreateCamera()
PositionEntity cam,0,0,0

Global lux = CreateLight()
PositionEntity lux,0,10,0

Global cube = CreateCube()
PositionEntity cube,0,0,10

Type mov
 Field obj
 Field x#
 Field y#
 Field z#
End Type
Global xinc#=-1.3
Global yinc#= 1
Global direction#=.1
Global newdirection#

For a = 0 To 19
 mov1.mov = New mov
  mov1\obj = CreateCube()
  EntityColor mov1\obj,120,120,120
  ScaleEntity mov1\obj,.1,.1,.1
  If xinc > 2 Then yinc = yinc - 1 : xinc = -1.3
  mov1\y = yinc
  mov1\x = 0.1 + xinc
  xinc = xinc + 1.0
  mov1\z = -3
  EntityParent mov1\obj,cube
  PositionEntity mov1\obj,mov1\x,mov1\y,mov1\z
Next

While Not KeyDown(1)

If KeyDown(203) Then TurnEntity cube,0,-1,0
If KeyDown(205) Then TurnEntity cube,0,1,0
If KeyDown(200) Then MoveEntity cam,0,0,.1
If KeyDown(208) Then MoveEntity cam,0,0,-.1
MoveEntity cam,0,0,0
TurnEntity cube,0,0,0

newdirection = direction
For mov1.mov = Each mov
 PositionEntity mov1\obj,mov1\x,mov1\y,mov1\z
 If mov1\x > 1.9 Then newdirection = -0.1
 If mov1\x < -1.9 Then newdirection =0.1
 mov1\x = mov1\x + newdirection
Next
direction = newdirection

RenderWorld()
UpdateWorld()
Flip
Wend



ingenium(Posted 2007) [#6]
Aaaahhhhh I understand! Ok thanks... I AM AN IDIOT!


b32(Posted 2007) [#7]
Ah, funny :D