GLBasic forum

Codesnippets => Inline / 3rd party => Topic started by: Kitty Hello on 2009-Sep-29

Title: Box2d - 2D physics
Post by: Kitty Hello on 2009-Sep-29
OK, here's Schranz0r's port of Box2D. I extended it a bit so you can debug the shapes, too.

Also, at the very bottom of the file, there's an example ("b_WakeUp") that should get you started how to extend the wrapper. You might need the get/set acceleration and velocity functions as well as the add impact functions. A "is point in shape" function would be handy for picking, too.
It's all there, just wrap it. Ask here if you need help.

You propably need the header pack (which I should update, btw.)
@Schranz0r - where is the header pack?!

Bugfix: b_DestroyBody didn't set pointer to NULL.

[edit]
Updated version 13 oct 2011


[attachment deleted by admin]
Title: Re: Box2d - 2D physics
Post by: Schranz0r on 2009-Sep-29
xD


This one?

http://www.glbasic.com/files/headers.rar
Title: Re: Box2d - 2D physics
Post by: codegit on 2009-Sep-29
This is soooooooooo cooooooooool ....thanks guys.  :good: :good: :good:
Title: Re: Box2d - 2D physics
Post by: Schranz0r on 2009-Sep-29
No Problem  :good:
Title: Re: Box2d - 2D physics
Post by: bigsofty on 2009-Sep-30
well done guys!  :good:

P.S. Will this work on the iPhone?
Title: Re: Box2d - 2D physics - error messages
Post by: Hemlos on 2009-Sep-30
My EditorE.exe is version: GLBasic IDE, Version: 7.115
GLBasic had a long list of errors on the first compile.
So, I installed the header pack.
After installing the header pack, i get a new error message:
Code: GLBasic [Select]
*** Configuration: WIN32 ***
precompiling:
GPC - GLBasic Precompiler V.7.045 SN:ee90b650 - 3D, NET
Wordcount:52 commands
compiling:
*** create process failed
*** FATAL ERROR - Please post this output in the forum
_______________________________________
*** Finished ***
Elapsed: 0.5 sec. Time: 21:08
Build: 0 succeeded.
*** 1 FAILED ***

BTW, this looks really cool.  :good:
Title: Re: Box2d - 2D physics
Post by: Kitty Hello on 2009-Sep-30
oh dear. Shut down, reboot and try again. That sounds really bad.
Title: Re: Box2d - 2D physics
Post by: Schranz0r on 2009-Sep-30
I hope you put all into   "C:\Program Files\GLBasic\Compiler\platform" ?

EDIT:

Look at this, thats cool:

http://www.youtube.com/watch?v=_k87Ybaz8tY
Title: Re: Box2d - 2D physics
Post by: Hemlos on 2009-Sep-30
@Gernot:
I rebooted, it didnt help.

@Schranz0r:
Well yes, I installed the files and allowed overwriting of directories, it asked for each platform.
Afterwards, the error with a long list didnt happen anymore.
A new error message, the one i posted in my last post, is what i get now.
Title: Re: Box2d - 2D physics
Post by: neseir on 2009-Sep-30
I have the same version but did not update the header files. Works fine here. What are the changes in the header files and why should I install them ?

(From compilation of the Box2D project)

Quote
_______________________________________
*** Configuration: WIN32 ***
precompiling:
GPC - GLBasic Precompiler V.7.045 SN:eb010adf - 3D, NET
Wordcount:52 commands
compiling:

linking:
success
_______________________________________
*** Finished ***
Elapsed: 13.2 sec. Time: 14:19
Build: 1 succeeded.
Title: Re: Box2d - 2D physics
Post by: codegit on 2009-Sep-30
I replaced the headers and I still get the following errors.  :'( :'( I would be thankful for any suggestions.  O_O

Code: GLBasic [Select]
In file included from C:/glbasicProjects/GLBasic/Box2D/Source/Common/b2BlockAllocator.cpp:20,
                 from C:\Temp\glbasic\gpc_temp1.cpp:37:
C:/Program Files/GLBasic/Compiler/platform/Win32/Bin/../lib/gcc/mingw32/3.4.5/../../../../include/cstdlib:49:28: bits/c++config.h: No such file or directory
In file included from C:/Program Files/GLBasic/Compiler/platform/Win32/Bin/../lib/gcc/mingw32/3.4.5/../../../../include/bits/stl_algobase.h:70,
                 from C:/Program Files/GLBasic/Compiler/platform/Win32/Bin/../lib/gcc/mingw32/3.4.5/../../../../include/memory:54,
                 from C:/glbasicProjects/GLBasic/Box2D/Source/Common/b2BlockAllocator.cpp:21,
                 from C:\Temp\glbasic\gpc_temp1.cpp:37:
C:/Program Files/GLBasic/Compiler/platform/Win32/Bin/../lib/gcc/mingw32/3.4.5/../../../../include/iosfwd:46:29: bits/c++locale.h: No such file or directory
C:/Program Files/GLBasic/Compiler/platform/Win32/Bin/../lib/gcc/mingw32/3.4.5/../../../../include/iosfwd:47:25: bits/c++io.h: No such file or directory
In file included from C:/Program Files/GLBasic/Compiler/platform/Win32/Bin/../lib/gcc/mingw32/3.4.5/../../../../include/memory:55,
                 from C:/glbasicProjects/GLBasic/Box2D/Source/Common/b2BlockAllocator.cpp:21,
                 from C:\Temp\glbasic\gpc_temp1.cpp:37:
C:/Program Files/GLBasic/Compiler/platform/Win32/Bin/../lib/gcc/mingw32/3.4.5/../../../../include/bits/allocator.h:52:31: bits/c++allocator.h: No such file or directory
In file included from C:/Program Files/GLBasic/Compiler/platform/Win32/Bin/../lib/gcc/mingw32/3.4.5/../../../../include/memory:55,
                 from C:/glbasicProjects/GLBasic/Box2D/Source/Common/b2BlockAllocator.cpp:21,
                 from C:\Temp\glbasic\gpc_temp1.cpp:37:
C:/Program Files/GLBasic/Compiler/platform/Win32/Bin/../lib/gcc/mingw32/3.4.5/../../../../include/bits/allocator.h:80: error: expected template-name before &#39;<&#39; token
C:/Program Files/GLBasic/Compiler/platform/Win32/Bin/../lib/gcc/mingw32/3.4.5/../../../../include/bits/allocator.h:80: error: expected `{&#39; before &#39;<&#39; token
C:/Program Files/GLBasic/Compiler/platform/Win32/Bin/../lib/gcc/mingw32/3.4.5/../../../../include/bits/allocator.h:80: error: expected unqualified-id before &#39;<&#39; token
*** FATAL ERROR - Please post this output in the forum
 
Title: Re: Box2d - 2D physics
Post by: Schranz0r on 2009-Sep-30
Copy all files from -> C:\Program Files\GLBasic\Compiler\platform\Win32\Include\mingw32\bits

To -> C:\Program Files\GLBasic\Compiler\platform\Win32\Include\bits

Hope it fix it!

Or see attachment and place it on -> C:\Program Files\GLBasic\Compiler\platform\Win32

[attachment deleted by admin]
Title: Re: Box2d - 2D physics
Post by: kaotiklabs on 2009-Sep-30
Copy all files from -> C:\Program Files\GLBasic\Compiler\platform\Win32\Include\mingw32\bits
To -> C:\Program Files\GLBasic\Compiler\platform\Win32\Include\bits

Mine was fixed with this. Thanks!
Title: Re: Box2d - 2D physics
Post by: codegit on 2009-Sep-30
MIne also fixed. Thank you, thank you, thank you very much.  =D
Title: Re: Box2d - 2D physics
Post by: Moru on 2009-Sep-30
Look at this, thats cool:

http://www.youtube.com/watch?v=_k87Ybaz8tY

This is cool too:
http://www.youtube.com/watch?v=T-Aplr8nijM&NR=1

Not related but anyway... :-)
Title: Re: Box2d - 2D physics
Post by: Hemlos on 2009-Oct-01
Copy all files from -> C:\Program Files\GLBasic\Compiler\platform\Win32\Include\mingw32\bits

To -> C:\Program Files\GLBasic\Compiler\platform\Win32\Include\bits

Hope it fix it!

Or see attachment and place it on -> C:\Program Files\GLBasic\Compiler\platform\Win32

ah ok i figgured it out. lol
I moved the folder in the box2d/platform to the glbasic/../platform, and voila.
Title: Re: Box2d - 2D physics
Post by: Hemlos on 2009-Oct-01
Pretty cool,

Is this something you are currently working on KH?

Do you plan on adding all those Dynamics functions?
Title: Re: Box2d - 2D physics
Post by: Kitty Hello on 2009-Oct-01
Must I update the headers package!?
Title: Re: Box2d - 2D physics
Post by: Schranz0r on 2009-Oct-01
Yes, would be better Gernot ;)
Title: Re: Box2d - 2D physics
Post by: neseir on 2009-Oct-03
Hi

This is cool. Have created something simular using require etc but I will rather use this implementation. One problem I encountered when I extended the program with the possibility to pick objects. It just freezes for a couple of seconds and then it continues like there have been no problems. Thought first I had messed up the mouse joint but adding some debug statement indicates it happens around the showscreen function. The changes I have done is in the Box2D.gbas and the test program main.gbas (NB have not added functionality to scale the pick function to find the World coordinates. It works in this example since the world is equal to the screen.....).

Tested the program on a different computer and it works fine. Got to be something with the other installation/GFX card driver or something (the one wich works is ATI and the other is NVidia. Have had some strange GL problems earlier on the PC with the NVidia card).

[EDIT]
Just updated the NVidia driver and now it works !  =D

BR
Eirik


[attachment deleted by admin]
Title: Re: Box2d - 2D physics
Post by: freshworks on 2009-Oct-21
Hi,

Just found a blitzmax box2d wrapper, could things probaly make easier ?

http://maxmods.googlecode.com/files/box2d_1_03_src.zip

Cheers,
Freshworks
Title: Re: Box2d - 2D physics
Post by: MrTAToad on 2009-Oct-28
Hows it going for the iPhone ?  =D
Title: Re: Box2d - 2D physics
Post by: bigsofty on 2009-Oct-29
Yes, is anyone working on an iPhone port?
Title: Re: Box2d - 2D physics
Post by: trucidare on 2009-Oct-29
need a working example in glbasic for win to port correctly. Who wants?
Title: Re: Box2d - 2D physics
Post by: bigsofty on 2009-Oct-30
Is anyone actually working on the iPhone port?  :whip: :P

Wish I could help more but my C++ skills are rubbish.

Title: Re: Box2d - 2D physics
Post by: codegit on 2009-Oct-30
Guys, I have not tried it yet on the iPhone. But if it works under win32 it should also work on the iphone as from what I can see its mostly inline c++ code with nothing specific to any platform.  :good:   When I get a chance I will give a try and let everybody know how it pans out.  =D
Title: Re: Box2d - 2D physics
Post by: kaotiklabs on 2009-Oct-30
Great mate, that would be awesome.  =D
Title: Re: Box2d - 2D physics
Post by: bigsofty on 2009-Oct-30
Ooh that sounds promising!   :good:
Title: Re: Box2d - 2D physics
Post by: freshworks on 2009-Nov-06
Hi,

i've tried the example within the "All entries section", but seems that it won't compile. Is there an out of the box sample that works, so i could test this on the iphone ?

http://www.glbasic.com/showroom.php?site=games&game=box2dinterfaceinglbasic&lang=en

When i try to compile the above example it will start with the following missing files (errors):

*** Configuration: WIN32 ***
precompiling:
GPC - GLBasic Precompiler V.7.098 SN:479d16c7 - 3D, NET
"glbox2d.gbas"(35) warning : probably unassigned variable : screenWidth
"glbox2d.gbas"(35) warning : probably unassigned variable : screenHeight
"glbox2d.gbas"(43) warning : probably unassigned variable : mbr
Wordcount:68 commands
compiling:
X:\Iphone\Box2D\sim_box2d.cpp:1:19: stdio.h: No such file or directory
X:\Iphone\Box2D\sim_box2d.cpp:2:20: stdlib.h: No such file or directory
In file included from X:\Iphone\Box2D\Box2D/Include/Box2D.h:34,
                 from X:\Iphone\Box2D\sim_box2d.cpp:4:
X:\Iphone\Box2D\Box2D/Include/../Source/Common/b2Settings.h:22:20: assert.h: No such file or directory
X:\Iphone\Box2D\Box2D/Include/../Source/Common/b2Settings.h:23:18: math.h: No such file or directory
In file included from X:\Iphone\Box2D\Box2D/Include/Box2D.h:34,
                 from X:\Iphone\Box2D\sim_box2d.cpp:4:
X:\Iphone\Box2D\Box2D/Include/../Source/Common/b2Settings.h: In function `float32 b2MixFriction(float32, float32)':
X:\Iphone\Box2D\Box2D/Include/../Source/Common/b2Settings.h:167: error: `sqrtf' was not declared in this scope
In file included from X:\Iphone\Box2D\Box2D/Include/../Source/Collision/Shapes/b2Shape.h:22,
                 from X:\Iphone\Box2D\Box2D/Include/../Source/Collision/Shapes/b2CircleShape.h:22,
                 from X:\Iphone\Box2D\Box2D/Include/Box2D.h:36,



Ettccc...........




Title: Re: Box2d - 2D physics
Post by: Schranz0r on 2009-Nov-07
I can compile for Iphone ;)
Have to wrapp all that stuff into GLBasic so you can use it for each platform that GLBasic support :P
Title: Re: Box2d - 2D physics
Post by: Kitty Hello on 2009-Nov-09
Is it done, yet? :P
Title: Re: Box2d - 2D physics
Post by: MrTAToad on 2009-Nov-10
It might be easier to look into Chipmunk instead - it needs no external references beyond anything in stdlib

One problem :

The Linux 3.3.3 headers dont have stdlib...

Any chance of updating the headers file with those for the WIZ ?
Title: Re: Box2d - 2D physics
Post by: Kitty Hello on 2009-Nov-10
the current version of box2d that Schranz0r is working on depends on nothing. That's very handy.
Title: Re: Box2d - 2D physics
Post by: MrTAToad on 2009-Nov-10
Very!
Title: Re: Box2d - 2D physics
Post by: bigsofty on 2009-Dec-14
Any news on this one?  O_O
Title: Re: Box2d - 2D physics
Post by: Schranz0r on 2009-Dec-14
No news sorry... have no time to wrapp all that stuff...  :'(
Title: Re: Box2d - 2D physics
Post by: Kitty Hello on 2009-Dec-14
Is it "life"? Must be a hard disease. ;)
Title: Re: Box2d - 2D physics
Post by: bigsofty on 2009-Dec-15
No worries, better answer than "cancelled"  :D
Title: Re: Box2d - 2D physics
Post by: Ozden79 on 2010-Mar-10
Any chance that Box2D is going to make it's way to IPhone? :whistle:
Title: Re: Box2d - 2D physics
Post by: Schranz0r on 2010-Mar-10
sure its just math, not more!
Title: Re: Box2d - 2D physics
Post by: Ozden79 on 2010-Mar-11
Any estimation? Any help I can do about it?
Title: Re: Box2d - 2D physics
Post by: Kitty Hello on 2010-Mar-11
See the first post. I was about to implement the joints now. It's not compiling but if you can help, you surely can get rid of these compiler errors, too. I'd like to finish this myself, but I think it's a lot of work, still.
Title: Re: Box2d - 2D physics
Post by: Ozden79 on 2010-Mar-11
Hello Gernot,

I got the file and I see theerrors in the generated temp cpp files. I looked a bit into the GLBasic codes and it seems you are straight converting/injecting the codes of Box2D instead of just importing the functions from it's dll (if it has?), is that right?

In that case, what are the functions that you convert to GLBasic? I would like to really help as one of the games I design needs some kind of physics and having such a feature in GLBasic (on IPhone) would be really great...

Özden
Title: Re: Box2d - 2D physics
Post by: Kitty Hello on 2010-Mar-11
If it compiles, it will compile for iPhone, too. That's why I don't wrap a dll.
Title: Re: Box2d - 2D physics
Post by: Ozden79 on 2010-Mar-11
Is it only the errors in it or does it have anything missing for implementation? I'm trying to understand whether it's something that only needs to be corrected or have anything to be added/enhanced as well... :)
Title: Re: Box2d - 2D physics
Post by: bigsofty 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)&#39;:
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 ***
Title: Re: Box2d - 2D physics
Post by: Kitty Hello on 2010-Mar-12
Oops - Typo.
See 1st post as a fix. Not tested the joints, yet. (needs implementing in the debug, too)
Title: Re: Box2d - 2D physics
Post by: bigsofty on 2010-Mar-12
Wow, this is a lot of fun! Thanks Gernot/Shranz!  :enc:
Title: Re: Box2d - 2D physics
Post by: Ozden79 on 2010-Mar-12
Yep, it compiles and runs the demo, nice work!  =D

Is there anything missing besides than testing joints?
Title: Re: Box2d - 2D physics
Post by: Kitty Hello on 2010-Mar-12
I think with joints we are fully equipped.
Oh, no!! Callbacks for sounds on impact are missing then.
Title: Re: Box2d - 2D physics
Post by: bigsofty 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
Title: Re: Box2d - 2D physics
Post by: bigsofty 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
 
Title: Re: Box2d - 2D physics
Post by: Ozden79 on 2010-Mar-23
Thank you, these are great...

Any chance to merge these into the original files at some point?
Title: Re: Box2d - 2D physics
Post by: bigsofty 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.
Title: Re: Box2d - 2D physics
Post by: codegit on 2010-Mar-23
Important additions. Thanks.  :good:
Title: Re: Box2d - 2D physics
Post by: Kitty Hello on 2010-Mar-23
I'll add that.. See 1st post after some minutes.
Title: Re: Box2d - 2D physics
Post by: bigsofty 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
Title: Re: Box2d - 2D physics
Post by: Schranz0r on 2010-Mar-30
Nice idea!
Title: Re: Box2d - 2D physics
Post by: backslider 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
Title: Re: Box2d - 2D physics
Post by: blackway 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 !!!
Title: Re: Box2d - 2D physics
Post by: Kitty Hello on 2010-Jun-24
Uh-Oh!
I'll try to see what this could be.
Title: Re: Box2d - 2D physics
Post by: MrTAToad on 2010-Jun-24
Great to see that this doesn't need headers :)
Title: Re: Box2d - 2D physics
Post by: blackway on 2010-Jun-27
Hello Gernot!
Any news??
Thank you!!
Title: Re: Box2d - 2D physics
Post by: matchy on 2010-Jun-28
Works well on the iPod Touch and iPad. :)
http://www.youtube.com/watch?v=fksUQYHNHiw
Title: Re: Box2d - 2D physics
Post by: blackway on 2010-Jun-29
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!
Title: Re: Box2d - 2D physics
Post by: 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
 
Title: Re: Box2d - 2D physics
Post by: blackway on 2010-Jul-18
Thanks matchy for the update...
Anybody knows how to fix box to box collision and/or poly to box collision??    :(
Thanks again!
Title: Re: Box2d - 2D physics
Post by: bigsofty on 2010-Jul-20
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
Title: Re: Box2d - 2D physics
Post by: blackway on 2010-Jul-31
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!
Title: Re: Box2d - 2D physics
Post by: mentalthink on 2010-Jul-31
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.
Title: Re: Box2d - 2D physics
Post by: matchy on 2010-Aug-01
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'
..

Title: Re: Box2d - 2D physics
Post by: Kitty Hello on 2010-Aug-09
inline float cosf(float c){return cos(c);}
Title: Re: Box2d - 2D physics
Post by: blackway on 2010-Aug-17
Hi Gernot, sorry for ask again, and again, and again  :P
Any news about box to box collision on iphone?
Thank you!
Title: Re: Box2d - 2D physics
Post by: Kitty Hello on 2010-Aug-19
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.
Title: Re: Box2d - 2D physics
Post by: blackway on 2010-Aug-19
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 (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!
Title: Re: Box2d - 2D physics
Post by: matchy on 2010-Aug-21
The issue could be not GLB related regarding box-to and poly-to collison and I tried setting the those flags in Xcode/Other C Flag and GLB CMP but no difference on compile.
 :(

Adding this to the Box2D.h enabled WinCE compile. It's slow but a quick test it works. Thanks KH.  :booze:
Code: GLBasic [Select]
        inline float cosf(float c){return cos(c);}
        inline float sinf(float c){return sin(c);}
        inline float sqrtf(float c){return sqrt(c);}
        inline float atan2f(float c){return atan2(c,c);}
 


In the meantime I made this cool 3D pool demo (WIP maybe)!  :P :-[ :good:
http://www.youtube.com/watch?v=8vn9uDq0AI8
Title: Re: Box2d - 2D physics
Post by: Kitty Hello on 2010-Aug-26
Didn't you solve the box-box collision bug by making boxes from poly-objects?
Title: Re: Box2d - 2D physics
Post by: blackway on 2010-Aug-26
Hello Gernot!
The bug is box to box, box to poly,  poly to poly,
The only thing that works is circle to everything else  including circle to circle.

Did you read the post in the box2d forum??
That guy had exactly the same problem, under windows run ok but have that bug on the iphone...

Finally he found the problem and solution:

"The ClipVertex objects were not created properly in the engine because of a small change I made. Peculiar how it worked on Visual Studio though. I guess some difference in the compilers..."

I don't understand nothing about ClipVertex objects  and how GLbasic is handling =D  but I guess that there is the problem, maybe you need to change something ...

Gernot, we believe in you!  :good:
Bye!





Title: Re: Box2d - 2D physics
Post by: Kitty Hello on 2010-Aug-26
his option was to put the "--no-idunnowhat" flag in the CMP box for the iPhone platform in project options.
Title: Re: Box2d - 2D physics
Post by: blackway on 2010-Aug-26
Are you talking about this???:

"gcc 4.0 has some bugs with agressive optimization, I suggest you to add the -fno-regmove instruction in OTHER_CFLAGS to avoid weird behavior. gcc 4.2 fix the problem."

That's another guy telling that is a good practice to compile with that flags, but he found the solution before that and the solution was on Clipvertex Object's side.

Matchy tried setting those flags in Xcode/Other C Flag and GLB CMP but no difference on compile.

Bye!
Title: Re: Box2d - 2D physics
Post by: Kitty Hello on 2010-Aug-27
Did he fix box2d and submit the code?
Title: Re: Box2d - 2D physics
Post by: blackway on 2010-Aug-30
Umm I think that he fix his engine if I don´t understood wrong  :blink:
Title: Re: Box2d - 2D physics
Post by: matchy on 2010-Sep-06
The issue with non circle body collision (circle to box/poly is okay) when compiling for iPhone is lurking a dark cloud and needs to be resolved soon.
I tried the "-fno-regmove" flag in both GLB project options; CMP and, as suggested, in OTHER_CFLAG in XCode but neither made any changes.

I afraid to think about Kitty Hello's suggestion was to import the box2d framework into xcode. :(


Title: Re: Box2d - 2D physics
Post by: blackway on 2010-Sep-06
Matchy I agree with you, It's needs to be resolved soon !!
Maybe Kitty can contact Erin Catto to ask about this issue  ;)
Title: Re: Box2d - 2D physics
Post by: monono on 2010-Sep-06
Is there any way to delete a box after creating it? As far as I know there is no function for that in the Box2d.gbas. I tried to do it myself, but how do I include "void b2World::DestroyBody(b2Body* body)"? I have no clue of C++.
Title: Re: Box2d - 2D physics
Post by: backslider on 2010-Sep-07
try this (copy this function into the box2d.gbas):

Code: GLBasic [Select]
FUNCTION b_destroyBody: body%
INLINE
 bWorld->DestroyBody(bBody(body));
ENDINLINE
ENDFUNCTION
 

Hope it works =)
Title: Re: Box2d - 2D physics
Post by: monono on 2010-Sep-07
Nice 1 backslider! It works.

I have the urgend feeling that the port is nearly complete if everybody would insert the parts on their harddrives.

To all the heavy-users:
The ordinary GLB user can´t make any games with this. Until now there wasn´t even a way to delete objects. So please start a community edition where you add your parts. :nw:
Title: Re: Box2d - 2D physics
Post by: backslider on 2010-Sep-08
I will search all box2d functions in the forum, add it to the .gbas of this thread and then I will add it here as "userfriendly" box2d-lib :P

If it´s OK for you :D
I need the lib, too.

cheers

Edit: Now I added all functions from this thread.

edited functions:
 - b_CreateCircle (extended parameters)
 - b_CreateBox    (extended parameters)
 - b_CreatePoly   (extended parameters)

new functions:
 - b_DestroyJoint% (function has now code to destroy joints)
 - b_destroyBody    (delete bodys that you don´t need)

That´s all I can find, but should be enough for GLBasic!?! =)

[attachment deleted by admin]
Title: Re: Box2d - 2D physics
Post by: blackway on 2010-Sep-09
Hello to all....
I was searching and searching :)  and I found this:

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

Matchy, what do you think?
I can't test nothing because I'm at work, If you finally find the solution please share !!!   =D
Title: Re: Box2d - 2D physics
Post by: matchy on 2010-Sep-19
blackway, you thread find has lead me to the solution to iPhone box to box collision!!!  :enc:  :whistle: Simply add -O2 to the cmp. ;)   :good:

I'm thirsty.  :coke: :coke: :coke:
Title: Re: Box2d - 2D physics
Post by: blackway on 2010-Sep-19
That's a very good news for everyone !!!!
I had no time to test because I was outside my country (I was in Brazil) for bussiness.
Now It's time to test !!!
Bye!

Title: Re: Box2d - 2D physics
Post by: Leos on 2010-Sep-29
I loved this port!

Has anyone managed to export a dll wrapper of Box2D to be used separately ?
Title: Re: Box2d - 2D physics
Post by: jaywat on 2010-Oct-12
For info, there is something wrong with the b_WorldDebug implementation in both the original box2d.gpas and the extended version. In the extended version, I cannot even compile a project with it.

In the other version, calling b_WorldDebug anywhere in your loop, before or after the b_Update tick, when an object has been deleted, causes an app crash.  Note, it doesnt even have to be in the same b_Update loop, so working round it by trying to make b_WorldDebug conditional on 'noDeletesThisLoop' doesn't work. There's probably a clue in the fact it still draws deleted objects long after they're deleted, even after multiple calls to b_Update. These deleted objects only seem to get replaced after I create completely new, seperately indexed objects, for some reason.

And I am being extremely, unbelievably careful about my deletes. I never delete more than one object in one b_Update call, and they're all done from stored indexes, and I never inadvertantly call a destroyed object.

Indeed, my code works perfectly, as long as I don't call b_WorldDebug. I assume it's somehow not taking into account deleted pointers and is calling them when it shouldnt.

I'm not a C coder, and don't have the knowledge to fix it. Hence this being for info, mainly for anyone else having huge frustrations with crashing when their code is actually perfectly fine!
Title: Re: Box2d - 2D physics
Post by: Kitty Hello on 2010-Oct-12
post a small example that crashes, please.
Title: Re: Box2d - 2D physics
Post by: jaywat on 2010-Oct-12
post a small example that crashes, please.

Well, I don't really have a 'small example'. I have my game code, which would obscure the issue, and I can't easily just hack out the bits that cause the issue into a coherent example without a lot of work. And like I said, my code works fine without a call to b_WorldDebug.

It was just a heads up, really, for anyone else who finds themselves with crashing code, especially if you're destroying objects... save yourself a lot of headaches and try removing the call to b_WorldDebug as a first step, because your code might be fine.

I'm not going to use GLBasic or Box 2D anyway for this project, because apparently crucial components of box2d such as collision listeners etc have not been implemented in the wrapper, and since sprcoll doesnt evaluate the object at a rotated angle, I can't use that for accurate collision detection. So I don't see that there is any way forward.
Title: Re: Box2d - 2D physics
Post by: jaywat on 2010-Oct-27
I've posted an example below of a few edits I've made to my own box2d.gpas. After reading the Box2D manual and discovering there were a number of features and behaviours I wanted to make use of but weren't implemented in the wrapper, I had a play around. I'm sure it's all simple stuff to most, but I felt quite accomplished getting them to work, since I don't know anything at all about any flavour of C based languages. Hence coding in BASIC!

I figured I'd post the amendments I made in case they might be of use to others. I've supplied default behaviours, so they should work just fine with your existing code without complaining that you haven't supplied enough parameters and without changing the behaviour of your objects.

The extra parameters:

isSensor... A sensor is a shape that detects collision but does not produce a response. You can flag any shape as being a sensor. Sensors may be static or dynamic. Note it won't collide with anything at all! As the manual says, useful for when you need to know when two shapes overlap yet there should be no collision response.

fixedRotation... stops a physical body from being able to rotate. Can be a useful override for certain objects (say, moving platforms) since the default behaviour of box2d is that objects do rotate on collision etc.

groupIndex... You can have all shapes with the same group index ALWAYS collide by setting a POSITIVE group index, or NEVER collide by setting a NEGATIVE group index. Zero is default behaviour. Very useful, because it allows you to override the default collisions of Box2D while still being able to get and set any of Box2D's physical properties for objects. Using 'isSensor' doesn't cut it for this, because then the object wont collide with anything at all, including groundboxes.  I noticed this line was in box2d-extended but commented out. I've personally not found any issues with it.

Incidentally, personally speaking, I've found that ground boxes dont seem to have as good collision detection as actual boxes. I really dont know why, but my objects at even reasonably low velocities will embed themselves into the ground boxes and be unable to move again, even if I use setBullet for supposedly enhanced collision handling. So I now use a static 'ordinary' box (any object is static as long as it has a mass of 0.0) with a positive group index instead of ground boxes, which seems to have eliminated that problem entirely.


Code: GLBasic [Select]
FUNCTION b_CreateBox: mass, friction, restitution, lineardamp,angulardamping, x, y, w, h, isSensor = FALSE, fixedRotation = FALSE, groupIndex = 0
   INLINE
   b2BodyDef def;
   def.position.Set(x,y);
   def.linearDamping = lineardamp;
   def.angularDamping = angulardamping;
   def.fixedRotation = fixedRotation;         // you would need to add this to other shape functions you want to do the same with
   b2Body* pBody = bWorld->CreateBody( &def );
   b2PolygonDef poly;
      if(mass>0.0) poly.density = mass;
      poly.filter.groupIndex = groupIndex;   // and add this!
      poly.friction = friction;
      poly.restitution = restitution;
      poly.isSensor = isSensor;              // and add this!
      poly.SetAsBox(w*.5, h*.5);
      pBody->CreateShape( &poly);
      if(mass>0.0) pBody->SetMassFromShapes();
   return b_NewObject(pBody, bBody);
   ENDINLINE
ENDFUNCTION
 
Title: Re: Box2d - 2D physics
Post by: matchy on 2010-Oct-27
Well done! I've already started doing that where linear and angular damping is very much required.  :D
http://www.glbasic.com/forum/index.php?topic=3561.msg35761#msg35761

Lately I've been using group index and lower and upper angular joint limits for collision  :good: although I couldn't get MaxMotorTorque or MotorSpeed with this version  :zzz:.
Title: Re: Box2d - 2D physics
Post by: Wampus on 2010-Nov-07
Thanks to everyone who has been working and sharing with Box2D to get it working with GLBasic. My GF gave me an idea for a game that truly needs 2D physics so I'll be needing to use Box 2D some time soon.
Title: Re: Box2d - 2D physics
Post by: matchy on 2010-Nov-07
My GF gave me an idea for a game that truly needs 2D physics so I'll be needing to use Box 2D some time soon.

The idea needs 2D physics so you need Box 2D because you GF's concept. Thanks for sharing that vital information.  :D
Title: Re: Box2d - 2D physics
Post by: Wampus on 2010-Nov-08
No problem. Incidentally, I've added you, Matchy, to an email newsletter and Facebook group so you find out first what's happening in the world of my GF having an idea for a game which needs a 2D engine like Box2D. There is also a website and multimedia promotional advertising to come soon – just so everyone knows that my GF had an idea for a game which needs a 2D engine like Box2D. They'll make a major movie about this one day. For now I just have to deal with the crowds of journalists asking me questions about it whenever I try to leave the house.
Title: Re: Box2d - 2D physics
Post by: matchy on 2010-Nov-08
Incidentally, I've added you, Matchy, to an email newsletter and Facebook group...

Um, how?
Title: Re: Box2d - 2D physics
Post by: pinete on 2010-Nov-20
Hi all,
I'm just download some rars posted on this thread but I'm pretty sure they're not updated, as far as I guess, maybe I'm wrong..
Could someone help me sharing a 'complete package'? I mean, a thing that I can deal with it from the very beginning without problems? or at least posting a link to it (or links) :P
Thanks a lot in advance!!
Title: Re: Box2d - 2D physics
Post by: jaywat on 2010-Dec-06
I'm just curious to know if anyone is actually using box2d with GLBasic for anything 'serious'? Somehow I assume not.

Thing is, this box2d wrapper is all very nice and all for just fooling around with, but unless I'm very much missing the point, it's really of no practical use at all without the implementation of box2d contact listeners.

Sure, it physically HANDLES your object-object collisions, but without contact listeners, you can't get any feedback on those collisions. So how, for example, can you play a sound effect or animation? or add to a game score? or know when to destroy an object that was collided with? You can't do any of the normal things you'd expect to have in a game related to collision if you can't detect that a collision happened!

I've tried implementing glbasic sprite collisions, first of all with exact size sprites, but unless the collision just happens to exactly fall on the exact millisecond that your next frame update and collision check happens, it of course never sees that collision. (To be clear, I'm talking about a collision where the two objects will react to each other, ie, bounce off. The kind where contact happens for a fraction of a second. Detecting collisions where one object passes through another sensor object is obviously less of an issue).

I've tried using sprite collisions with a slightly larger sprite as a collision boundary in the hope of having more chance of catching the actual collision. Still unsatisfactory. Still misses at least 25% of collisions because they still don't exactly happen on the frame update you're testing - you're either just about to collide, or just finished colliding. Obviously the accuracy gets less and less the faster your object is moving.

Which is exactly why you need to get realtime feedback from box2d itself... direct feedback from the physics simulation is the only sure fire way of being sure to catch every collision so that you can act on them.

So if you ARE successfully using box2d in a glbasic game or whatever, perhaps you'd share how you're getting round this major problem? I don't even need code, really, if you can clearly describe your method.

I'm assuming it's no trivial matter to implement the contact listeners, or someone would have done it by now, because it must surely be one of the most necessary features of any physics engine. In fact, I simply can't see the point at all of using a physics engine you can't get any useful feedback from.
Title: Re: Box2d - 2D physics
Post by: matchy on 2010-Dec-06
It would just be a matter of porting the contact listener functions over.

For still and moving shapes that collide and play a sound (pool balls), a current simple method is to check for IsSleeping(). Also check for bounce speed and direction change amount to determine the type of response/sound effect. These are really the only way to get collision data from inside the current box2d glb lib. Sometimes it can depend on what is really needed and in this case it doesn't check for walls with zero mass. :(
Title: Re: Box2d - 2D physics
Post by: jaywat on 2010-Dec-06
It would just be a matter of porting the contact listener functions over.

If it's 'just be a matter of porting the contact listener functions', can someone who knows how do it, please? I wouldn't have the first clue how. I just assumed it wasn't a simple task since it hasn't been done in the wrapper, and as I said, surely it's one of the most essential functions of a physics implementation?

a current simple method is to check for IsSleeping()

None of the box2d implementations I've gotten from this thread have a way of interrogating if an object is sleeping? You have allowsleeping and puttosleep... but as far as I can tell, no way of asking if it IS asleep.
Title: Re: Box2d - 2D physics
Post by: jaywat on 2010-Dec-06
Replying to myself here :p

Ok, isSleeping is the same as any other flag interrogation. So the code to implement it is:

Code: GLBasic [Select]
FUNCTION b_IsSleeping%: body%
INLINE
        b2Body* pB = bBody(body); return pB->IsSleeping() ? TRUE:FALSE;
ENDINLINE
ENDFUNCTION
 

And yes, I can now see how I can use that if one of my objects is static. I can explicitly put an object to sleep, then check for it waking up, and play a sound or add a score or whatever accordingly, then put the object back to sleep. So certainly for dynamic->static collisions, that helps, thanks. Unfortunately, that doesn't help at all if you're trying to detect dynamic->dynamic collisions. So even in the case of pool balls... once you've woken the pool ball up by colliding with it, if another ball collides with the now moving ball, you couldn't detect that collision and play a sound.

Title: Re: Box2d - 2D physics
Post by: blackway on 2010-Dec-06
I'm just curious to know if anyone is actually using box2d with GLBasic for anything 'serious'? Somehow I assume not.

Thing is, this box2d wrapper is all very nice and all for just fooling around with, but unless I'm very much missing the point, it's really of no practical use at all without the implementation of box2d contact listeners.

Sure, it physically HANDLES your object-object collisions, but without contact listeners, you can't get any feedback on those collisions. So how, for example, can you play a sound effect or animation? or add to a game score? or know when to destroy an object that was collided with? You can't do any of the normal things you'd expect to have in a game related to collision if you can't detect that a collision happened!

I've tried implementing glbasic sprite collisions, first of all with exact size sprites, but unless the collision just happens to exactly fall on the exact millisecond that your next frame update and collision check happens, it of course never sees that collision. (To be clear, I'm talking about a collision where the two objects will react to each other, ie, bounce off. The kind where contact happens for a fraction of a second. Detecting collisions where one object passes through another sensor object is obviously less of an issue).

I've tried using sprite collisions with a slightly larger sprite as a collision boundary in the hope of having more chance of catching the actual collision. Still unsatisfactory. Still misses at least 25% of collisions because they still don't exactly happen on the frame update you're testing - you're either just about to collide, or just finished colliding. Obviously the accuracy gets less and less the faster your object is moving.

Which is exactly why you need to get realtime feedback from box2d itself... direct feedback from the physics simulation is the only sure fire way of being sure to catch every collision so that you can act on them.

So if you ARE successfully using box2d in a glbasic game or whatever, perhaps you'd share how you're getting round this major problem? I don't even need code, really, if you can clearly describe your method.

I'm assuming it's no trivial matter to implement the contact listeners, or someone would have done it by now, because it must surely be one of the most necessary features of any physics engine. In fact, I simply can't see the point at all of using a physics engine you can't get any useful feedback from.


Hi Jaywat....
I don't know if Roll'n'Fall is anything 'serious' but if you like you can test it (today is free):
http://itunes.apple.com/us/app/rollnfall/id397681449?mt=8 (http://itunes.apple.com/us/app/rollnfall/id397681449?mt=8)


To check collisions I used the "Sprite bigger than the physics object tricky" and I compare the object's speed between last frame and actual frame. It works good, but I think that you are right, we need the contact listener working.

Cheers!


 
Title: Re: Box2d - 2D physics
Post by: matchy on 2010-Dec-06
I tried to have a go at porting but I can't so I settled for temporary solution for now. I would like have more collision data but for now these are just short cuts. That is comparing objects to previous states. As far as pool ball collisions it possible to detect and compare shape velocities will reveal a moving collision.

Title: Re: Box2d - 2D physics
Post by: jaywat on 2010-Dec-06
I would like have more collision data but for now these are just short cuts. That is comparing objects to previous states. As far as pool ball collisions it possible to detect and compare shape velocities will reveal a moving collision.

I'm not sure I follow? Are you essentially saying, assuming our pool example, to check each ball each frame and decide that 'if I just significantly changed speed or direction, assume something else hit me'?

Title: Re: Box2d - 2D physics
Post by: jaywat on 2010-Dec-06
Hi Jaywat....
I don't know if Roll'n'Fall is anything 'serious' but if you like you can test it (today is free):
http://itunes.apple.com/us/app/rollnfall/id397681449?mt=8 (http://itunes.apple.com/us/app/rollnfall/id397681449?mt=8)

Just downloaded. Very nice! Your accelerometer control feels just right. I tend to dislike accelerometer controlled games in general cuz they're always either way too sensitive and the tiniest unintentional movement screws up your control... or they're not sensitive enough and you have to turn the device nearly upside down to move! :)
Title: Re: Box2d - 2D physics
Post by: blackway on 2010-Dec-06
Thanks Jaywat!  :good:
If you play it a bit you'll see how barrels and boxes does a sound when fall and hit a platform or how a barrel crush a enemy...
Bye!

Title: Re: Box2d - 2D physics
Post by: matchy on 2010-Dec-06
I would like have more collision data but for now these are just short cuts. That is comparing objects to previous states. As far as pool ball collisions it possible to detect and compare shape velocities will reveal a moving collision.

I'm not sure I follow? Are you essentially saying, assuming our pool example, to check each ball each frame and decide that 'if I just significantly changed speed or direction, assume something else hit me'?

It's my pool example and you're not meant to follow me. Post sample code and we can work on that!
Title: Re: Box2d - 2D physics
Post by: jaywat on 2010-Dec-07
It's my pool example and you're not meant to follow me. Post sample code and we can work on that!

Really? Seems a bit pointless. I'd have to write code especially, which would ultimately be of no particular value to me or anyone else, when we could surely just talk in terms of the basic principle of how you would handle collisions between ANY two dynamic and currently moving objects without contact listeners, which would surely be more useful for anyone else who is interested in the future.

As I said, isSleeping wouldn't work where a pool ball has already been struck, has woken and is now moving, if it should then collide with another moving ball. Or if a character collides with a moving platform, or one race car that collides with another, etc etc, if you want to be strangely protective of your pool ball analogy. So interpreting what you said subsequent to that, I was merely asking if you were stating that, as a general principle, you would monitor each object each frame, and assume that any rapid deviation of speed or angle must have been caused by a collision? Or did you mean something else entirely that I haven't understood?

If you're saying that it depends what I want to do as to how it should be handled, and that's why you requested I post code, then I'd counter that this is precisely why it's pretty much essential that contact listeners are implemented for box2d to be of any use!.. they are the default, one-size-fits-all solution. And then we wouldn't have to be talking around all these less than perfect workarounds!

Title: Re: Box2d - 2D physics
Post by: jaywat on 2011-Jan-06
Seems I'm the only one interested in box2d these days! Though hopefully this will catch the eye of someone who can answer my (hopefully very basic) C related question.

I've added motors and limit angles to joints in the GLBasic wrapper. Yay me. But being a complete non-programmer type, I can't get my head round returning references across classes. I was wondering if anyone here could give me a heads up (see below for specifics), cuz if I can solve this little issue I think I can add a heck of a lot more to the box2d wrapper functionality.

Ok. Here's the thing. I can reference a DIRECT definition of 'class b2Joint', such as the joint type, with a function such as the following:

Code: GLBasic [Select]
FUNCTION b_JointGetType: joint%

INLINE
        b2Joint* pB = bJoints(joint); return pB->GetType();
ENDINLINE

ENDFUNCTION
 

but what I need to know is how to reference a definition specific to 'class b2RevoluteJoint : public b2Joint', such as IsMotorEnabled() ?

I can't just express:

Code: GLBasic [Select]
b2RevoluteJoint* pB = bJoints(joint);

because it tells me error: invalid conversion from `b2Joint*' to `b2RevoluteJoint*'.

I can't say:

Code: GLBasic [Select]
b2Joint* pB = bJoints(joint); return pB->IsMotorEnabled();

because it tells me error: 'class b2Joint' has no member named 'IsMotorEnabled', and since I don't know anything about C, now I'm out of guesses and am stuck! ;)

