ODE : sphere-plane collision from tutorial not working

Started by Petr Schreiber, August 27, 2008, 06:06:23 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Petr Schreiber

Hi,

I still did not gave up on ODE under PowerBASIC, but I have some problems.
I translated tutorial:
http://opende.sourceforge.net/mediawiki-1.6.10/index.php/HOWTO_simple_bouncing_sphere

... to PB as following:

#COMPILE EXE
#DIM ALL

%ODE_DOUBLE = 0
#INCLUDE "ODE.INC"

GLOBAL world AS DWORD
GLOBAL space AS DWORD
GLOBAL body AS DWORD
GLOBAL geom AS DWORD
GLOBAL m AS dMass
GLOBAL contactgroup AS DWORD

GLOBAL CallBackDebug AS STRING

SUB NearCallback CDECL ( BYVAL aData AS DWORD, BYVAL o1 AS DWORD, BYVAL o2 AS DWORD )
  CallBackDebug = "Collision Evaluated"
  LOCAL b1, b2 AS DWORD
  b1 = dGeomGetBody(o1)
  b2 = dGeomGetBody(o2)
  LOCAL contact AS dContact

  contact.surface.mode = %dContactBounce OR %dContactSoftCFM

  ' -- friction parameter
  contact.surface.mu = 3.40*10^38' -- %dInfinity ?
  ' -- bounce is the amount of "bouncyness".
  contact.surface.bounce = 0.9 ' 0 - 1
  ' -- bounce_vel is the minimum incoming velocity to cause a bounce
  contact.surface.bounce_vel = 0.1
  ' -- constraint force mixing parameter
  contact.surface.soft_cfm = 0.001


  LOCAL numc AS LONG
  numc = dCollide (o1,o2,1,contact.geom,SIZEOF(dContact))
  IF numc THEN
    CallBackDebug = CallBackDebug + " *** contact evaluated ( but probably no bounce )***"
    LOCAL c AS DWORD
    c = dJointCreateContact (world,contactgroup, contact)
    dJointAttach (c,b1,b2)
  ELSE
    CallBackDebug = CallBackDebug + " --- NO CONTACT CREATED ---"
  END IF

END SUB

' --  simulation loop
SUB simLoop ()
  LOCAL x, y, z AS SINGLE
  LOCAL Position AS SINGLE PTR

  ' --  find collisions and add contact joints
  dSpaceCollide (space, BYVAL 0,CODEPTR(nearCallback))
  ' --  step the simulation
  dWorldStep (world,0.01)
  ' --  remove all contact joints
  dJointGroupEmpty (contactgroup)
  ' --  redraw sphere at new location
  Position = dGeomGetPosition (geom)

  ' --  DRAW
  IF Position THEN
     x = @Position[0]
     y = @Position[1]
     z = @Position[2]
  END IF
  GRAPHIC CLEAR %WHITE
  GRAPHIC COLOR %BLACK
  GRAPHIC PRINT USING$("#.## #.## #.##", x, y, z)
  GRAPHIC PRINT CallBackDebug
  GRAPHIC SCALE (-40, 40)-(40, -40)

  ' -- Draw the plane
  GRAPHIC WIDTH 2
  GRAPHIC LINE (-20, 0)-(20, 0), RGB(255,0,0)

  ' -- Draw the ball
  GRAPHIC COLOR %RED, -2
  GRAPHIC ELLIPSE (x-0.5,z-0.5)-(x+0.5, z+0.5)
  GRAPHIC SET POS (x, z)
  GRAPHIC PRINT "XZ"
'  GRAPHIC COLOR %green
'  GRAPHIC ELLIPSE (x-0.5,y-0.5)-(x+0.5, y+0.5)
  GRAPHIC COLOR %BLUE, -2
  GRAPHIC ELLIPSE (y-0.5,z-0.5)-(y+0.5, z+0.5)
  GRAPHIC SET POS (y, z)
  GRAPHIC PRINT "YZ"

  GRAPHIC REDRAW

END SUB


FUNCTION PBMAIN () AS LONG

  LOCAL hWin AS DWORD

  GRAPHIC WINDOW "Sphere plane collision", 300, 300, 640, 640 TO hWin
  GRAPHIC ATTACH hWin, 0, REDRAW

  ' --  setup pointers to drawstuff callback functions
  dInitODE ()
  ' --  create world
  world = dWorldCreate ()
  space = dHashSpaceCreate (0)
  dWorldSetGravity (world, 0, 0, -0.2)
  dWorldSetCFM (world,1e-5)
  dCreatePlane (space,0,0,1,0)
  contactgroup = dJointGroupCreate (0)
  ' --  create object
  body = dBodyCreate (world)
  geom = dCreateSphere (space,0.5)
  dMassSetSphere (m,1,0.5)
  dBodySetMass (body,m)
  dGeomSetBody (geom,body)
  ' --  set initial position
  dBodySetPosition (body,0,0,30)

  ' --  run simulation
  LOCAL i AS LONG
  FOR i = 1 TO 4000
    simLoop()
  NEXT
  ' -- ' -- dsSimulationLoop (argc,argv,352,288,&fn)
  ' --  clean up
  dJointGroupDestroy (contactgroup)
  dSpaceDestroy (space)
  dWorldDestroy (world)
  dCloseODE()

  GRAPHIC WINDOW END

END FUNCTION


You will notice the sphere falls, when it reaches plane ( which should bounce it ) it just slows down and happily continues with some change in direction :D
Does anybody has any idea ... why ? :)

I use latest DLL from here, compiled it in CodeBlocks> click


Petr
AMD Sempron 3400+ | 1GB RAM @ 533MHz | GeForce 6200 / GeForce 9500GT | 32bit Windows XP SP3

psch.thinbasic.com
  •  

José Roca

#1
 
I have no idea, sorry. One thing that I have noticed, although doesn't solve the problem, is that SUB NearCallback should be SUB NearCallback CDECL, because ODE uses the C parameter passing convention.
  •  

Petr Schreiber

Hi José,

thanks, I corrected the sample now.
I used CDECL in demos before, but seems to not make any difference in this case.

I wanted to do some introduction to ODE for others, but without this fixed it would not make much sense.
I am sure I am missing something basic.


Petr
AMD Sempron 3400+ | 1GB RAM @ 533MHz | GeForce 6200 / GeForce 9500GT | 32bit Windows XP SP3

psch.thinbasic.com
  •