Box2d - 2D physics

Previous topic - Next topic

bigsofty

Tried a wee compile...

Code (glbasic) Select
_______________________________________
*** Configuration: WIN32 ***
precompiling:
GPC - GLBasic Precompiler V.6.873 SN:2a049f7b - 3D, NET
"main.gbas"(11) warning : command deprecated : Use CLEARSCREEN
"main.gbas"(15) warning : command deprecated : Use CLEARSCREEN
Wordcount:52 commands
compiling:
C:\DOCUME~1\Dad\LOCALS~1\Temp\glbasic\gpc_temp1.cpp: In function `DGInt __GLBASIC__::b_CreateCircle(DGInt, DGInt, DGInt, DGInt, DGInt)':
C:\DOCUME~1\Dad\LOCALS~1\Temp\glbasic\gpc_temp1.cpp:270: error: no matching function for call to `b_NewObject(b2Body*&, __GLBASIC__::DGArray<b2Body*>&)'
C:\DOCUME~1\Dad\LOCALS~1\Temp\glbasic\gpc_temp1.cpp: In function `DGInt __GLBASIC__::b_CreateBox(DGInt, DGInt, DGInt, DGInt, DGInt, DGInt)':
C:\DOCUME~1\Dad\LOCALS~1\Temp\glbasic\gpc_temp1.cpp:307: error: no matching function for call to `b_NewObject(b2Body*&, __GLBASIC__::DGArray<b2Body*>&)'
C:\DOCUME~1\Dad\LOCALS~1\Temp\glbasic\gpc_temp1.cpp: In function `DGInt __GLBASIC__::b_CreatePoly(DGInt, DGInt, DGInt, DGInt, __GLBASIC__::DGIntArray&)':
C:\DOCUME~1\Dad\LOCALS~1\Temp\glbasic\gpc_temp1.cpp:351: error: no matching function for call to `b_NewObject(b2Body*&, __GLBASIC__::DGArray<b2Body*>&)'
C:\DOCUME~1\Dad\LOCALS~1\Temp\glbasic\gpc_temp1.cpp: In function `DGInt __GLBASIC__::b_CreateGroundbox(DGInt, DGInt, DGInt, DGInt)':
C:\DOCUME~1\Dad\LOCALS~1\Temp\glbasic\gpc_temp1.cpp:384: error: no matching function for call to `b_NewObject(b2Body*&, __GLBASIC__::DGArray<b2Body*>&)'
C:\DOCUME~1\Dad\LOCALS~1\Temp\glbasic\gpc_temp1.cpp: In function `DGNat __GLBASIC__::b_JointDistance(DGNat, DGNat, DGInt, DGInt, DGInt, DGInt, DGNat)':
C:\DOCUME~1\Dad\LOCALS~1\Temp\glbasic\gpc_temp1.cpp:1276: error: `myBody1' was not declared in this scope
C:\DOCUME~1\Dad\LOCALS~1\Temp\glbasic\gpc_temp1.cpp:1276: error: `myBody2' was not declared in this scope
C:\DOCUME~1\Dad\LOCALS~1\Temp\glbasic\gpc_temp1.cpp:1278: error: 'class b2World' has no member named 'AddJoint'
C:\DOCUME~1\Dad\LOCALS~1\Temp\glbasic\gpc_temp1.cpp:1279: error: no matching function for call to `b_NewObject(b2Joint*&, __GLBASIC__::DGArray<b2JointType*>&)'
C:\DOCUME~1\Dad\LOCALS~1\Temp\glbasic\gpc_temp1.cpp: In function `DGNat __GLBASIC__::b_JointRevolute(DGNat, DGNat, DGInt, DGInt)':
C:\DOCUME~1\Dad\LOCALS~1\Temp\glbasic\gpc_temp1.cpp:1308: error: `myBody1' was not declared in this scope
C:\DOCUME~1\Dad\LOCALS~1\Temp\glbasic\gpc_temp1.cpp:1308: error: `myBody2' was not declared in this scope
C:\DOCUME~1\Dad\LOCALS~1\Temp\glbasic\gpc_temp1.cpp:1309: error: `bCollideConnected' was not declared in this scope
C:\DOCUME~1\Dad\LOCALS~1\Temp\glbasic\gpc_temp1.cpp:1310: error: `pJ' was not declared in this scope
*** FATAL ERROR - Please post this output in the forum
_______________________________________
*** Finished ***
Elapsed: 2.7 sec. Time: 16:06
Build: 0 succeeded.
*** 1 FAILED ***

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)

Kitty Hello

Oops - Typo.
See 1st post as a fix. Not tested the joints, yet. (needs implementing in the debug, too)

bigsofty

Wow, this is a lot of fun! Thanks Gernot/Shranz!  :enc:
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)

Ozden79

Yep, it compiles and runs the demo, nice work!  =D

Is there anything missing besides than testing joints?

Kitty Hello

I think with joints we are fully equipped.
Oh, no!! Callbacks for sounds on impact are missing then.

bigsofty

Added the setting code for friction, it was missing in the C code.

It ranges from 0.0 to 1.0 ...

Code (glbasic) Select
box2 = b_CreateBox(1, 0.0 , 347,20,32,32) // No friction
box2 = b_CreateBox(1, 0.5 , 347,20,32,32) // Half friction
box2 = b_CreateBox(1, 1.0 , 347,20,32,32) // Full friction


Code from Box2D.gbas ...

Code (glbasic) Select

FUNCTION b_CreateCircle: mass, friction, x, y, radius
INLINE
b2BodyDef def;
def.position.Set(x,y);

b2Body* pBody = bWorld->CreateBody( &def );

b2CircleDef poly;
poly.radius = radius;
poly.friction = friction;
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, x, y, w, h
INLINE
b2BodyDef def;
def.position.Set(x,y);

b2Body* pBody = bWorld->CreateBody( &def );

b2PolygonDef poly;
poly.friction = friction;
if(mass>0.0) poly.density = mass;
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, x, y, pts[]
INLINE
b2BodyDef def;
def.position.Set(x,y);

b2Body* pBody = bWorld->CreateBody( &def );

b2PolygonDef poly;
poly.friction = friction;
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;
pBody->CreateShape( &poly);
if(mass>0.0) pBody->SetMassFromShapes();
return b_NewObject(pBody, bBody);
ENDINLINE
ENDFUNCTION
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)

bigsofty

And now with restitution added. Resititution can be though of as 'bounciness', more restitution, more like made out of rubber it becomes. So the boxes now bounce.

Code (glbasic) Select
box2 = b_CreateBox(1, 0.5 , 0.0, 347,20,32,32) // No restitution
box2 = b_CreateBox(1, 0.5 , 0.5, 347,20,32,32) // Half restitution
box2 = b_CreateBox(1, 0.5 , 1.0, 347,20,32,32) // Full restitution


Code (glbasic) Select

FUNCTION b_CreateCircle: mass, friction, rest, x, y, radius
INLINE
b2BodyDef def;
def.position.Set(x,y);

b2Body* pBody = bWorld->CreateBody( &def );

b2CircleDef poly;
poly.radius = radius;
poly.friction = friction;
poly.restitution = rest;
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, rest, x, y, w, h
INLINE
b2BodyDef def;
def.position.Set(x,y);

b2Body* pBody = bWorld->CreateBody( &def );

b2PolygonDef poly;
poly.friction = friction;
poly.restitution = rest;
poly.density = 1;
if(mass>0.0) poly.density = mass;
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, rest, x, y, pts[]
INLINE
b2BodyDef def;
def.position.Set(x,y);

b2Body* pBody = bWorld->CreateBody( &def );

b2PolygonDef poly;
poly.friction = friction;
poly.restitution = rest;
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;
pBody->CreateShape( &poly);
if(mass>0.0) pBody->SetMassFromShapes();
return b_NewObject(pBody, bBody);
ENDINLINE
ENDFUNCTION
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)

Ozden79

Thank you, these are great...

Any chance to merge these into the original files at some point?

bigsofty

Yes, I think any viable changes the the GLB Box2D package will be added to its archive.

Unfortunately I don't have access personally but I am sure Gernot will add this snippet later.
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)

codegit

Important additions. Thanks.  :good:
------------------------------------------
1 X Acer TravelMate 4270, laptop, XP PRO
1 X Dell Studio 17 laptop, Windows 7
1 X MacBook Pro 2,2 GHz Core 2 Duo, 2 GB RAM, 160 GB HDD, 9400M
2 X iTouch
1 X HTC Desire (Android 2.1)
iPad soon to be added

Kitty Hello

I'll add that.. See 1st post after some minutes.

bigsofty

A little addition to the debug view, mainly so I can see what circles are doing!  ;)

This modification adds a little red arrow to the debug view for each object, showing its angle.

Code (glbasic) Select
FUNCTION b_WorldDebug: xorg=0, yorg=0, scale=1
INLINE

b2Vec2 offset(xorg, yorg);
for(int i=0; i<LEN(bBody); ++i)
{
b2Body* pB = bBody(i);
if(!pB) continue;
DGInt a = -pB->GetAngle() * 180.0 / 3.14159265358979323846;


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 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, RGB(0,255,0));
ox=x; oy=y;
}
const b2Vec2& p2 = pB->GetWorldPoint( pCirc->GetLocalPosition() );
DRAWLINE(p2.x+SIN(a)*r*scale, p2.y+COS(a)*r*scale, p2.x+SIN(a-15)*(r*.80)*scale, p2.y+COS(a-15)*(r*.80)*scale, RGB(255,0,0));
DRAWLINE(p2.x, p2.y, p2.x+SIN(a)*r*scale, p2.y+COS(a)*r*scale, RGB(255,0,0));
DRAWLINE(p2.x+SIN(a)*r*scale, p2.y+COS(a)*r*scale, p2.x+SIN(a+15)*(r*.80)*scale, p2.y+COS(a+15)*(r*.80)*scale, RGB(255,0,0));
}

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, RGB(0,255,0));
}
const b2Vec2& p2 = pB->GetPosition() ;
DRAWLINE(p2.x+SIN(a)*20*scale, p2.y+COS(a)*20*scale, p2.x+SIN(a-15)*15*scale, p2.y+COS(a-15)*16*scale, RGB(255,0,0));
DRAWLINE(p2.x, p2.y, p2.x+SIN(a)*20*scale, p2.y+COS(a)*20*scale, RGB(255,0,0));
DRAWLINE(p2.x+SIN(a)*20*scale, p2.y+COS(a)*20*scale, p2.x+SIN(a+15)*15*scale, p2.y+COS(a+15)*16*scale, RGB(255,0,0));
}
BREAK;
}
}
}

ENDINLINE
ENDFUNCTION
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)

Schranz0r

I <3 DGArray's :D

PC:
AMD Ryzen 7 3800X 16@4.5GHz, 16GB Corsair Vengeance LPX DDR4-3200 RAM, ASUS Dual GeForce RTX™ 3060 OC Edition 12GB GDDR6, Windows 11 Pro 64Bit, MSi Tomahawk B350 Mainboard

backslider

Hi guys,

can you tell me, how i can move objects with the mouse or with keys?
I think it is possible, but i donÃ,´t know how to start  =D

thanks

blackway

Hello,
I have a few problems when I run the example that comes with the wrapper under my Ipod Touch. Everything compiles OK but I have collisions only with the circle, the two boxes fall forever and never collide.
Any Ideas?
Glbasic is so good and if I can use Box2D under Ipod\Iphone I'll stick with it.
Thanks !!!