So if anyone could post the INLINE for the as yet non existent function b_JointIsMotorEnabled: joint% to show me how to correctly reference b2RevoluteJoint, I'm pretty sure that following this example there's a whole raft of extra functionality I could add to the box2d wrapper.

I realise there's potentially an issue with 'what if the joint isn't one with a motor?', but I'm not too worried about that right now, since I'm hoping to be able to write the function to use the above gettype(), then interrogate the correct class and return a value conditionally... all I'm really interested in right now is how to return b2RevoluteJoint->IsMotorEnabled in the first place!

Thanks!
Title: Re: Box2d - 2D physics
Post by: matchy on 2011-Jan-06
I think, maybe IsMotorEnabled is in a later version of Box2D.
Title: Re: Box2d - 2D physics
Post by: jaywat on 2011-Jan-06
I think, maybe IsMotorEnabled is in a later version of Box2D.

No, it is in the b2RevoluteJoint.h file that we're using with the current wrapper.

Code: GLBasic [Select]
class b2RevoluteJoint : public b2Joint
{
public:
        /// Is the joint motor enabled?
        bool IsMotorEnabled() const;
}
 

As I understand it, b2RevoluteJoint extends b2Joint, and I know how to reference b2Joint, but not the extended members
Title: Re: Box2d - 2D physics
Post by: Kitty Hello on 2011-Jan-07
If you really, really, really know, your joint "IS" a b2RevoluteJoint, you can do:

