I am using the new DragonFireSDK Box2D, which uses C++ as the original.
Up to now, everything has gone well, but now I have hit a major flaw, as I see it.
I have created a bBall dynamic body, falling under gravity, and a bCar kinematic body, below it.
I have set the bCar body to have a small leftward movement, horizontally, using SetLinearVelocity..
However, as the bBall falls, NO collision occurs between the 2 bodies, and bBall just falls through
bCar.
This is a very simple example indeed, so I can't think of any outside influences. (I have made it
simple for now, for testing purposes).
iforce2D tutorials show something similar in oriiginal Box2D, and a collision occurs under similar
circumstances, so that's why there is a flaw in the DFSDK version, unless I've made some error!
Here's the code - can anyone suggest a solution?
Code:
#include "DragonFireSDK.h"
#include <Box2D/Box2D.h>
const float SCALE = 1.0f / 32.0f;
const float DELTA = 1.0f / 30.0f;
const float BODY_SIZE = (50.f * SCALE) / 2.0f;
const float EARTH_GRAVITY = 9.81f;
const bool AllowSleep = true;
b2World* myWorld;
b2BodyDef ballBodyDef;
b2BodyDef carBodyDef;
b2CircleShape CircleShape;
b2PolygonShape BoxShape;
b2Body *bBall;
b2Body *bCar;
b2Vec2 Gravity;
int vwBall;
b2BodyDef floorBodyDef;
b2Body *bFloor;
b2PolygonShape FloorEdge;
float xTilt;
float yTilt;
int Car8View;
void AppMain()
{
myWorld = new b2World(b2Vec2(0.0f, 0.8f), true);
ballBodyDef.type = b2_dynamicBody;
ballBodyDef.position.Set((130 * SCALE), (50 * SCALE));
CircleShape.m_radius = BODY_SIZE;
b2FixtureDef CircleFixture;
CircleFixture.shape = &CircleShape;
CircleFixture.density = 1.f;
CircleFixture.restitution = .5f;
CircleFixture.friction = .2f;
bBall = myWorld->CreateBody(&ballBodyDef);
bBall->CreateFixture(&CircleFixture);
vwBall = ViewAdd("Images/ball.png", 0, 0);
carBodyDef.type = b2_kinematicBody;
carBodyDef.position.Set((130 * SCALE), (180 * SCALE));
BoxShape.SetAsBox(BODY_SIZE, BODY_SIZE);
b2FixtureDef BoxFixture;
BoxFixture.shape = &BoxShape;
BoxFixture.density = 1.0f;
BoxFixture.restitution = 1.0f;
BoxFixture.friction = 1.2f;
bCar = myWorld->CreateBody(&carBodyDef);
bCar ->CreateFixture(&BoxFixture);
Car8View = ViewAdd("Images/Car4.png", 0, 0);
floorBodyDef.position.Set(0.0f, 480 * SCALE);
bFloor = myWorld->CreateBody(&floorBodyDef);
FloorEdge.SetAsEdge(b2Vec2(-0,0), b2Vec2(320 * SCALE,0));
bFloor->CreateFixture(&FloorEdge, 0.0f);
//Gravity.Set(0.0f, EARTH_GRAVITY); // Set default gravity vector:
//myWorld = new b2World(Gravity, AllowSleep); // Initialize the World object:
}
void OnTimer()
//{
myWorld->Step(DELTA , 10, 10);
ViewSetxy(vwBall ,((bBall ->GetPosition().x - BODY_SIZE) / SCALE),((bBall ->GetPosition().y - BODY_SIZE) / SCALE));
ViewSetxy(Car8View ,((bCar ->GetPosition().x - BODY_SIZE) / SCALE),((bCar ->GetPosition().y - BODY_SIZE) / SCALE));
{
xTilt = TiltGetx()/50.0f;
yTilt = TiltGety()/50.0f;
bBall->ApplyLinearImpulse(b2Vec2(xTilt, yTilt), bBall->GetWorldCenter());
//bCar->ApplyLinearImpulse(b2Vec2(xTilt, yTilt), bCar->GetWorldCenter());
bCar->SetLinearVelocity(b2Vec2(-2, 0));
// If tilt not available, give the yTilt a default value (Earth-like gravity):
if (yTilt==0.0)
yTilt=EARTH_GRAVITY;
// Update the gravity vector:
Gravity.Set(xTilt, yTilt);
// Set the updated gravity vector to the World object:
myWorld->SetGravity(Gravity);
}
}
void AppExit()
{
}