I made a simulation of a car with two wheels using my plugin. When the user presses the upkey or downkey, the motor force and speed for the wheel joints is set to make the car travel forwards and backwards. Sometimes, if the car crashes into a steep or vertical slope, the simulation 'freezes'. It doesn't stop running, it's just that nothing moves anymore 🙁
Here is the executable and its media in a .zip.
Here is the DBP code:
and here is the relevent plugin code that is used by it:
Here is the executable and its media in a .zip.
Here is the DBP code:
Code:
sync on
load image "Car.png",1,1
load image "Tire.png",2,1
sprite 1,-200,-200,1
sprite 2,-200,-200,2
offset sprite 1,75,15
offset sprite 2,12,12
b2SetUnits 0.1,0.017453292519943295769236907684886
MyWorld = b2CreateWorld(-2000,-2000,2000,2000,0,50,1)
MyFloor = b2CreateBoxBody(MyWorld, 200, 50, 0, 0.3, 5)
MyFloor2 = b2CreateBoxBody(MyWorld, 1024, 50, 0, 0.3, 5)
MyFloor3 = b2CreateBoxBody(MyWorld, 1024, 50, 0, 0.3, 5)
MyFloor4 = b2CreateBoxBody(MyWorld, 1024, 50, 0, 0.3, 5)
MyCar = b2CreateBoxBody(MyWorld, 140, 30, 1, 0.3, 0.01)
MyWheel1 = b2CreateCircleBody(MyWorld, 12, 1, 0.3, 2500)
MyWheel2 = b2CreateCircleBody(MyWorld, 12, 1, 0.3, 2500)
b2PositionBody MyFloor,512,670
b2PositionBody MyFloor2,512,700
b2PositionBody MyFloor3,974,600
b2PositionBody MyFloor4,50,600
b2RotateBody MyFloor,-20
b2RotateBody MyFloor3,-70
b2RotateBody MyFloor4,70
b2PositionBody MyCar,512,335
b2PositionBody MyWheel1,460,365
b2PositionBody MyWheel2,564,365
MyAxel1 = b2CreateRevoluteJoint(MyWorld, MyCar, MyWheel1, 460,365)
MyAxel2 = b2CreateRevoluteJoint(MyWorld, MyCar, MyWheel2, 564,365)
b2SetJointMotorSpeed MyAxel1,1000
do
if upkey()
b2SetJointMotorSpeed MyAxel1,1000
b2SetJointMotorForce MyAxel1,200
else
if downkey()
b2SetJointMotorSpeed MyAxel1,-1000
b2SetJointMotorForce MyAxel1,50
else
b2SetJointMotorForce MyAxel1,0
endif
endif
if spacekey()
b2SetBodyLinearVelocity MyCar,b2GetBodyLinearVelocityX(MyCar),-100
endif
torque = rightkey()-leftkey()
b2ApplyTorque MyCar,torque*50000
b2Step MyWorld
b2DrawDebugInfo MyWorld,0,0,1,1
rotate sprite 1,b2GetBodyAngle(MyCar)
paste sprite 1,b2GetBodyX(MyCar),b2GetBodyY(MyCar)
rotate sprite 2,b2GetBodyAngle(MyWheel1)
paste sprite 2,b2GetBodyX(MyWheel1),b2GetBodyY(MyWheel1)
rotate sprite 2,b2GetBodyAngle(MyWheel2)
paste sprite 2,b2GetBodyX(MyWheel2),b2GetBodyY(MyWheel2)
if a
box 100,100,110,110
endif
a = 1-a
sync
cls 0
loop
load image "Car.png",1,1
load image "Tire.png",2,1
sprite 1,-200,-200,1
sprite 2,-200,-200,2
offset sprite 1,75,15
offset sprite 2,12,12
b2SetUnits 0.1,0.017453292519943295769236907684886
MyWorld = b2CreateWorld(-2000,-2000,2000,2000,0,50,1)
MyFloor = b2CreateBoxBody(MyWorld, 200, 50, 0, 0.3, 5)
MyFloor2 = b2CreateBoxBody(MyWorld, 1024, 50, 0, 0.3, 5)
MyFloor3 = b2CreateBoxBody(MyWorld, 1024, 50, 0, 0.3, 5)
MyFloor4 = b2CreateBoxBody(MyWorld, 1024, 50, 0, 0.3, 5)
MyCar = b2CreateBoxBody(MyWorld, 140, 30, 1, 0.3, 0.01)
MyWheel1 = b2CreateCircleBody(MyWorld, 12, 1, 0.3, 2500)
MyWheel2 = b2CreateCircleBody(MyWorld, 12, 1, 0.3, 2500)
b2PositionBody MyFloor,512,670
b2PositionBody MyFloor2,512,700
b2PositionBody MyFloor3,974,600
b2PositionBody MyFloor4,50,600
b2RotateBody MyFloor,-20
b2RotateBody MyFloor3,-70
b2RotateBody MyFloor4,70
b2PositionBody MyCar,512,335
b2PositionBody MyWheel1,460,365
b2PositionBody MyWheel2,564,365
MyAxel1 = b2CreateRevoluteJoint(MyWorld, MyCar, MyWheel1, 460,365)
MyAxel2 = b2CreateRevoluteJoint(MyWorld, MyCar, MyWheel2, 564,365)
b2SetJointMotorSpeed MyAxel1,1000
do
if upkey()
b2SetJointMotorSpeed MyAxel1,1000
b2SetJointMotorForce MyAxel1,200
else
if downkey()
b2SetJointMotorSpeed MyAxel1,-1000
b2SetJointMotorForce MyAxel1,50
else
b2SetJointMotorForce MyAxel1,0
endif
endif
if spacekey()
b2SetBodyLinearVelocity MyCar,b2GetBodyLinearVelocityX(MyCar),-100
endif
torque = rightkey()-leftkey()
b2ApplyTorque MyCar,torque*50000
b2Step MyWorld
b2DrawDebugInfo MyWorld,0,0,1,1
rotate sprite 1,b2GetBodyAngle(MyCar)
paste sprite 1,b2GetBodyX(MyCar),b2GetBodyY(MyCar)
rotate sprite 2,b2GetBodyAngle(MyWheel1)
paste sprite 2,b2GetBodyX(MyWheel1),b2GetBodyY(MyWheel1)
rotate sprite 2,b2GetBodyAngle(MyWheel2)
paste sprite 2,b2GetBodyX(MyWheel2),b2GetBodyY(MyWheel2)
if a
box 100,100,110,110
endif
a = 1-a
sync
cls 0
loop
and here is the relevent plugin code that is used by it:
Code:
DLLCMDC int CreateCircleBody(int ID, float Radius, float Density, float Restitution, float Friction)
{
b2World* MyWorld = GetWorldPtr(ID);
b2CircleDef* MyShape = new b2CircleDef();
MyShape->radius = Radius*Units;
MyShape->density = Density;
MyShape->restitution = Restitution;
MyShape->friction = Friction;
b2BodyDef* MyBodyTemplate = new b2BodyDef();
MyBodyTemplate->AddShape(MyShape);
return GetBodyID(MyWorld->CreateBody(MyBodyTemplate));
}
DLLCMDC int CreateBoxBody(int ID, float Width, float Height, float Density, float Restitution, float Friction)
{
b2World* MyWorld = GetWorldPtr(ID);
b2BoxDef* MyShape = new b2BoxDef();
MyShape->extents.Set(Width*0.5f*Units, Height*0.5f*Units);
MyShape->density = Density;
MyShape->restitution = Restitution;
MyShape->friction = Friction;
b2BodyDef* MyBodyTemplate = new b2BodyDef();
MyBodyTemplate->AddShape(MyShape);
return GetBodyID(MyWorld->CreateBody(MyBodyTemplate));
}
DLLCMDC int CreateRevoluteJoint(int ID, int Body1ID, int Body2ID, float x, float y)
{
b2RevoluteJointDef* NewJointDef = new b2RevoluteJointDef();
NewJointDef->body1 = GetBodyPtr(Body1ID);
NewJointDef->body2 = GetBodyPtr(Body2ID);
NewJointDef->anchorPoint.Set(x*Units, y*Units);
return GetJointID(GetWorldPtr(ID)->CreateJoint(NewJointDef));
}
DLLCMDC void SetJointMotorForce(int JointID, float Force)
{
b2Joint* MyJoint = GetJointPtr(JointID);
switch (MyJoint->m_type)
{
case e_prismaticJoint:
{
b2PrismaticJoint* MyPrismaticJoint = (b2PrismaticJoint*)MyJoint;
if (Force)
{
MyPrismaticJoint->m_enableMotor = true;
MyPrismaticJoint->m_maxMotorForce = Force;
}
else
{
MyPrismaticJoint->m_enableMotor = false;
}
}
break;
case e_revoluteJoint:
{
b2RevoluteJoint* MyRevoluteJoint = (b2RevoluteJoint*)MyJoint;
if (Force)
{
MyRevoluteJoint->m_enableMotor = true;
MyRevoluteJoint->m_maxMotorTorque = Force;
}
else
{
MyRevoluteJoint->m_enableMotor = false;
}
}
break;
}
}
DLLCMDC void SetJointMotorSpeed(int JointID, float Speed)
{
b2Joint* MyJoint = GetJointPtr(JointID);
switch (MyJoint->m_type)
{
case e_prismaticJoint:
{
b2PrismaticJoint* MyPrismaticJoint = (b2PrismaticJoint*)MyJoint;
MyPrismaticJoint->m_motorSpeed = Speed*Units;
}
break;
case e_revoluteJoint:
{
b2RevoluteJoint* MyRevoluteJoint = (b2RevoluteJoint*)MyJoint;
MyRevoluteJoint->m_motorSpeed = Speed*AngUnits;
}
break;
}
}
DLLCMDC void Step2(int ID)
{
GetWorldPtr(ID)->Step(DefaultTimeStep, 10);
}
{
b2World* MyWorld = GetWorldPtr(ID);
b2CircleDef* MyShape = new b2CircleDef();
MyShape->radius = Radius*Units;
MyShape->density = Density;
MyShape->restitution = Restitution;
MyShape->friction = Friction;
b2BodyDef* MyBodyTemplate = new b2BodyDef();
MyBodyTemplate->AddShape(MyShape);
return GetBodyID(MyWorld->CreateBody(MyBodyTemplate));
}
DLLCMDC int CreateBoxBody(int ID, float Width, float Height, float Density, float Restitution, float Friction)
{
b2World* MyWorld = GetWorldPtr(ID);
b2BoxDef* MyShape = new b2BoxDef();
MyShape->extents.Set(Width*0.5f*Units, Height*0.5f*Units);
MyShape->density = Density;
MyShape->restitution = Restitution;
MyShape->friction = Friction;
b2BodyDef* MyBodyTemplate = new b2BodyDef();
MyBodyTemplate->AddShape(MyShape);
return GetBodyID(MyWorld->CreateBody(MyBodyTemplate));
}
DLLCMDC int CreateRevoluteJoint(int ID, int Body1ID, int Body2ID, float x, float y)
{
b2RevoluteJointDef* NewJointDef = new b2RevoluteJointDef();
NewJointDef->body1 = GetBodyPtr(Body1ID);
NewJointDef->body2 = GetBodyPtr(Body2ID);
NewJointDef->anchorPoint.Set(x*Units, y*Units);
return GetJointID(GetWorldPtr(ID)->CreateJoint(NewJointDef));
}
DLLCMDC void SetJointMotorForce(int JointID, float Force)
{
b2Joint* MyJoint = GetJointPtr(JointID);
switch (MyJoint->m_type)
{
case e_prismaticJoint:
{
b2PrismaticJoint* MyPrismaticJoint = (b2PrismaticJoint*)MyJoint;
if (Force)
{
MyPrismaticJoint->m_enableMotor = true;
MyPrismaticJoint->m_maxMotorForce = Force;
}
else
{
MyPrismaticJoint->m_enableMotor = false;
}
}
break;
case e_revoluteJoint:
{
b2RevoluteJoint* MyRevoluteJoint = (b2RevoluteJoint*)MyJoint;
if (Force)
{
MyRevoluteJoint->m_enableMotor = true;
MyRevoluteJoint->m_maxMotorTorque = Force;
}
else
{
MyRevoluteJoint->m_enableMotor = false;
}
}
break;
}
}
DLLCMDC void SetJointMotorSpeed(int JointID, float Speed)
{
b2Joint* MyJoint = GetJointPtr(JointID);
switch (MyJoint->m_type)
{
case e_prismaticJoint:
{
b2PrismaticJoint* MyPrismaticJoint = (b2PrismaticJoint*)MyJoint;
MyPrismaticJoint->m_motorSpeed = Speed*Units;
}
break;
case e_revoluteJoint:
{
b2RevoluteJoint* MyRevoluteJoint = (b2RevoluteJoint*)MyJoint;
MyRevoluteJoint->m_motorSpeed = Speed*AngUnits;
}
break;
}
}
DLLCMDC void Step2(int ID)
{
GetWorldPtr(ID)->Step(DefaultTimeStep, 10);
}