Code: GLBasic [Select]
b2RevoluteJoint* pB = (b2RevoluteJoint*)bJoints(joint);
 

You could do an dynamic_cast<b2RevoluteJoint*>(bJoints(joint)) and check if it's NULL.
That would require RTTI enabled, though - don't do that.
Title: Re: Box2d - 2D physics
Post by: AlienMenace on 2011-Mar-01
I have used box2d quite a bit with Darkbasic and am excited that there is at least a partial implementation for GLBasic. I have virtually no C skills whatsoever so trying to extend the wrapper myself has been a head-banging exercise in futility. So, I do have a couple of questions and hopefully somebody will be kind enough to  help me out...

This code was posted in this thread as a replacement for the original b_WorldDebug. It allows the debug drawing to be offset, I was wondering if it were possible to modify it to actually offset the position of the world bodies themselves. DB has a command for something similar to this in it's wrapper called Screen Transform.. I've used it to scroll the world and it has worked really well.

Code: GLBasic [Select]
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, RGB(0,255,0));
                                                        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, RGB(255,255,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));
                                        }
                                        }
                                        break;
                                }
                        }
                }
        ENDINLINE
ENDFUNCTION
 

My second question concerns how to read a vector from GLBasic. This function returns vec.x and vec.y but I have no idea how to store and use the information it is returning..

