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

Offline neseir

  • Mc. Print
  • *
  • Posts: 21
    • View Profile
Re: Box2d - 2D physics
« Reply #165 on: 2011-Oct-14 »
Regarding grabbing objects I did a implementation some time ago like this :

Added this together with the world definition  :

Code: GLBasic [Select]
b2MouseJoint* m_mouseJoint = NULL;
 

And this to the end of the Box2d.gbas file

Code: GLBasic [Select]
FUNCTION b_GrabBodyAtMouse% : BYREF mx, BYREF my
INLINE
        if (m_mouseJoint != NULL)
        {
                return 0;
        }

        // Make a small box.
        b2Vec2 p(mx, my);
        b2AABB aabb;
        b2Vec2 d;
        d.Set(0.001f, 0.001f);
        aabb.lowerBound = p - d;
        aabb.upperBound = p + d;

        // Query the world for overlapping shapes.
        const int32 k_maxCount = 10;
        b2Shape* shapes[k_maxCount];
        int32 count = bWorld->Query(aabb, shapes, k_maxCount);
        b2Body* body = NULL;
        for (int32 i = 0; i < count; ++i)
        {
                b2Body* shapeBody = shapes[i]->GetBody();
                if (shapeBody->IsStatic() == false && shapeBody->GetMass() > 0.0f)
                {
                        bool inside = shapes[i]->TestPoint(shapeBody->GetXForm(), p);
                        if (inside)
                        {
                                body = shapes[i]->GetBody();
                                break;
                        }
                }
        }

        if (body)
        {
                b2MouseJointDef md;
                md.body1 = bWorld->GetGroundBody();
                md.body2 = body;
                md.target = p;
#ifdef TARGET_FLOAT32_IS_FIXED
                md.maxForce = (body->GetMass() < 16.0)?
                        (1000.0f * body->GetMass()) : float32(16000.0);
#else
                md.maxForce = 1000.0f * body->GetMass();
#endif
                m_mouseJoint = (b2MouseJoint*)bWorld->CreateJoint(&md);
                body->WakeUp();
        }

ENDINLINE
ENDFUNCTION


FUNCTION b_GrabBodyMouseUp:
INLINE
        if (m_mouseJoint)
        {
                bWorld->DestroyJoint(m_mouseJoint);
                m_mouseJoint = NULL;
        }
ENDINLINE
ENDFUNCTION

FUNCTION b_GrabBodyMouseMove : BYREF mx, BYREF my
INLINE
        b2Vec2 p(mx, my);
        if (m_mouseJoint)
        {
                m_mouseJoint->SetTarget(p);
        }
ENDINLINE
 


Before the main loop :
Code: GLBasic [Select]
LOCAL mbl = 0
LOCAL mbr = 0
LOCAL mx = 0
LOCAL my = 0
LOCAL oldMbl
LOCAL oldMbr
LOCAL oldMx
LOCAL oldMy
 

In my main loop I added (before the b_update):

Code: GLBasic [Select]

        oldMbl = mbl
        oldMx = mx
        oldMy = my
        MOUSESTATE mx, my, mbl, mbr
        IF mbl = 1 AND oldMbl = 0
                DEBUG "Grab at : " + mx + ", " + my + "\n"
                b_GrabBodyAtMouse(mx, my)
        ENDIF
        IF mbl = 0 AND oldMbl = 1
                DEBUG "Released at : " + mx + ", " + my + "\n"
                b_GrabBodyMouseUp()
        ENDIF

        IF mx <> oldMx OR my <> oldMy
                b_GrabBodyMouseMove(mx, my)
        ENDIF

 

Regards
Eirik


Offline Smashton

  • Mc. Print
  • *
  • Posts: 4
    • View Profile
Re: Box2d - 2D physics
« Reply #166 on: 2011-Oct-15 »
 C:\Users\SMASHTON\AppData\Local\Temp\glbasic\gpc_temp1.cpp: In function `DGInt __GLBASIC__::b_CreatePoly(DGInt, DGInt, DGInt, DGInt, DGInt, __GLBASIC__::DGIntArray&)':
C:\Users\SMASHTON\AppData\Local\Temp\glbasic\gpc_temp1.cpp:727: error: no match for call to `(__GLBASIC__::DGIntArray) (int&, int)'
C:/Program Files (x86)/GLBasic/Compiler/platform/Include/glb.h:402: note: candidates are: DGInt& __GLBASIC__::DGIntArray::operator()(int)
C:/Program Files (x86)/GLBasic/Compiler/platform/Include/glb.h:442: note:                 __GLBASIC__::DGIntArray& __GLBASIC__::DGIntArray::operator()()
C:\Users\SMASHTON\AppData\Local\Temp\glbasic\gpc_temp1.cpp:727: error: no match for call to `(__GLBASIC__::DGIntArray) (int&, int)'
C:/Program Files (x86)/GLBasic/Compiler/platform/Include/glb.h:402: note: candidates are: DGInt& __GLBASIC__::DGIntArray::operator()(int)
C:/Program Files (x86)/GLBasic/Compiler/platform/Include/glb.h:442: note:                 __GLBASIC__::DGIntArray& __GLBASIC__::DGIntArray::operator()()
*** FATAL ERROR - Please post this output in the forum

Thank you for all your help. I am still getting the same error as AMateus. How do you solve this problem?
I follow your instruction by enabling compiler option but this problem comes back everytime.

Offline Smashton

  • Mc. Print
  • *
  • Posts: 4
    • View Profile
Re: Box2d - 2D physics
« Reply #167 on: 2011-Oct-15 »
Also, Thanks Eirik  :booze:

Offline AMateus

  • Mr. Drawsprite
  • **
  • Posts: 61
    • View Profile
Re: Box2d - 2D physics
« Reply #168 on: 2011-Oct-15 »
C:\Users\SMASHTON\AppData\Local\Temp\glbasic\gpc_temp1.cpp: In function `DGInt __GLBASIC__::b_CreatePoly(DGInt, DGInt, DGInt, DGInt, DGInt, __GLBASIC__::DGIntArray&)':
C:\Users\SMASHTON\AppData\Local\Temp\glbasic\gpc_temp1.cpp:727: error: no match for call to `(__GLBASIC__::DGIntArray) (int&, int)'
C:/Program Files (x86)/GLBasic/Compiler/platform/Include/glb.h:402: note: candidates are: DGInt& __GLBASIC__::DGIntArray::operator()(int)
C:/Program Files (x86)/GLBasic/Compiler/platform/Include/glb.h:442: note:                 __GLBASIC__::DGIntArray& __GLBASIC__::DGIntArray::operator()()
C:\Users\SMASHTON\AppData\Local\Temp\glbasic\gpc_temp1.cpp:727: error: no match for call to `(__GLBASIC__::DGIntArray) (int&, int)'
C:/Program Files (x86)/GLBasic/Compiler/platform/Include/glb.h:402: note: candidates are: DGInt& __GLBASIC__::DGIntArray::operator()(int)
C:/Program Files (x86)/GLBasic/Compiler/platform/Include/glb.h:442: note:                 __GLBASIC__::DGIntArray& __GLBASIC__::DGIntArray::operator()()
*** FATAL ERROR - Please post this output in the forum

