Need help with physics

Blitz3D Forums/Blitz3D Programming/Need help with physics

slenkar(Posted 2004) [#1]
so far I have this:
Function Calc_Speed()
If time>speed_timer+1000
For s.ship=Each ship

s\old_x=s\new_x
s\new_x=EntityX(s\ship_handle,1)
s\old_y=s\new_y
s\new_y=EntityY(s\ship_handle,1)
s\old_z=s\new_z
s\new_z=EntityZ(s\ship_handle,1)

Next
speed_timer=time
EndIf
End Function

a function to get the positions of the entities
every second

Function Get_speed#(ship1,axis)
s.ship=Object.ship(EntityName(ship1))

If axis=x
Return Float ((s\new_x-s\old_x)/10)
EndIf

If axis=y
Return Float ((s\new_y-s\old_y)/10)
EndIf

If axis=z
Return Float ((s\new_z-s\old_Z)/10)
EndIf
End Function


a function to calculate the speed

Function Ship_Collision(ship1,ship2)

s.ship=Object.ship(ship2)
collider=ship1

xspeed#=Get_speed(collider,x)-get_speed(s\ship_handle,x)
yspeed#=get_speed(collider,y)-get_speed(s\ship_handle,y)
zspeed#=get_speed(collider,z)-get_speed(s\ship_handle,z)

translateentity s\ship_handle,xspeed,yspeed,zspeed

End Function



this function applies the forces

when the ships collide at certain angles its realistic but at other angles they bounce in the opposite direction they should,so I think the problem is with plus and minus


Jeppe Nielsen(Posted 2004) [#2]
I think your Get_speed() function is returning the x speed every time, correct it to this (I think):
Function Get_speed#(ship1,axis)
s.ship=Object.ship(EntityName(ship1))
select axis
case 0
Return Float ((s\new_x-s\old_x)/10)
case 1
Return Float ((s\new_y-s\old_y)/10)
case 2
Return Float ((s\new_z-s\old_Z)/10)
end select
End Function

And then change your Ship_collision() function to this:
Function Ship_Collision(ship1,ship2)

s.ship=Object.ship(ship2)
collider=ship1


If s<>Null

xspeed#=Get_speed(collider,0)-get_speed(s\ship_handle,0)
yspeed#=get_speed(collider,1)-get_speed(s\ship_handle,1)
zspeed#=get_speed(collider,2)-get_speed(s\ship_handle,2)

translateentity s\ship_handle,xspeed,yspeed,zspeed

endif

End Function


Hope this helps.


slenkar(Posted 2004) [#3]
ah yeah you are right I forgot to set CONSTS for x,y and z doh!!!! Ill see what happens


slenkar(Posted 2004) [#4]
Run this program:

Type globes
Field old_x#
Field new_x#
Field old_y#
Field new_y#
Field old_z#
Field new_z#
Field speed#
Field translatex#
Field translatey#
Field translatez#
Field collided
Field target_handle
Field globe_handle
Field pivot_handle
End Type

Const sphere_type=1
Const x_axis=1
Const y_axis=2
Const z_axis=3

Global speed_timer
Global transx#
Global transy#
Global transz#

Dim move_nodes(5)
Dim spheres(20)

Graphics3D 800,600
SetBuffer BackBuffer()
Global camera=CreateCamera()
SeedRnd MilliSecs()
PositionEntity camera,0,50,0
RotateEntity camera,-90,0,0
light=CreateLight()



Global sphere=CreateSphere()
EntityRadius sphere,1
EntityType sphere,sphere_type
Collisions sphere_type,sphere_type,1,2
HideEntity sphere

For n= 1To 5
move_nodes(n)=CreatePivot()
PositionEntity move_nodes(n),n*10,0,0
Next

For n=1 To 9
spheres(n)=CopyEntity(sphere)
g.globes=New globes
g\globe_Handle=spheres(n)
g\pivot_handle=CreatePivot(spheres(n))
g\target_handle=move_nodes(Rnd(1,5))
PositionEntity g\globe_Handle,n*10,0,0
PointEntity camera,g\globe_handle
EntityColor g\globe_handle,Rnd(1,255),Rnd(1,255),Rnd(1,255)

Next

While Not KeyDown(1)
For g.globes=Each globes
slow_point(g\pivot_handle,g\globe_handle,2,g\target_handle)
If EntityDistance (g\target_handle,g\globe_handle)>1
MoveEntity g\globe_handle,0,0,0.5
EndIf

g\collided=EntityCollided (g\globe_handle,sphere_type)
If g\collided
ship_collision(g\globe_Handle,g\collided)
EndIf



g\translatex=g\translatex*0.9


g\translatey=g\translatey*0.9


g\translatez=g\translatez*0.9


TranslateEntity g\globe_handle,g\translatex,g\translatey,g\translatez,1
calc_speed()

Next
If KeyHit(57)
For g.globes=Each globes
PointEntity camera,g\globe_handle
Exit
Next
EndIf
RenderWorld
UpdateWorld
Flip
Wend
End
Function Slow_Point(pivot,ship,turn_speed#,target)

If target>0
PointEntity pivot,target
EndIf

pivotyaw#=EntityYaw(pivot)
pivotpitch#=EntityPitch(pivot)
pivotroll#=EntityRoll(pivot)


g#=turn_speed/10

If target=0
If pivotroll<0.5 And pivotroll>-0.5
If pivotpitch<0.5 And pivotpitch>-0.5
If pivotyaw<0.5 And pivotyaw>-0.5
Return(yes)
EndIf
EndIf
EndIf
EndIf

If target<>0
If pivotroll<1 And pivotroll>-1
If pivotpitch<1 And pivotpitch>-1
If pivotyaw<1 And pivotyaw>-1
PointEntity ship,target
Return(yes)
EndIf
EndIf
EndIf
EndIf

If pivotroll>0
If pivotroll>1
TurnEntity ship,0,0,turn_speed
Else
TurnEntity ship,0,0,g
EndIf
EndIf

If pivotroll<0
If pivotroll<-1
TurnEntity ship,0,0,-turn_speed
Else
TurnEntity ship,0,0,-g
EndIf
EndIf

If pivotpitch>0
If pivotpitch>1
TurnEntity ship,turn_speed,0,0
Else
TurnEntity ship,g,0,0
EndIf
EndIf

If pivotpitch<0 
If pivotpitch<-1
TurnEntity ship,-turn_speed,0,0
Else
TurnEntity ship,-g,0,0
EndIf
EndIf


If pivotyaw>0
If pivotyaw>1
TurnEntity ship,0,turn_speed,0
Else
TurnEntity ship,0,g,0
EndIf
EndIf

If pivotyaw<0
If pivotyaw<-1
TurnEntity ship,0,-turn_speed,0
Else
TurnEntity ship,0,-g,0
EndIf
EndIf


End Function

Function Get_speed#(ship1,axis)
For g.globes=Each globes
If g\globe_handle=ship1

If axis=x_axis
Return Float ((g\new_x-g\old_x)/10)
EndIf

If axis=y_axis
Return Float ((g\new_y-g\old_y)/10)
EndIf

If axis=z_axis
Return Float ((g\new_z-g\old_Z)/10)
EndIf
EndIf
Next
End Function

Function Ship_Collision(ship1,ship2)
For g.globes=Each globes
If g\globe_handle=ship2

xspeed#=Get_speed(ship1,x_axis)-get_speed(ship2,x_axis)
yspeed#=get_speed(ship1,y_axis)-get_speed(ship2,y_axis)
zspeed#=get_speed(ship1,z_axis)-get_speed(ship2,z_axis)

transx=xspeed

transy=yspeed

transz=zspeed

g\translatex=transx

g\translatey=transy
g\translatez=transz
EndIf
Next
End Function

Function Calc_Speed()
If MilliSecs()>speed_timer+1000
For g.globes=Each globes
g\old_x=g\new_x
g\new_x=EntityX(g\globe_handle,1)
g\old_y=g\new_y
g\new_y=EntityY(g\globe_handle,1)
g\old_z=g\new_z
g\new_z=EntityZ(g\globe_handle,1)
Next
speed_timer=MilliSecs()
EndIf
End Function


sometimes the balls move in the opposite direction they should when they get stuck and start twitching