Code: GLBasic [Select]
FUNCTION b_GetWorldVector AS Tb_Vec2: body%, localx, localy
INLINE
        b2Body* pB = bBody(body);
        b2Vec2 vec = pB->GetWorldVector(b2Vec2(localx, localy));
        Tb_Vec2 out;
        out.x = vec.x; out.y=vec.y; return out;
ENDINLINE
ENDFUNCTION
 

I have a few more headaches I'm trying to figure out but if somebody could help me out with these, I would very much appreciate it.
Thanks
Title: Re: Box2d - 2D physics
Post by: matchy on 2011-Mar-01
Something like:

Code: GLBasic [Select]
TYPE _vec
 x
 y
ENDTYPE

LOCAL vec as _vec

LOCAL body_id = b_CreatePoly(...
LOCAL body_angle

vec.x = b_BodyGetPosX(body_id) // + offset_x
vec.y = b_BodyGetPosY(body_id) // + offset_y
// INC body_angle, 30
b_BodySetMove(body_id, vec.x, vec.y, body_angle)

// LOCAL vec_world AS _vec = b_GetWorldVector(body_id, vec.x, vec.y)
// LOCAL vector_x = vec_world.x
// LOCAL vector_y = vec_world.y
 
Title: Re: Box2d - 2D physics
Post by: mentalthink on 2011-May-09
Hi friends, I´ve got a question about the polygon creator command.

Well I try to make shapes whit more of 7 index on the first dimension of the array, but the code crashes.

It´s limited this command to 7 index (1st Index), or perhaps I made some wrong.

Kinds Regards,
Iván J.

Title: Re: Box2d - 2D physics
Post by: neseir on 2011-May-09
Hi

There are a limitation in the Box2D code that limits the number of polygonvertices to 8. It is located in the b2settings.h file (line 75 : const int32 b2_maxPolygonVertices = 8;)

Regards
Eirik
Title: Re: Box2d - 2D physics
Post by: mentalthink on 2011-May-11
HI neseir, lot of thanks I go to see this.
Thanks  :nw:

Iván J.
Title: Re: Box2d - 2D physics
Post by: quangdx on 2011-Jul-24
Is there a command to destroy/remove objects after they've been created?
Also there doesn't seem to be any commands to manually set the x and y position of an object that has already been created.
Any help here would be great.
Title: Re: Box2d - 2D physics
Post by: backslider on 2011-Jul-25
Hi there,

you should download the library from page 6 in this thread.

The direct link is: http://www.glbasic.com/forum/index.php?action=dlattach;topic=3561.0;attach=2262

Then you can use some "new" functions:

edited functions:
 - b_CreateCircle (extended parameters)
 - b_CreateBox    (extended parameters)
 - b_CreatePoly   (extended parameters)

new functions:
 - b_DestroyJoint% (function has now code to destroy joints)
 - b_destroyBody    (delete bodys that you don´t need)

cheers
Title: Re: Box2d - 2D physics
Post by: Kitty Hello on 2011-Jul-25
wouldn't it be better to put the extended parameters into a seperate function and update "any" shape after creation?
Title: Re: Box2d - 2D physics
Post by: quangdx on 2011-Jul-27
Hi there,

you should download the library from page 6 in this thread.

The direct link is: http://www.glbasic.com/forum/index.php?action=dlattach;topic=3561.0;attach=2262

Then you can use some "new" functions:

edited functions:
 - b_CreateCircle (extended parameters)
 - b_CreateBox    (extended parameters)
 - b_CreatePoly   (extended parameters)

new functions:
 - b_DestroyJoint% (function has now code to destroy joints)
 - b_destroyBody    (delete bodys that you don´t need)

cheers

Many thanks dude,
So for me to manually change the location of an Object at a later point,
I have to destroy the body then recreate it in the new location?
Would be a lot easy to just update the X pos and Y pos.
Title: Re: Box2d - 2D physics
Post by: backslider on 2011-Jul-28
Ah, sorry.

To move an object, you have to add the following function to your box2d library
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
 

Somebody should update the library with all functions in this thread and put it into the first post. :)
Title: Re: Box2d - 2D physics
Post by: bigsofty on 2011-Jul-28
I started to add some features a while back but later programers would alter a copy of the original source and erase my changes in the process. It was like running up a sandy hill... this lib desperately needs an SVN IMHO.  :S
Title: Re: Box2d - 2D physics
Post by: backslider on 2011-Jul-28
*sign*
Title: Re: Box2d - 2D physics
Post by: Kitty Hello on 2011-Jul-28
I implemented all changes (except the parameter on creation extension - I''d like to see that in a seperate function) and updated post #1.
Title: Re: Box2d - 2D physics
Post by: Crivens on 2011-Jul-28
Out of interest I've read box2d is compatible with iOS with a small change to compiler parameters, but what about Android and WebOS?

