Box2d - 2D physics

Previous topic - Next topic

Kitty Hello

Uh-Oh!
I'll try to see what this could be.

MrTAToad

Great to see that this doesn't need headers :)

blackway

Hello Gernot!
Any news??
Thank you!!

matchy


blackway

I hate you Matchy !!!  =D

What could be the problem, then?
Any tips to share?
Did you test the example than comes with the wrapper ???
Thanks!

matchy

I think there still is an issue with a lack of box to box/ground collision on the iPhone/iPad compile only. Circles to circles/box is fine so I've been thinking about games with these properties only for now.  O_O

To move a body with the mouse, I add the inline code for SetXForm and added damping to bodies to the original wrapper. I also added circle radius markers in World Debug.
:)
Code (glbasic) Select

FUNCTION b_BodySetXForm: body%, x, y, angle // add this function to the wrapper
INLINE
b2Vec2 pos = bBody(body)->GetPosition();
pos.x=x;
pos.y=y;
bBody(body)->SetXForm(b2Vec2(x,y), angle);
ENDINLINE
ENDFUNCTION

// included damping
FUNCTION b_CreateCircle: mass, friction, restitution, lineardamp, angulardamp, x, y, radius
INLINE
b2BodyDef def;
def.position.Set(x,y);
def.linearDamping = lineardamp; //
def.angularDamping = angulardamp; //
b2Body* pBody = bWorld->CreateBody( &def );
b2CircleDef poly;
poly.radius = radius;
poly.friction = friction;
poly.restitution = restitution;
if(mass>0.0) poly.density = mass;
pBody->CreateShape( &poly);
if(mass>0.0) pBody->SetMassFromShapes();
return b_NewObject(pBody, bBody);
ENDINLINE
ENDFUNCTION

FUNCTION b_CreateBox: mass, friction, restitution, lineardamp,angulardamping, x, y, w, h //, groupindex
INLINE
b2BodyDef def;
def.position.Set(x,y);
def.linearDamping = lineardamp;
def.angularDamping = angulardamping;
b2Body* pBody = bWorld->CreateBody( &def );
b2PolygonDef poly;
if(mass>0.0) poly.density = mass;
// poly.filter.groupIndex = 2;
poly.friction = friction;
poly.restitution = restitution;
poly.SetAsBox(w*.5, h*.5);
pBody->CreateShape( &poly);
if(mass>0.0) pBody->SetMassFromShapes();
return b_NewObject(pBody, bBody);
ENDINLINE
ENDFUNCTION


//! Create a polygonal shaped object. The pts[] is a 2D array
//! that has the polygon points. The last point must not equal the first point.
//! pts[] must be clockwise points.
FUNCTION b_CreatePoly: mass, friction, restitution, lineardamp, angulardamp, x, y, pts[]
INLINE
b2BodyDef def;
def.position.Set(x,y);
def.linearDamping = lineardamp;
def.angularDamping = angulardamp;
b2Body* pBody = bWorld->CreateBody( &def );
b2PolygonDef poly;
poly.vertexCount = LEN(pts);
for(int i=0; i<LEN(pts); ++i)
{
poly.vertices[i] = b2Vec2(pts(i,0), pts(i,1));
}
if(mass>0.0) poly.density = mass;
poly.friction = friction;
poly.restitution = restitution;
pBody->CreateShape( &poly);
if(mass>0.0) pBody->SetMassFromShapes();
return b_NewObject(pBody, bBody);
ENDINLINE
ENDFUNCTION

FUNCTION b_WorldDebug: xorg, yorg, scale // added circle radius marker
INLINE