Thank you for all your help. I am still getting the same error as AMateus. How do you solve this problem?
I follow your instruction by enabling compiler option but this problem comes back everytime.

Put this 2 commands somewhere in your project:

LOCAL pts[]
DIM pts[0][0]

Regards,
António

Offline Smashton

  • Mc. Print
  • *
  • Posts: 4
    • View Profile
Re: Box2d - 2D physics
« Reply #169 on: 2011-Oct-16 »
Quote
Put this 2 commands somewhere in your project:
LOCAL pts[]
DIM pts[0][0]


Thank You António! :nw:

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 #170 on: 2011-Oct-17 »
it's because FUNCTION b_CreatePoly: mass, friction, restitution, x, y, pts[]
uses a 2D array. Should I make this a TYPE instead? 2D arrays are a slight slowdown for the whole program otherwise.

Offline AMateus

  • Mr. Drawsprite
  • **
  • Posts: 61
    • View Profile
Re: Box2d - 2D physics
« Reply #171 on: 2011-Oct-17 »
Everything to make a faster app. :)

Nevertheless, I use b_CreatePoly extensively. Does the change means that the the way we currently feed the pts[] will change?

Regards,
António

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 #172 on: 2011-Oct-17 »
yes. You would need a 1D array of a TYPE Tbox2x_point then.

Offline AMateus

  • Mr. Drawsprite
  • **
  • Posts: 61
    • View Profile
Re: Box2d - 2D physics
« Reply #173 on: 2011-Oct-17 »
Ah, I see. It's not the end of the world :)

If the application will be faster, then go for it ;)

Regards,
António

Offline Leos

  • Mc. Print
  • *
  • Posts: 22
  • Wrecking Everything
    • View Profile
Re: Box2d - 2D physics
« Reply #174 on: 2011-Oct-18 »
@ neseir !
Man... THANKS A LOT, I was trying to make my own function using b2MouseJoint, but I was totally lost, and everything was going wrong... then when I thought I'd be ok, I couldn't make one variable get seen in the main loop..... and yes, you saved me :D

Btw, how can we declare some variable to be seen through source files? Just so I don't fall in the same mistake next time T_T , I mean, c++ var using INLINE and stuff ?
Leo.

GLBasic fan!

Offline djtoon

  • Dr. Type
  • ****
  • Posts: 290
    • View Profile
Re: Box2d - 2D physics
« Reply #175 on: 2011-Nov-01 »
hi all
anyone have any ideas on how do delete every joint connected to a body??
i try'd:

UNCTION removeAllJoins:body%
INLINE
   b2Body* pb = bBody(body);
for(b2Joint *j = pb->GetJointList()->joint; j; j = j->GetNext())
        {
 

       pb->GetWorld()->DestroyJoint(j);
       
        }



ENDINLINE

ENDFUNCTION

bug it crashs  any ideas?

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 #176 on: 2011-Nov-02 »
try
Code: GLBasic [Select]
INLINE
while(pb->GetJointList()->joint)
    pb->GetWorld()->DestroyJoint(  bp ->GetJointList()->joint);
ENDINLINE

...always delete the first one until there's no first one anymore.

Offline djtoon

  • Dr. Type
  • ****
  • Posts: 290
    • View Profile
Re: Box2d - 2D physics
« Reply #177 on: 2011-Nov-03 »
thank you very much

Offline mentalthink

  • Prof. Inline
  • *****
  • Posts: 3366
  • Integrated Brain
    • View Profile
Re: Box2d - 2D physics
« Reply #178 on: 2011-Nov-15 »
HI for web Admin, the file for download the Box2d file, in the first post, it´s wrong, appears a index.php file but have to be somthing like 2dBox.rar, the file contanins the needed for use Box2d, but if something don´t know how make it, only change the extension file to .RAR

Offline Nathan

  • Mr. Polyvector
  • ***
  • Posts: 116
    • View Profile
Re: Box2d - 2D physics
« Reply #179 on: 2011-Nov-15 »
I've noticed all attachments are now downloading as index.php, its not just restricted to this file.  It possibly first started happening for me when Firefox upgraded itself to the latest version, but I can't confirm that.

The file downloads okay however when using MSIE, I haven't tried any other browsers though.