Home Tutorials Download Beta Store Forum Documentation KnowledgeBase Wiki Blog

ShiVa3D

Return to Game programming

Dynamics of a monocycle

All about the StoneScript

Dynamics of a monocycle

Postby freezer » 21 May 2012, 15:56

the following is from a spawn function. there is supposed to be a body (box), under that to the side is a support (connected by slider), then on the side of that is a hinge2 which drives a ball.

the issue is that it falls and goes crazy, it looks like it's the hinge2 joint which is the issue, any ideas? or is this setup doomed to fail?


Code: Select all

    local hUser = application.getCurrentUser()
   local hScene = application.getCurrentUserScene()
   
    --[[
        The user is made up of 3 parts
        - the main body
        - a vertical support attached to the main body by slider
        - a ball wheel attached to the support by a hinge2 with motor
       
    --]]
   
    --  ADD USER BODY
    -----------------------------------------------------------------------
    local nBSize = 0.4
    local hObject2 = scene.createRuntimeObject(hScene, "box")
    scene.setObjectTag(hScene, hObject2, "user_body" )
    object.setScale(hObject2, nBSize, nBSize, nBSize)
    shape.setMeshMaterial(hObject2, "ball")
   
    if(dynamics.createBoxBody(hObject2, nBSize, nBSize, nBSize)) then
        dynamics.setMass                    ( hObject2, 100 )
        dynamics.enableDynamics             ( hObject2, true )
        dynamics.enableCollisions           ( hObject2, true )
        dynamics.enableGravity              ( hObject2, true )
        dynamics.setLinearDamping           ( hObject2,  0 )    --  100 is a medium damping.
        dynamics.setAngularDamping          ( hObject2,  0 )    --  100 is a medium damping.
        dynamics.setFriction                ( hObject2,  0 )        --
        dynamics.setBounce                  ( hObject2,  0.0 )        --0.50
    end
   
   
    --  CALC TO START POINT
    -----------------------------------------------------------------------
    local hStart = application.getCurrentUserSceneTaggedObject(this.sStartName())
    local x, y, z = 0
    if(hStart) then
        x, y, z = object.getTranslation(hStart, object.kGlobalSpace)
        object.translateTo(hObject2, x, y+1, z, object.kGlobalSpace, 1)
    else
        --  ???
        log.warning("NO START POINT FOUND")
    end
   
   
    --  ADD USER SUPPORT
    -----------------------------------------------------------------------
    local hObject3 = scene.createRuntimeObject(hScene, "box")
    scene.setObjectTag(hScene, hObject3, "user_supp" )
    object.setScale(hObject3, 0.1, 0.5, 0.3)
    shape.setMeshMaterial(hObject3, "ball")
    object.translateTo(hObject3, x+0.5, y+0.5, z, object.kGlobalSpace, 1)
   
    if(dynamics.createBoxBody(hObject3, 0.1, 0.5, 0.3)) then
        dynamics.setMass                    ( hObject3, 100 )
        dynamics.enableDynamics             ( hObject3, true )
        dynamics.enableCollisions           ( hObject3, true )
        dynamics.enableGravity              ( hObject3, true )
        dynamics.setLinearDamping           ( hObject3,  0 )
        dynamics.setAngularDamping          ( hObject3,  0 )
        dynamics.setFriction                ( hObject3,  0 )
        dynamics.setBounce                  ( hObject3,  0.0 )
    end
   
    --  JOIN SUPPORT TO BODY
    if ( dynamics.createSliderJoint  ( hObject3, hObject2, "car_sus1" ) )then
        dynamics.setSliderJointAxis                     ( hObject3, "car_sus1", 0, 0.5, 0, object.kGlobalSpace )
        dynamics.setSliderJointAxisExtensionLimitBounce ( hObject3, "car_sus1", 0 )
        dynamics.setSliderJointAxisExtensionLimitCFM    ( hObject3, "car_sus1", 0.0 )
        dynamics.setSliderJointAxisExtensionLimitERP    ( hObject3, "car_sus1", 0.4 )
        dynamics.setSliderJointAxisExtensionLimitMax    ( hObject3, "car_sus1", 0.3 )
        dynamics.setSliderJointAxisExtensionLimitMin    ( hObject3, "car_sus1", 0 )
    end
   
   
    --  ADD USER BALL MESH
    -----------------------------------------------------------------------
    local nSize = 0.25
    local hObject = scene.createRuntimeObject(hScene, "ball_01_01")
    scene.setObjectTag(hScene, hObject, "user_ball" )
    object.setScale(hObject, nSize, nSize, nSize)
    object.addAIModel(hObject, "user_ball")
    shape.setMeshMaterial(hObject, "ball")
   
    object.translateTo(hObject, x, y, z, object.kGlobalSpace, 1)
   
    dynamics.createSphereBody(hObject, nSize)
    dynamics.enableDynamics(hObject, true)
    dynamics.enableCollisions(hObject, true)
    dynamics.enableGravity(hObject, true)
    dynamics.setMass(hObject, 0.2)
    dynamics.setFriction(hObject, 0.25)      --  0=no, between 0 and 1
    dynamics.setBounce(hObject, 0.25)
    dynamics.setBounceThreshold(hObject, 0)
   
   
    --  JOIN BALL TO SUPPORT
    local sJointName = "ball_joint"
    if ( dynamics.createHinge2Joint ( hObject, hObject3, sJointName ) ) then
        -- Setting its parameters
        dynamics.setHinge2JointAnchor ( hObject, sJointName, 0.5, 0, 0, object.kLocalSpace )
        dynamics.setHinge2JointAxis1 ( hObject, sJointName, 0, 0, 0.5, object.kGlobalSpace )
        dynamics.setHinge2JointAxis1AngleLimitCFM ( hObject, sJointName, 0) --20 )
        dynamics.setHinge2JointAxis1AngleLimitERP ( hObject, sJointName, 0.4)   --10 )
        dynamics.setHinge2JointAxis1AngleLimitMax ( hObject, sJointName, 0 )
        dynamics.setHinge2JointAxis1AngleLimitMin ( hObject, sJointName, 0 )
        dynamics.setHinge2JointAxis1SuspensionCFM ( hObject, sJointName, 0 )
        dynamics.setHinge2JointAxis1SuspensionERP ( hObject, sJointName, 0.4)   --1 )
        dynamics.setHinge2JointAxis2 ( hObject, sJointName, 0, 0, 0.5, object.kGlobalSpace )
        dynamics.setHinge2JointAxis2MotorAcceleration ( hObject, sJointName,0)
        dynamics.setHinge2JointAxis2MotorSpeedLimit ( hObject, sJointName,1)
    end
   



