ErosOlmi
10-08-2007, 12:46
A little modification of ODE Sample 02. Now all parameters are passed using data structures. A little speed improve thanks to the fact less params are passed.
' ODE02B - Connecting bodies with joints - more complex variant
uses "TBGL"
#include "%APP_INCLUDEPATH%/thinbasic_ode.inc"
' -- This will create window
dim hWnd as dword = tbgl_CreateWindowEx("ODE 02 - Bodies and joints - Hanged triangle", 640, 480, 32, 0)
tbgl_ShowWindow
tbgl_UseLighting 1
tbgl_UseLightsource %GL_LIGHT0, 1
' -- Yes, to create new world is that easy
dim myWorld as dWorldID = dWorldCreate
' -- Planet Earth
dWorldSetGravity(myWorld, 0, -9.81, 0)
' -- We need two bodies...
dim myBody1 as dBodyID = dBodyCreate( myWorld )
dim myBody2 as dBodyID = dBodyCreate( myWorld )
dim myBody3 as dBodyID = dBodyCreate( myWorld )
dim myBody4 as dBodyID = dBodyCreate( myWorld )
dim myBody5 as dBodyID = dBodyCreate( myWorld )
' -- Mass will be "recycled" for both of them
' -- Here we will store mass
dim myMass AS dMass
' -- Although it might seem like something else, we give DENSITY and RADIUS
dMassSetSphere( myMass, 2500.0, 0.05 )
' -- Assign mass to body
dBodySetMass( myBody1, myMass )
' -- Lets position mass at x, y, z
dBodySetPosition( myBody1, 1, 2, 0 )
' -- Assign mass to body
dBodySetMass( myBody2, myMass )
' -- Lets position mass at x, y, z
dBodySetPosition( myBody2, 1, 2, 1 )
' -- Assign mass to body
dBodySetMass( myBody3, myMass )
' -- Lets position mass at x, y, z
dBodySetPosition( myBody3, 0, 2, 1 )
' -- Assign mass to body
dBodySetMass( myBody4, myMass )
' -- Lets position mass at x, y, z
dBodySetPosition( myBody4, 1, 2, 2 )
' -- Assign mass to body
'msgbox 0, "---------------------------------------------------------------"
dBodySetMass( myBody5, myMass )
' -- Lets position mass at x, y, z
dBodySetPosition( myBody5, 0, 2, 2 )
dim JointPoint1 as dJointID = dJointCreateBall( myWorld, 0 )
dim JointPoint2 as dJointID = dJointCreateBall( myWorld, 0 )
dim JointPoint3 as dJointID = dJointCreateBall( myWorld, 0 )
dim JointPoint4 as dJointID = dJointCreateBall( myWorld, 0 )
dim JointPoint5 as dJointID = dJointCreateBall( myWorld, 0 )
dim JointPoint6 as dJointID = dJointCreateBall( myWorld, 0 )
dJointAttach( JointPoint1, myBody1, 0 )
dJointSetBallAnchor( JointPoint1, 0, 2, 0 )
dJointAttach( JointPoint2, myBody1, myBody2 )
dJointSetBallAnchor( JointPoint2, 1, 2, 0 )
dJointAttach( JointPoint3, myBody2, myBody3 )
dJointSetBallAnchor( JointPoint3, 1, 2, 1 )
dJointAttach( JointPoint4, myBody1, myBody3 )
dJointSetBallAnchor( JointPoint4, 1, 2, 0 )
dJointAttach( JointPoint5, myBody2, myBody4 )
dJointSetBallAnchor( JointPoint5, 1, 2, 1 )
dJointAttach( JointPoint6, myBody3, myBody5 )
dJointSetBallAnchor( JointPoint6, 0, 2, 1 )
dim FPS, dt as double
dim LoopFlag as BYTE = %TRUE
dim Clk as long = gettickcount
tbgl_BackColor 255, 255, 255
type t_xyz
x as dReal
y as dReal
z as dReal
end type
dim xyz0 as t_xyz
xyz0.x = 0.0
xyz0.y = 2.0
xyz0.z = 0.0
dim xyz1 as t_xyz at dBodyGetPosition ( myBody1 )
dim xyz2 as t_xyz at dBodyGetPosition ( myBody2 )
dim xyz3 as t_xyz at dBodyGetPosition ( myBody3 )
dim xyz4 as t_xyz at dBodyGetPosition ( myBody4 )
dim xyz5 as t_xyz at dBodyGetPosition ( myBody5 )
tbgl_GetAsyncKeyState(-1)
doevents on
dim i as long
While LoopFlag
FPS = tbgl_getFrameRate
if tbgl_GetWindowKeyState( hWnd, %VK_ESCAPE ) then loopFlag = %FALSE
tbgl_ClearFrame
tbgl_Camera 0, 0, 5, 0, 0, 0
Render_Scene
tbgl_translate 0, -1, 0
tbgl_scale 1, 0, 1
Render_Scene
tbgl_DrawFrame
dt = 1/FPS
if FPS < 25 then dt = 0.04
incr i
if i > 1000 then
i = 0
tbgl_SetWindowTitle( hWnd, FPS & " - " & time$)
end if
dWorldStep( myWorld, dt )
wend
tbgl_DestroyWindow
sub Render_Scene()
DrawSphere( xyz1 )
DrawLine2( xyz0, xyz1 )
DrawLine2( xyz1, xyz2 )
DrawSphere( xyz2 )
DrawLine2( xyz2, xyz3 )
DrawSphere( xyz3 )
DrawLine2( xyz1, xyz3 )
DrawSphere( xyz4 )
DrawLine2( xyz2, xyz4 )
DrawSphere( xyz5 )
DrawLine2( xyz3, xyz5 )
end sub
sub DrawSphere( xyz as t_xyz )
tbgl_PushMatrix
tbgl_Translate xyz.x, xyz.y, xyz.z
tbgl_color 255, 0, 0
tbgl_Sphere 0.05
tbgl_PopMatrix
end sub
sub DrawLine2( P1 as t_xyz, P2 as t_xyz)
tbgl_color 0, 255, 0
tbgl_BeginPoly %GL_LINES
tbgl_Vertex P1.x, P1.y, P1.z
tbgl_Vertex P2.x, P2.y, P2.z
tbgl_EndPoly
end sub
' ODE02B - Connecting bodies with joints - more complex variant
uses "TBGL"
#include "%APP_INCLUDEPATH%/thinbasic_ode.inc"
' -- This will create window
dim hWnd as dword = tbgl_CreateWindowEx("ODE 02 - Bodies and joints - Hanged triangle", 640, 480, 32, 0)
tbgl_ShowWindow
tbgl_UseLighting 1
tbgl_UseLightsource %GL_LIGHT0, 1
' -- Yes, to create new world is that easy
dim myWorld as dWorldID = dWorldCreate
' -- Planet Earth
dWorldSetGravity(myWorld, 0, -9.81, 0)
' -- We need two bodies...
dim myBody1 as dBodyID = dBodyCreate( myWorld )
dim myBody2 as dBodyID = dBodyCreate( myWorld )
dim myBody3 as dBodyID = dBodyCreate( myWorld )
dim myBody4 as dBodyID = dBodyCreate( myWorld )
dim myBody5 as dBodyID = dBodyCreate( myWorld )
' -- Mass will be "recycled" for both of them
' -- Here we will store mass
dim myMass AS dMass
' -- Although it might seem like something else, we give DENSITY and RADIUS
dMassSetSphere( myMass, 2500.0, 0.05 )
' -- Assign mass to body
dBodySetMass( myBody1, myMass )
' -- Lets position mass at x, y, z
dBodySetPosition( myBody1, 1, 2, 0 )
' -- Assign mass to body
dBodySetMass( myBody2, myMass )
' -- Lets position mass at x, y, z
dBodySetPosition( myBody2, 1, 2, 1 )
' -- Assign mass to body
dBodySetMass( myBody3, myMass )
' -- Lets position mass at x, y, z
dBodySetPosition( myBody3, 0, 2, 1 )
' -- Assign mass to body
dBodySetMass( myBody4, myMass )
' -- Lets position mass at x, y, z
dBodySetPosition( myBody4, 1, 2, 2 )
' -- Assign mass to body
'msgbox 0, "---------------------------------------------------------------"
dBodySetMass( myBody5, myMass )
' -- Lets position mass at x, y, z
dBodySetPosition( myBody5, 0, 2, 2 )
dim JointPoint1 as dJointID = dJointCreateBall( myWorld, 0 )
dim JointPoint2 as dJointID = dJointCreateBall( myWorld, 0 )
dim JointPoint3 as dJointID = dJointCreateBall( myWorld, 0 )
dim JointPoint4 as dJointID = dJointCreateBall( myWorld, 0 )
dim JointPoint5 as dJointID = dJointCreateBall( myWorld, 0 )
dim JointPoint6 as dJointID = dJointCreateBall( myWorld, 0 )
dJointAttach( JointPoint1, myBody1, 0 )
dJointSetBallAnchor( JointPoint1, 0, 2, 0 )
dJointAttach( JointPoint2, myBody1, myBody2 )
dJointSetBallAnchor( JointPoint2, 1, 2, 0 )
dJointAttach( JointPoint3, myBody2, myBody3 )
dJointSetBallAnchor( JointPoint3, 1, 2, 1 )
dJointAttach( JointPoint4, myBody1, myBody3 )
dJointSetBallAnchor( JointPoint4, 1, 2, 0 )
dJointAttach( JointPoint5, myBody2, myBody4 )
dJointSetBallAnchor( JointPoint5, 1, 2, 1 )
dJointAttach( JointPoint6, myBody3, myBody5 )
dJointSetBallAnchor( JointPoint6, 0, 2, 1 )
dim FPS, dt as double
dim LoopFlag as BYTE = %TRUE
dim Clk as long = gettickcount
tbgl_BackColor 255, 255, 255
type t_xyz
x as dReal
y as dReal
z as dReal
end type
dim xyz0 as t_xyz
xyz0.x = 0.0
xyz0.y = 2.0
xyz0.z = 0.0
dim xyz1 as t_xyz at dBodyGetPosition ( myBody1 )
dim xyz2 as t_xyz at dBodyGetPosition ( myBody2 )
dim xyz3 as t_xyz at dBodyGetPosition ( myBody3 )
dim xyz4 as t_xyz at dBodyGetPosition ( myBody4 )
dim xyz5 as t_xyz at dBodyGetPosition ( myBody5 )
tbgl_GetAsyncKeyState(-1)
doevents on
dim i as long
While LoopFlag
FPS = tbgl_getFrameRate
if tbgl_GetWindowKeyState( hWnd, %VK_ESCAPE ) then loopFlag = %FALSE
tbgl_ClearFrame
tbgl_Camera 0, 0, 5, 0, 0, 0
Render_Scene
tbgl_translate 0, -1, 0
tbgl_scale 1, 0, 1
Render_Scene
tbgl_DrawFrame
dt = 1/FPS
if FPS < 25 then dt = 0.04
incr i
if i > 1000 then
i = 0
tbgl_SetWindowTitle( hWnd, FPS & " - " & time$)
end if
dWorldStep( myWorld, dt )
wend
tbgl_DestroyWindow
sub Render_Scene()
DrawSphere( xyz1 )
DrawLine2( xyz0, xyz1 )
DrawLine2( xyz1, xyz2 )
DrawSphere( xyz2 )
DrawLine2( xyz2, xyz3 )
DrawSphere( xyz3 )
DrawLine2( xyz1, xyz3 )
DrawSphere( xyz4 )
DrawLine2( xyz2, xyz4 )
DrawSphere( xyz5 )
DrawLine2( xyz3, xyz5 )
end sub
sub DrawSphere( xyz as t_xyz )
tbgl_PushMatrix
tbgl_Translate xyz.x, xyz.y, xyz.z
tbgl_color 255, 0, 0
tbgl_Sphere 0.05
tbgl_PopMatrix
end sub
sub DrawLine2( P1 as t_xyz, P2 as t_xyz)
tbgl_color 0, 255, 0
tbgl_BeginPoly %GL_LINES
tbgl_Vertex P1.x, P1.y, P1.z
tbgl_Vertex P2.x, P2.y, P2.z
tbgl_EndPoly
end sub