Box2D Forums

It is currently Sat May 25, 2013 6:42 am

All times are UTC - 8 hours [ DST ]




Post new topic Reply to topic  [ 7 posts ] 
Author Message
PostPosted: Tue Jun 19, 2012 3:57 am 
Offline

Joined: Tue Jun 19, 2012 3:45 am
Posts: 3
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()
{

}


Top
 Profile  
 
PostPosted: Tue Jun 19, 2012 8:07 am 
Offline

Joined: Tue Jun 24, 2008 8:25 pm
Posts: 1517
Location: Tokyo
Hi Dave
I threw this code into the testbed to try it out, and it seems like the car simply moves out of the way before the ball gets to it (with xTilt and yTilt both zero and not changing the gravity). Does Dragonfire SDK have any way to use Box2D's debug draw feature?

EDIT: my mistake... it seems that with the ball being given an impulse equal to gravity every timestep, it moves quick enough to catch the back edge of the car. Perhaps you could give some specific values for that ApplyLinearImpulse, otherwise we're just guessing.


Attachments:
debugdraw.png
debugdraw.png [ 453 Bytes | Viewed 514 times ]
Top
 Profile  
 
PostPosted: Tue Jun 19, 2012 11:43 pm 
Offline

Joined: Tue Jun 19, 2012 3:45 am
Posts: 3
Thanks for the reply!
However, in the DFSDK simulator, the Ball passes straight through the middle of the Car image,
so I'm not quite sure what you mean by altering the ApplyLinearImpulse values - if I do that, then
the Ball will miss the Car in my simulator.
You say that the Ball "catches" the back end of the Car in your testbed - if so, why is a collision
not registered? This is the problem I have - I have put together the correct code, but nothing is
happening, collision-wise.
Do you think its an inherent problem with the DFSDK/Box2D code? In other words, such an
arrangement would work for plain Box2D C++, but NOT working in DFSDK??


Top
 Profile  
 
PostPosted: Wed Jun 20, 2012 1:56 am 
Offline

Joined: Tue Jun 24, 2008 8:25 pm
Posts: 1517
Location: Tokyo
Are you looking at Box2D's debug draw, or something else (eg. sprites), that's the first thing to clarify. If you are already looking at the debug draw output, maybe you could give some code that has fixed values so that others can check without guessing things like the tilt values.

I'll attach what I was using here, in case it helps


Attachments:
dave204.h [3.52 KiB]
Downloaded 16 times
Top
 Profile  
 
PostPosted: Wed Jun 20, 2012 4:14 am 
Offline

Joined: Tue Jun 19, 2012 3:45 am
Posts: 3
Hi everyone - I've solved the problem - it turns out it was my SetAsBox setting was too small
- I was using Constants supplied in a DFSDK tutorial, which meant that the Ball had only a very
narrow area to bounce against the Car!
Altering the width of this Box provided the correct result!


Top
 Profile  
 
PostPosted: Wed Jun 20, 2012 5:21 am 
Offline

Joined: Tue Jun 24, 2008 8:25 pm
Posts: 1517
Location: Tokyo
I'm guessing that means you were not using Box2D's debug draw output, as it would nip problems like this in the bud. I just took a quick look at the DFSDK and it doesn't seem like there is any ready-to-use method to show the debug draw display. The necessary base functions (DrawLine etc) are already there so maybe you could prod them to set something up, I'm sure it would help out a lot of people in future.


Top
 Profile  
 
PostPosted: Mon Jul 02, 2012 10:17 am 
Offline

Joined: Mon Jul 02, 2012 10:13 am
Posts: 1
irresistible force wrote:
I'm guessing that means you were not using Box2D's debug draw output, as it would nip problems like this in the bud. I just took a quick look at the DFSDK and it doesn't seem like there is any ready-to-use method to show the debug draw display. The necessary base functions (DrawLine etc) are already there so maybe you could prod them to set something up, I'm sure it would help out a lot of people in future.


Actually, if you check over at the DragonFireSDK forums, someone has a solution for this: http://dragonfiresdk.net/forum/index.php?topic=174.0

He hooked up DebugDraw and has the source code for it posted on Github. Hope this helps!


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 7 posts ] 

All times are UTC - 8 hours [ DST ]


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group