btw i updated the physics step too

Code: Select all
        --  DYNAMICS SETTINGS
        scene.setDynamicsIterationsPerStep(hScene, 32)
        scene.setDynamicsTimeStep(hScene, 0.00416)



any other ideas on achieving something like this welcome ;)
freezer
Gold Boarder
Gold Boarder
 
Posts: 216

Re: Dynamics of a monocycle

Postby freezer » 21 May 2012, 17:15

this change seems to hold the ball in the right place

Code: Select all

    --  JOIN BALL TO SUPPORT
    local sJointName = "ball_joint"
    if ( dynamics.createHinge2Joint ( hObject, hObject3, sJointName ) ) then
        -- Setting its parameters
        dynamics.setHinge2JointAnchor ( hObject, sJointName, 0.0, 0, 0, object.kLocalSpace )
        dynamics.setHinge2JointAxis1 ( hObject, sJointName, 0.2, 0, 0, object.kGlobalSpace )
        dynamics.setHinge2JointAxis1AngleLimitCFM ( hObject, sJointName, 0) --20 )
        dynamics.setHinge2JointAxis1AngleLimitERP ( hObject, sJointName, 0.8)   --10 )
        dynamics.setHinge2JointAxis1AngleLimitMax ( hObject, sJointName, 0.01 )
        dynamics.setHinge2JointAxis1AngleLimitMin ( hObject, sJointName, 0 )
        dynamics.setHinge2JointAxis1SuspensionCFM ( hObject, sJointName, 0 )
        dynamics.setHinge2JointAxis1SuspensionERP ( hObject, sJointName, 0.8)   --1 )
        dynamics.setHinge2JointAxis2 ( hObject, sJointName, 0, 0, -0.5, object.kGlobalSpace )
        dynamics.setHinge2JointAxis2MotorAcceleration ( hObject, sJointName,0)
        dynamics.setHinge2JointAxis2MotorSpeedLimit ( hObject, sJointName,0)
    end


http://www.ode.org/ode-latest-userguide.html#sec_7_3_5


so it may be a stability issue... is there a way to keep it vertical without adding (hidden) supports?
freezer
Gold Boarder
Gold Boarder
 
Posts: 216

Re: Dynamics of a monocycle

Postby NiCoX » 21 May 2012, 20:20

Hi,

I would not put a null CFM value. Try increasing it a bit (eg. 0.1), this should improve stabilities. Now to be totally honest, simulating a monocycle behavior (without hidden helpers) may be a difficult task...
User avatar
NiCoX
Platinum Boarder
Platinum Boarder
 
Posts: 5652
Location: France

Re: Dynamics of a monocycle

Postby freezer » 22 May 2012, 18:51

not that ive had chance to play, but thinking of segways and i have some convoluted ideas... glad i have a working basic cheat variation already though ;)
freezer
Gold Boarder
Gold Boarder
 
Posts: 216


Return to Game programming