Cheers
Title: Re: Box2d - 2D physics
Post by: backslider on 2011-Jul-28
With webOS the box2d-wrapper worked fine for me!
On Android I don´t know, but why not. :)
Title: Re: Box2d - 2D physics
Post by: Crivens on 2011-Jul-28
That's interesting to know. Thanks for that!

Cheers
Title: Re: Box2d - 2D physics
Post by: Kitty Hello on 2011-Jul-28
the iOS compiler is broken. The others are pretty up to date so no problems I hope.
Title: Re: Box2d - 2D physics
Post by: neseir on 2011-Jul-29
Hi

Can confirm that the sample and files that are in the first post of this thread (updated 28. july) works on android but I had to add a reference to the Box2D headerfile in the "Project Options/cmp" (-I<path to the box2d files>/Box2D)

Regards
Eirik


[attachment deleted by admin]
Title: Re: Box2d - 2D physics
Post by: AMateus on 2011-Sep-24
Hi,

I have been evaluating the possibility of making a top-down view game. But I need physics, so I start checking on box2d. But being a beginner in box2d I couldn't figure it out.

So, does box2d supports top-down view physics?

Thanks in advance,
António
Title: Re: Box2d - 2D physics
Post by: Kitty Hello on 2011-Sep-26
it's 2D - no matter what direction you put it. If you want topdown, just disable gravity.
Title: Re: Box2d - 2D physics
Post by: AMateus on 2011-Sep-26
it's 2D - no matter what direction you put it. If you want topdown, just disable gravity.

Thanks you Kitty, it works well.

But I have a new, more problematic question... How to control jump in a top down view perspective with box2d?

Thanks in advance,
António
Title: Re: Box2d - 2D physics
Post by: Slydog on 2011-Sep-26
Do you mean how to apply physics to a top-down view jump?
Is the jump towards the camera, and up and away from the 2d surface?
If so, that is no longer 2D!

Of course you could always simulate gravity on your own for jumping.
But you can't get Box2D to respond to your player landing 'on-top' of something, from the top-down perspective.
Since this would be 3D again.
Unless you can apply a force to inside an objects perimeter to simulate something hitting it from the top (or bottom).
Title: Re: Box2d - 2D physics
Post by: AMateus on 2011-Sep-26
Yes, exactly that, and that was what I was afraid of...

I'll have to fake the jump and disable box2d collision (for example) while jumping or something like that =)

Thanks both.

Regards,
António
Title: Re: Box2d - 2D physics
Post by: Slydog on 2011-Sep-26
I would love a 3D physics engine that works with GLB!

There is a port of Xors3D in these forums somewhere, here's the main product's site:
http://xors3d.com/ (http://xors3d.com/)

It is a paid for product, and only available for Windows and iOS.
I'm not sure if it is still being actively developed or not.

Ha, another option is to use Box2D for the main world top-down physics.
Then when you need the player to 'jump', create a new side-view world internally that contains the 'slice' of the 2D world along where the player is facing!  Respond to the physics and reapply the results to the top-down world!
Much easier if you limit the player to move fully up, down, left, or right.
If you allow directional movement, creating that 'slice' involves a lot more math!
Plus it wont account for when the player lands on half of a platform and should fall off or other events just away from that slice.
Should be very easy!   :D
Title: Re: Box2d - 2D physics
Post by: Kitty Hello on 2011-Sep-26
or you just "disable" the object's collision during the jumping, and after the "landing" you remove/destroy all objects that have contact with the player - or destroy the player if on spikes...
Title: Re: Box2d - 2D physics
Post by: AMateus on 2011-Sep-27
Kitty that was my idea :)