b2Vec2 offset(xorg, yorg);
for(int i=0; i<LEN(bBody); ++i)
{
b2Body* pB = bBody(i);
if(!pB) continue;
for(b2Shape* pShp = pB->GetShapeList(); pShp; pShp = pShp->GetNext())
{
switch(pShp->GetType())
{
case e_circleShape:
{
b2CircleShape* pCirc = (b2CircleShape*)pShp;
const b2Vec2& p1 = pB->GetWorldPoint( pCirc->GetLocalPosition() );
DGInt r = pCirc->GetRadius();
DGInt a = pB->GetAngle() * -180.0 / 3.14159265358979323846;
DGInt ox, oy;
for(DGInt phi=0; phi<=360; phi+=5)
{
DGInt x = scale*p1.x+xorg+SIN(phi)*r, y=scale*p1.y+yorg+COS(phi)*r;
if(phi>0)
DRAWLINE(ox, oy, x, y, COL_GREEN);
ox=x; oy=y;
}
DGInt x = scale*p1.x+xorg+SIN(a)*r, y = scale*p1.y+yorg+COS(a)*r; // circle radius marker
DRAWLINE(x, y, scale*p1.x+xorg, scale*p1.y+yorg, COL_YELLOW);     //
}
break;
case e_polygonShape:
{
b2PolygonShape* pPoly = (b2PolygonShape*)pShp;
for(int j=0; j<pPoly->GetVertexCount(); ++j)
{
int k = (j+1)%pPoly->GetVertexCount();
const b2Vec2& p1 = pB->GetWorldPoint(pPoly->GetVertices()[j]);
const b2Vec2& p2 = pB->GetWorldPoint(pPoly->GetVertices()[k]);
// Bah! Local coordinates!
DRAWLINE(scale*p1.x+xorg, scale*p1.y+yorg, scale*p2.x+xorg, scale*p2.y+yorg, COL_GREEN);
}
}
break;
}
}
}
ENDINLINE
ENDFUNCTION

blackway

Thanks matchy for the update...
Anybody knows how to fix box to box collision and/or poly to box collision??    :(
Thanks again!

bigsofty

Quote from: matchy on 2010-Jul-16
I think there still is an issue with a lack of box to box/ground collision on the iPhone/iPad compile only. Circles to circles/box is fine so I've been thinking about games with these properties only for now.  O_O

To move a body with the mouse, I add the inline code for SetXForm and added damping to bodies to the original wrapper. I also added circle radius markers in World Debug.
:)
Code (glbasic) Select

FUNCTION b_BodySetXForm: body%, x, y, angle // add this function to the wrapper
INLINE
b2Vec2 pos = bBody(body)->GetPosition();
pos.x=x;
pos.y=y;
bBody(body)->SetXForm(b2Vec2(x,y), angle);
ENDINLINE
ENDFUNCTION

// included damping
FUNCTION b_CreateCircle: mass, friction, restitution, lineardamp, angulardamp, x, y, radius
INLINE
b2BodyDef def;
def.position.Set(x,y);
def.linearDamping = lineardamp; //
def.angularDamping = angulardamp; //
b2Body* pBody = bWorld->CreateBody( &def );
b2CircleDef poly;
poly.radius = radius;
poly.friction = friction;
poly.restitution = restitution;
if(mass>0.0) poly.density = mass;
pBody->CreateShape( &poly);
if(mass>0.0) pBody->SetMassFromShapes();
return b_NewObject(pBody, bBody);
ENDINLINE
ENDFUNCTION

FUNCTION b_CreateBox: mass, friction, restitution, lineardamp,angulardamping, x, y, w, h //, groupindex
INLINE
b2BodyDef def;
def.position.Set(x,y);
def.linearDamping = lineardamp;
def.angularDamping = angulardamping;
b2Body* pBody = bWorld->CreateBody( &def );
b2PolygonDef poly;
if(mass>0.0) poly.density = mass;
// poly.filter.groupIndex = 2;
poly.friction = friction;
poly.restitution = restitution;
poly.SetAsBox(w*.5, h*.5);
pBody->CreateShape( &poly);
if(mass>0.0) pBody->SetMassFromShapes();
return b_NewObject(pBody, bBody);
ENDINLINE
ENDFUNCTION


//! Create a polygonal shaped object. The pts[] is a 2D array
//! that has the polygon points. The last point must not equal the first point.
//! pts[] must be clockwise points.
FUNCTION b_CreatePoly: mass, friction, restitution, lineardamp, angulardamp, x, y, pts[]
INLINE
b2BodyDef def;
def.position.Set(x,y);
def.linearDamping = lineardamp;
def.angularDamping = angulardamp;
b2Body* pBody = bWorld->CreateBody( &def );
b2PolygonDef poly;
poly.vertexCount = LEN(pts);
for(int i=0; i<LEN(pts); ++i)
{
poly.vertices[i] = b2Vec2(pts(i,0), pts(i,1));
}
if(mass>0.0) poly.density = mass;
poly.friction = friction;
poly.restitution = restitution;
pBody->CreateShape( &poly);
if(mass>0.0) pBody->SetMassFromShapes();
return b_NewObject(pBody, bBody);
ENDINLINE
ENDFUNCTION

FUNCTION b_WorldDebug: xorg, yorg, scale // added circle radius marker
INLINE

