Author Topic: Box2d - 2D physics  (Read 65570 times)

Offline bigsofty

  • Community Developer
  • Prof. Inline
  • ******
  • Posts: 2628
    • View Profile
Re: Box2d - 2D physics
« Reply #45 on: 2010-Mar-11 »
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*>&)&#39;
C:\DOCUME~1\Dad\LOCALS~1\Temp\glbasic\gpc_temp1.cpp: In function `DGInt __GLBASIC__::b_CreateBox(DGInt, DGInt, DGInt, DGInt, DGInt, DGInt)&#39;:
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*>&)&#39;
C:\DOCUME~1\Dad\LOCALS~1\Temp\glbasic\gpc_temp1.cpp: In function `DGInt __GLBASIC__::b_CreatePoly(DGInt, DGInt, DGInt, DGInt, __GLBASIC__::DGIntArray&)&#39;:
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*>&)&#39;
C:\DOCUME~1\Dad\LOCALS~1\Temp\glbasic\gpc_temp1.cpp: In function `DGInt __GLBASIC__::b_CreateGroundbox(DGInt, DGInt, DGInt, DGInt)&#39;:
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*>&)&#39;
C:\DOCUME~1\Dad\LOCALS~1\Temp\glbasic\gpc_temp1.cpp: In function `DGNat __GLBASIC__::b_JointDistance(DGNat, DGNat, DGInt, DGInt, DGInt, DGInt, DGNat)&#39;:
C:\DOCUME~1\Dad\LOCALS~1\Temp\glbasic\gpc_temp1.cpp:1276: error: `myBody1&#39; was not declared in this scope
C:\DOCUME~1\Dad\LOCALS~1\Temp\glbasic\gpc_temp1.cpp:1276: error: `myBody2&#39; was not declared in this scope
C:\DOCUME~1\Dad\LOCALS~1\Temp\glbasic\gpc_temp1.cpp:1278: error: &#39;class b2World&#39; has no member named &#39;AddJoint&#39;
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*>&)&#39;
C:\DOCUME~1\Dad\LOCALS~1\Temp\glbasic\gpc_temp1.cpp: In function `DGNat __GLBASIC__::b_JointRevolute(DGNat, DGNat, DGInt, DGInt)&#39;:
C:\DOCUME~1\Dad\LOCALS~1\Temp\glbasic\gpc_temp1.cpp:1308: error: `myBody1&#39; was not declared in this scope
C:\DOCUME~1\Dad\LOCALS~1\Temp\glbasic\gpc_temp1.cpp:1308: error: `myBody2&#39; was not declared in this scope
C:\DOCUME~1\Dad\LOCALS~1\Temp\glbasic\gpc_temp1.cpp:1309: error: `bCollideConnected&#39; was not declared in this scope
C:\DOCUME~1\Dad\LOCALS~1\Temp\glbasic\gpc_temp1.cpp:1310: error: `pJ&#39; 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)

Offline Kitty Hello

  • code monkey
  • Administrator
  • Prof. Inline
  • *******
  • Posts: 10714
  • here on my island the sea says 'hello'
    • View Profile
    • http://www.glbasic.com
Re: Box2d - 2D physics
« Reply #46 on: 2010-Mar-12 »
Oops - Typo.
See 1st post as a fix. Not tested the joints, yet. (needs implementing in the debug, too)

Offline bigsofty

  • Community Developer
  • Prof. Inline
  • ******
  • Posts: 2628
    • View Profile
Re: Box2d - 2D physics
« Reply #47 on: 2010-Mar-12 »
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)

Offline Ozden79

  • Mr. Polyvector
  • ***
  • Posts: 121
    • View Profile
Re: Box2d - 2D physics
« Reply #48 on: 2010-Mar-12 »
Yep, it compiles and runs the demo, nice work!  =D

Is there anything missing besides than testing joints?

Offline Kitty Hello

  • code monkey
  • Administrator
  • Prof. Inline
  • *******
  • Posts: 10714
  • here on my island the sea says 'hello'
    • View Profile
    • http://www.glbasic.com
Re: Box2d - 2D physics
« Reply #49 on: 2010-Mar-12 »
I think with joints we are fully equipped.
Oh, no!! Callbacks for sounds on impact are missing then.

Offline bigsofty

  • Community Developer
  • Prof. Inline
  • ******
  • Posts: 2628
    • View Profile
Re: Box2d - 2D physics
« Reply #50 on: 2010-Mar-23 »
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)

Offline bigsofty

  • Community Developer
  • Prof. Inline
  • ******
  • Posts: 2628
    • View Profile
Re: Box2d - 2D physics
« Reply #51 on: 2010-Mar-23 »
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)

Offline Ozden79

  • Mr. Polyvector
  • ***
  • Posts: 121
    • View Profile
Re: Box2d - 2D physics
« Reply #52 on: 2010-Mar-23 »
Thank you, these are great...

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

Offline bigsofty

  • Community Developer
  • Prof. Inline
  • ******
  • Posts: 2628
    • View Profile
Re: Box2d - 2D physics
« Reply #53 on: 2010-Mar-23 »
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)

Offline codegit

  • Dr. Type
  • ****
  • Posts: 270
    • View Profile
Re: Box2d - 2D physics
« Reply #54 on: 2010-Mar-23 »
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

Offline Kitty Hello

  • code monkey
  • Administrator
  • Prof. Inline
  • *******
  • Posts: 10714
  • here on my island the sea says 'hello'
    • View Profile
    • http://www.glbasic.com
Re: Box2d - 2D physics
« Reply #55 on: 2010-Mar-23 »
I'll add that.. See 1st post after some minutes.

Offline bigsofty

  • Community Developer
  • Prof. Inline
  • ******
  • Posts: 2628
    • View Profile
Re: Box2d - 2D physics
« Reply #56 on: 2010-Mar-30 »
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)

Offline Schranz0r

  • Premium User :)
  • Administrator
  • Prof. Inline
  • *******
  • Posts: 5022
  • O Rly?
    • View Profile
Re: Box2d - 2D physics
« Reply #57 on: 2010-Mar-30 »
Nice idea!
I <3 DGArray's :D

PC:
AMD Ryzen 7 1700 @3.9GHz, 16GB HyperX Fury 3000MHz Ram, ASUS ROG GTX 1060 STRIX 6GB, Windows 10 Pro 64Bit, MSi Tomahawk B350 Mainboard

Offline backslider

  • Community Developer
  • Prof. Inline
  • ******
  • Posts: 892
    • View Profile
Re: Box2d - 2D physics
« Reply #58 on: 2010-Apr-07 »
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

Offline blackway

  • Mr. Polyvector
  • ***
  • Posts: 186
    • View Profile
Re: Box2d - 2D physics
« Reply #59 on: 2010-Jun-24 »
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 !!!