Btw, is there a 3d physics engine for all the platforms supported by glb (desktop and mobile)?

Regards,
AMateus
Title: Re: Box2d - 2D physics
Post by: Kitty Hello on 2011-Sep-27
nope.
Title: Re: Box2d - 2D physics
Post by: bigsofty on 2011-Sep-27
I would love a 3D physics engine that works with GLB!

There is a port of Xors3D in these forums somewhere, here's the main product's site:
http://xors3d.com/ (http://xors3d.com/)

It is a paid for product, and only available for Windows and iOS.
I'm not sure if it is still being actively developed or not.

Ha, another option is to use Box2D for the main world top-down physics.
Then when you need the player to 'jump', create a new side-view world internally that contains the 'slice' of the 2D world along where the player is facing!  Respond to the physics and reapply the results to the top-down world!
Much easier if you limit the player to move fully up, down, left, or right.
If you allow directional movement, creating that 'slice' involves a lot more math!
Plus it wont account for when the player lands on half of a platform and should fall off or other events just away from that slice.
Should be very easy!   :D

iXors is already dead, the main programmer has left the project(Xors too long term IMHO), no development has been made since he left, don't waste your money... avoid!

Title: Re: Box2d - 2D physics
Post by: Nathan on 2011-Sep-27
Could anyone help me with this please?  I've tried to get the example in the first post working, but with no luck, when compiling I get the following ...

Code: GLBasic [Select]
C:\Users\Nathan\AppData\Local\Temp\glbasic\gpc_temp1.cpp: In function `DGInt __GLBASIC__::b_BodyGetAngle(DGNat)':
C:\Users\Nathan\AppData\Local\Temp\glbasic\gpc_temp1.cpp:659: error: `RETURN' was not declared in this scope
C:\Users\Nathan\AppData\Local\Temp\glbasic\gpc_temp1.cpp: In function `DGInt __GLBASIC__::b_WorldDebug(DGInt, DGInt, DGInt)':
C:\Users\Nathan\AppData\Local\Temp\glbasic\gpc_temp1.cpp:745: error: expected primary-expression before "int"
C:\Users\Nathan\AppData\Local\Temp\glbasic\gpc_temp1.cpp:745: error: `i' was not declared in this scope
C:\Users\Nathan\AppData\Local\Temp\glbasic\gpc_temp1.cpp:745: error: expected `;' before ')' token
 

Steps I've taken to try and resolve this issue
- Un-install and re-install GLB
- Copy the attachment headers.rar into the "platform" folder
- Copy Include.rar into "Win32\Include\Bits" folder
- Delete the contents of the above Temp\Glbasic folder

And unfortunately each time I still get the above error.  Strangely, on my PC at work, it compiled first time without any errors or even copying those header/include files.  At work I have Windows7 32bit, at home I have Vista x64, could this be the problem?
Thanks.
Title: Re: Box2d - 2D physics
Post by: Kitty Hello on 2011-Sep-28
somehow the box2d.gbas file has made inline code uppercase. I thought that was all fixed, but there might be a way to get it not to work still :(
Please just lowercase all IF, FOR and RETURN in the INLINE code fragments.
Title: Re: Box2d - 2D physics
Post by: Nathan on 2011-Sep-28
somehow the box2d.gbas file has made inline code uppercase. I thought that was all fixed, but there might be a way to get it not to work still :(
Please just lowercase all IF, FOR and RETURN in the INLINE code fragments.
Hi Kitty, thanks for your response.  I thought it might have been something as simple as that, but wasn't sure where the gpc_temp1.cpp was generated from.  I'll try it tonight at home.

It is a weird issue though, like I mentioned above, I downloaded the SAME zip file at work and at home.  At work, it's fine, at home, it errors.  The only difference is at work I use WinZip to unpack the file, at home I use the build in Windows Extractor.  Tonight at home I'll use WinRar or something.
Title: Re: Box2d - 2D physics
Post by: Moebius on 2011-Sep-28
Shouldn't be a problem with what you used to extract the files.  It's a minor bug in the GLBasic editor - it shouldn't capitalise stuff in the 'INLINE' code, but it does sometimes...
Title: Re: Box2d - 2D physics
Post by: Nathan on 2011-Sep-28
Ah I see, thanks for that. :good:
Maybe an issue when the IDE is running on Vista and/or x64 bit OS.  There's a receipe for disaster right there.
Title: Re: Box2d - 2D physics
Post by: Ruidesco on 2011-Sep-28
I don't know if it's some strange behaviour on Vista/x64, but I did notice that too.

While coding and testing the SunVox wrapper, it seemed that whenever I opened the project some of the INLINEs (not all of them, just the first four or five INLINEs) had their IFs, RETURNs and such common keywords automatically uppercased. I suspect it had something to do with parsing the code for bad casing on opening a project, since they were always saved with their correct C lower case.
Title: Re: Box2d - 2D physics
Post by: Kitty Hello on 2011-Sep-28
It's really bad. I have not found the last occourance, yet.
You can set the "auto-capitalize" option for the Editor to "On Save", that should really prevent it.
Title: Re: Box2d - 2D physics
Post by: AMateus on 2011-Oct-03
Hello,

Doing some test with Box2D. Copied the Box2D.gbas, box2d.h and everything in the Source dir from the project in the first page. When compiling give me this error...

Code: GLBasic [Select]
C:\Users\ANTNIO~1\AppData\Local\Temp\glbasic\gpc_temp1.cpp: In function `DGInt __GLBASIC__::b_CreatePoly(DGInt, DGInt, DGInt, DGInt, DGInt, __GLBASIC__::DGIntArray&)':
C:\Users\ANTNIO~1\AppData\Local\Temp\glbasic\gpc_temp1.cpp:492: error: no match for call to `(__GLBASIC__::DGIntArray) (int&, int)'
D:/Program Files (x86)/GLBasic/Compiler/platform/Include/glb.h:402: note: candidates are: DGInt& __GLBASIC__::DGIntArray::operator()(int)
D:/Program Files (x86)/GLBasic/Compiler/platform/Include/glb.h:442: note:                 __GLBASIC__::DGIntArray& __GLBASIC__::DGIntArray::operator()()
C:\Users\ANTNIO~1\AppData\Local\Temp\glbasic\gpc_temp1.cpp:492: error: no match for call to `(__GLBASIC__::DGIntArray) (int&, int)'
D:/Program Files (x86)/GLBasic/Compiler/platform/Include/glb.h:402: note: candidates are: DGInt& __GLBASIC__::DGIntArray::operator()(int)
D:/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
 

If I run the project from the first page everything is ok.

Any ideias?

Thanks,
António
Title: Re: Box2d - 2D physics
Post by: AMateus on 2011-Oct-03
Also, I'm no even using any command from the wrapper.. I've just add the file for future use  :blink:

I'm also doing some tests on the example and something is terribly wrong... sometimes compiles, sometimes don't. Even if I copy my files to the example project and run it sometimes has erros, sometimes don't.. I'm completly lost here... :|

my code for your review (I have a main file - this on -, the box2d wrapper, and a utils library, nothing more):

Code: GLBasic [Select]
GLOBAL SysInfo AS tSystemInfo                                           // Current Platform Info
GLOBAL AppInfo AS tApplicationInfo                                      // Application defined Info

GLOBAL sw%, sh%                                                                         // Screen Resolution
GLOBAL aw%, ah%                                                                         // Application defined resolution

GLOBAL lGameRunning                                                                     // Indicates the game is running. If FALSE then go to the menu
GLOBAL iAppState                                                                        // Indicates in what stage the application is: 1-Intro 2-Menu 3-Game

GLOBAL lDebug                                                                           // Indicates if the application is in debugmode

GOSUB InitializeApplication

doSleep = TRUE

b_InitWorld(0,0, aw%,ah%,  0.0, 10.0, doSleep)

WHILE lGameRunning=TRUE

        b_Update(0.1, 10)
       
        IF lDebug THEN b_WorldDebug()
       
        SHOWSCREEN
WEND

SUB GLB_ON_QUIT:
        b_Cleanup()
ENDSUB

SUB InitializeApplication:

        ALLOWESCAPE TRUE

        SysInfo.GetCurrentResolution(sw%,sh%)
        AppInfo.GetAppResolution(aw%,ah%)

        lGameRunning = TRUE
        iAppState = 1

        lDebug=TRUE
ENDSUB

SUB CloseApplication:
ENDSUB
 

Inside the project dir I copied from the example the source dir, the documentation dir and the Box2D.h file.

Any help?!

Thanks,
AMateus
Title: Re: Box2d - 2D physics
Post by: AMateus on 2011-Oct-03
Some more info...

If I use the example with some commented lines (see bellow) I have compile errors:

Code: GLBasic [Select]
doSleep = TRUE

// Box
DRAWRECT 0,0,32,32,RGB(255,255,255)
GRABSPRITE 0, 0,0,32,32
CLEARSCREEN

