Petr,
in a try to avoid those not ... "elegant" PEEK calling (please do not kill me for that ) her it is my code. It uses a type to store body coordinates at the same memory location of bodies. Please see if it works. It should also add some little more speed because it avoid to call ParseVector every time.
A lot of fun looking at those forces simulations, thank you VERY MUCH !
[code=thinbasic] ' ODE02 - Connecting bodies with joints
uses "TBGL"
#include "%APP_INCLUDEPATH%\thinbasic_ode.inc"
' -- This will create window
dim hWnd as dword = tbgl_CreateWindowEx("ODE 02 - Bodies and joints", 640, 480, 32, 0)
tbgl_ShowWindow
' -- 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 )
' -- 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, 2, 2, 0 )
dim JointPoint1 as dJointID = dJointCreateBall( myWorld, 0 )
dim JointPoint2 as dJointID = dJointCreateBall( myWorld, 0 )
dJointAttach( JointPoint1, myBody1, 0 )
dJointSetBallAnchor( JointPoint1, 0, 2, 0 )
dJointAttach( JointPoint2, myBody1, myBody2 )
dJointSetBallAnchor( JointPoint2, 1, 2, 0 )
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 xyz1 as t_xyz at dBodyGetPosition ( myBody1 )
dim xyz2 as t_xyz at dBodyGetPosition ( myBody2 )
tbgl_GetAsyncKeyState(-1)
While LoopFlag
FPS = tbgl_getFrameRate
if tbgl_GetWindowKeyState( hWnd, %VK_ESCAPE ) then loopFlag = %FALSE
tbgl_ClearFrame
tbgl_Camera 0, 1, 5, 0, 1, 0
DrawSphere( xyz1 )
DrawLine( 0, 2, 0, xyz1.x, xyz1.y, xyz1.z )
DrawLine( xyz1.x, xyz1.y, xyz1.z, xyz2.x, xyz2.y, xyz2.z )
DrawSphere( xyz2 )
tbgl_DrawFrame
dt = 1/FPS
if FPS < 25 then dt = 0.04
dWorldStep( myWorld, dt )
wend
tbgl_DestroyWindow
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 DrawLine( a as single, b as single, c as single, d as single, e as single, f as single)
tbgl_color 0, 255, 0
tbgl_BeginPoly %GL_LINES
tbgl_Vertex a, b, c
tbgl_Vertex d, e, f
tbgl_EndPoly
end sub
[/code]
A strage behave in my tests is if mouse is over or outside TBGL window. When mouse is inside I get about 800/900 FPS. If mouse is outside I get around 1200/1300 FPS.
When mouse is inside TBGL window it continue to be refreshed (flicker). Is it expected?
Ciao
Eros
Bookmarks