b2Vec2 offset(xorg, yorg);
for(int i=0; i<LEN(bBody); ++i)
{
b2Body* pB = bBody(i);
if(!pB) continue;
for(b2Shape* pShp = pB->GetShapeList(); pShp; pShp = pShp->GetNext())
{
switch(pShp->GetType())
{
case e_circleShape:
{
b2CircleShape* pCirc = (b2CircleShape*)pShp;
const b2Vec2& p1 = pB->GetWorldPoint( pCirc->GetLocalPosition() );
DGInt r = pCirc->GetRadius();
DGInt a = pB->GetAngle() * -180.0 / 3.14159265358979323846;
DGInt ox, oy;
for(DGInt phi=0; phi<=360; phi+=5)
{
DGInt x = scale*p1.x+xorg+SIN(phi)*r, y=scale*p1.y+yorg+COS(phi)*r;
if(phi>0)
DRAWLINE(ox, oy, x, y, COL_GREEN);
ox=x; oy=y;
}
DGInt x = scale*p1.x+xorg+SIN(a)*r, y = scale*p1.y+yorg+COS(a)*r; // circle radius marker
DRAWLINE(x, y, scale*p1.x+xorg, scale*p1.y+yorg, COL_YELLOW);     //
}
break;
case e_polygonShape:
{
b2PolygonShape* pPoly = (b2PolygonShape*)pShp;
for(int j=0; j<pPoly->GetVertexCount(); ++j)
{
int k = (j+1)%pPoly->GetVertexCount();
const b2Vec2& p1 = pB->GetWorldPoint(pPoly->GetVertices()[j]);
const b2Vec2& p2 = pB->GetWorldPoint(pPoly->GetVertices()[k]);
// Bah! Local coordinates!
DRAWLINE(scale*p1.x+xorg, scale*p1.y+yorg, scale*p2.x+xorg, scale*p2.y+yorg, COL_GREEN);
}
}
break;
}
}
}
ENDINLINE
ENDFUNCTION


Um, I think you've removed the angle arrows I added (The length of the arrow was the circle radius BTW)?

Looks like we have multiple sources floating about here...?  :S
Cheers,

Ian.

"It is practically impossible to teach good programming style to students that have had prior exposure to BASIC.  As potential programmers, they are mentally mutilated beyond hope of regeneration."
(E. W. Dijkstra)

blackway

Hello!, any news about the issue with box 2 box collisions on Iphone?

I'm trying to extend the wrapper, but my knowledge is very limited  =D
Anyway, now we can destroy joints:

Code (glbasic) Select


FUNCTION b_DestroyJoint%: joint%
INLINE

b2Joint* pB = bJoints(joint);
bWorld->DestroyJoint(pB);
joint = NULL;

ENDINLINE


ENDFUNCTION



Bye!

mentalthink

HI, anybody can tell how make what a sprite, have physics collsions:
I don´t know if I explained well.  An example make a hill, well putting boxes into the sprite can be make, but i think is very tedious and complicate to put, perhaps 100 boxes making the perimeter of the sprite. Can be do Box2d the shape of a determinated sprite although don´t be very precise, and this can work in example, how b_createBox command.

Thanks and best Regards.
Iván J.

matchy

I see that the Box2D.h file may have been modified for Mac and iPhone compile but how to compile for WINCE?

Code (glbasic) Select

#if defined( MACOSX) || defined(IPHONE) || defined(WINCE) // Added WINCE
typedef long unsigned int size_t;
#else
typedef unsigned int size_t;
#endif


Compile error:
/cygdrive/c/Users/matchy/AppData/Local/Temp/ccumT4oX.o(.text+0x750):gpc_temp2.cpp: undefined reference to `cosf'
/cygdrive/c/Users/matchy/AppData/Local/Temp/ccumT4oX.o(.text+0x75c):gpc_temp2.cpp: undefined reference to `sinf'
..


Kitty Hello

inline float cosf(float c){return cos(c);}

blackway

Hi Gernot, sorry for ask again, and again, and again  :P
Any news about box to box collision on iphone?
Thank you!

Kitty Hello

is there a bug? I thought it worked?

Oh dear. The problem is, I have no idea about Box2D. You might want to update the box2d package, though. Maybe they fixed that internally? I just wrapped the cpp files and functions. If it works on Windows, it must be some sort or Box2D internal problem.

blackway

Hi Gernot, Thanks for your reply.
I was searching in the box2d forum (sorry I didn't seek before) and I found a guy that have exactly the same problem but fix it:

http://www.box2d.org/forum/viewtopic.php?f=3&t=4524

I'm at work now so I can`t test if adding -fno-regmove instruction flags can fix the problem

What do you think?
Thanks!