DRAWRECT 0,0,320,32,RGB(0,100,255)
GRABSPRITE 1, 0,0,320,32
CLEARSCREEN

b_InitWorld(0,0, 640,480,  0.0, 10.0, doSleep)

//LOCAL box1%, ground%
//box1 = b_CreateBox(1, .2, .5, 301,20,32,32)
//box2 = b_CreateBox(1, 3.2,.5, 347,20,32,32)
//cir1 = b_CreateCircle(1,1,.5, 290,-10,25)
//ground = b_CreateGroundbox(323,300,320,32)
//
//
//DEBUG "box1 "+box1+"\n"
//DEBUG "box2 "+box2+"\n"
//DEBUG "cir1 "+cir1+"\n"
//DEBUG "gnd  "+ground+"\n"
//
//// a triangle
//LOCAL pts[]
//DIM pts[3][2]
//      // polygon points (clockwise!!)
//      //        x            y
//      pts[0][0]=0; pts[0][1]=0
//      pts[1][0]=20;pts[1][1]=15
//      pts[2][0]=0; pts[2][1]=20
//      b_CreatePoly( 1, 2.0, 0.5,  301,60, pts[])
//
//
//
//b_BodySetAngle(box1,47)
//
//b_BodySetAngle(box2,44)

WHILE TRUE
b_Update(0.1, 10)

//      ROTOSPRITE 0, b_BodyGetPosX(box1)-16,    b_BodyGetPosY(box1)-16,   b_BodyGetAngle(box1)
//      ROTOSPRITE 0, b_BodyGetPosX(box2)-16,    b_BodyGetPosY(box2)-16,   b_BodyGetAngle(box2)
//      ROTOSPRITE 1, b_BodyGetPosX(ground)-160, b_BodyGetPosY(ground)-16, b_BodyGetAngle(ground)


        b_WorldDebug() // debug a wireframe of what's happening
SHOWSCREEN
WEND




SUB GLB_ON_QUIT:
        b_Cleanup()
ENDSUB
 

Really don't understand why. I only comment the drawing routines...
Title: Re: Box2d - 2D physics
Post by: AMateus on 2011-Oct-03
Well, one afternoon to find the solution... nevermind the 3 post above =)

António
Title: Re: Box2d - 2D physics
Post by: Kitty Hello on 2011-Oct-06
See update in post #1:
Code: GLBasic [Select]
        // iWhat: 1 - contact, 0 - persistent, -1 - contact released
        IF LEN(b_Collisions[]) THEN INC iStep%
        FOREACH col IN b_Collisions[]
                DEBUG iStep% + "Collision: "+col.iBody1+", "+col.iBody2+" = "+col.iWhat+"\n"
        NEXT
 

Is that of any use?
Title: Re: Box2d - 2D physics
Post by: AMateus on 2011-Oct-06
Oh yes IT IS! =)  :nw: :nw: :nw:

Thank you thank you thank you :)
Title: Re: Box2d - 2D physics
Post by: AMateus on 2011-Oct-13
Hello,

I'm having problems with destroying a body in the Box2D environment.

Everytime I try to b_DestroyBody(wall) I have the following reaction:

1- I do b_DestroyBody(wall) on contact. The wall only disappears AFTER the next collision with a different object happens

2 - The next collisions (variable) the App crashes.

Please see the attachment. The ball will go right and collide with the right wall. The expected behavior is that the wall should disappear right away, but it only disappears in the next collision. Then when it collides with the left wall the app crashes.

I appreciate if someone could help me.

Thanks,
AMateus


[attachment deleted by admin]
Title: Re: Box2d - 2D physics
Post by: AMateus on 2011-Oct-13
Well, following my previous post, I did more tests and it *seems* the problem is in the b_WorldDebug function of the wrapper. I'll have to do more tests (mainly replacing the lines of b_WorldDebug with sprites, cause when I'm prototyping I have only placeholders) to confirm it.

António

Title: Re: Box2d - 2D physics
Post by: AMateus on 2011-Oct-13
Ok guys, I can now confirm that b_WorldDebug crashes the application if b_DestroyBody is used.

It's possible that b_WorldDebug tries to use a body already deleted, but I leave that to the C experts out there.

Regards,
António
Title: Re: Box2d - 2D physics
Post by: Kitty Hello on 2011-Oct-13
Bugfix in 1st post. (Update your browser's cache).
The pointer was not set to NULL in the body list in b_DestroyBody. So sorry...
Title: Re: Box2d - 2D physics
Post by: AMateus on 2011-Oct-13
You're the best :)

Thank you so much Kitty.

António
Title: Re: Box2d - 2D physics
Post by: Smashton on 2011-Oct-14
It's really bad. I have not found the last occourance, yet.
You can set the "auto-capitalize" option for the Editor to "On Save", that should really prevent it.

How do you do that? How do I set this option in my editor. I can't find it. Also are you going to support Mouse Joint? Thank you for your help.
Title: Re: Box2d - 2D physics
Post by: Kitty Hello on 2011-Oct-14
Tools/Options -> Reformat code on compile (or so...) the checkbox.
Title: Re: Box2d - 2D physics
Post by: neseir 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

Title: Re: Box2d - 2D physics
Post by: 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.
Title: Re: Box2d - 2D physics
Post by: Smashton on 2011-Oct-15
Also, Thanks Eirik  :booze:
Title: Re: Box2d - 2D physics
Post by: AMateus 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
Title: Re: Box2d - 2D physics
Post by: Smashton on 2011-Oct-16
Quote
Put this 2 commands somewhere in your project:
LOCAL pts[]
DIM pts[0][0]


Thank You António! :nw:
Title: Re: Box2d - 2D physics
Post by: Kitty Hello 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.
Title: Re: Box2d - 2D physics
Post by: AMateus 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
Title: Re: Box2d - 2D physics
Post by: Kitty Hello on 2011-Oct-17
yes. You would need a 1D array of a TYPE Tbox2x_point then.
Title: Re: Box2d - 2D physics
Post by: AMateus 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
Title: Re: Box2d - 2D physics
Post by: Leos 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 ?
Title: Re: Box2d - 2D physics
Post by: djtoon 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?
Title: Re: Box2d - 2D physics
Post by: Kitty Hello 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.
Title: Re: Box2d - 2D physics
Post by: djtoon on 2011-Nov-03
thank you very much
Title: Re: Box2d - 2D physics
Post by: mentalthink 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
Title: Re: Box2d - 2D physics
Post by: Nathan 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.
Title: Re: Box2d - 2D physics
Post by: mentalthink on 2011-Nov-15
Yes nathan don´t care I post before you, the files are correct.
 :good: :good:
Title: Re: Box2d - 2D physics
Post by: Codeguru on 2011-Dec-25
anyone able to compile the test into the android target ?
i got massive errors not found any header file:

compile+link:
C:\Users\MickyD\AppData\Local\Temp\glbasic\gpc_temp1.cpp:83:19: error: box2d.h: No such file or directory
C:\Users\MickyD\AppData\Local\Temp\glbasic\gpc_temp1.cpp:87:46: error: Source/Common/b2BlockAllocator.cpp: No such file or directory

.....
and a lot more errors
Title: Re: Box2d - 2D physics
Post by: neseir on 2011-Dec-25
Hi

I made it work on Android but I had to add a reference to the header (check this posting earlier in the thread)  :http://www.glbasic.com/forum/index.php?topic=3561.msg53765#msg53765 (http://www.glbasic.com/forum/index.php?topic=3561.msg53765#msg53765)

//Eirik
Title: Re: Box2d - 2D physics
Post by: bigsofty on 2011-Dec-29
Not touched Box2D for a LONG time but I thought I would have a little play with this great lib once again, one thing though before I start a new project. The first post says "Updated version 13 oct 2011", is this the most recent version?
Title: Re: Box2d - 2D physics
Post by: Kitty Hello on 2011-Dec-29
Yes, I think so.
Title: Re: Box2d - 2D physics
Post by: bigsofty on 2011-Dec-29
Ah, cool, thank you.  :good:
Title: Re: Box2d - 2D physics
Post by: msx on 2013-Oct-17
Is there a newer version?. There are many functions that are not implemented. Does anyone has done it?. I am interested in the various types of joints mainly, but any progress is welcome.

Sorry I can not help in the port but I am zero in C + +.

Thanks for everything.  :-[
Title: Re: Box2d - 2D physics
Post by: msx on 2013-Oct-17
Is there a newer version?. There are many functions that are not implemented. Does anyone has done it?. I am interested in the various types of joints mainly, but any progress is welcome.

Sorry I can not help with the port but I am zero in C + +.

Thanks for everything.  :-[
Title: Re: Box2d - 2D physics
Post by: mentalthink on 2013-Oct-18
I think not,but really don't worry about C++, I don't have too much idea and I do my OpenGL Libs in C++...

Basically it's make a function standard of GLbasic,...

function physics:
inline
Here copy the Code of the real C++ code of Box2D,
endinline
endfunction

If you take a look to the another functions you can see the way... perhaps the problem can be with Arrays, and Integers in the C++ pg GLbasic have the pronoum DGInt ...

I think can't be complex but it's time for do it  :whistle: :whistle: