Box2d - 2D physics

Previous topic - Next topic

neseir

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


Smashton

 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.

Smashton

Also, Thanks Eirik  :booze:

AMateus

Quote from: Smashton 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

Smashton

Quote
Put this 2 commands somewhere in your project:
LOCAL pts[]
DIM pts[0][0]


Thank You António! :nw:

Kitty Hello

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.

AMateus

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

Kitty Hello

yes. You would need a 1D array of a TYPE Tbox2x_point then.

AMateus

Ah, I see. It's not the end of the world :)

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

Regards,
António

Leos

@ 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!

djtoon

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?

Kitty Hello

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.

djtoon

thank you very much

mentalthink

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

Nathan

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.