Port b2ConstantVolumeJoint from jbox2d (java) to Box2d 2.3.2

Post your tutorials and samples here.
doananhtai
Posts: 2
Joined: Mon Mar 02, 2015 7:56 pm

Port b2ConstantVolumeJoint from jbox2d (java) to Box2d 2.3.2

Postby doananhtai » Mon Mar 02, 2015 8:13 pm

https://www.dropbox.com/s/39iccklso9jves5/b2ContantVolumeJoint_Cpp.zip?dl=0

https://www.youtube.com/watch?v=2UQqtdGQcL0

-------------
Box2D/Box2D.h
---------
+ #include <Box2D/Dynamics/Joints/b2ConstantVolumeJoint.h>



Box2D/Dynamics/b2Body.h
---------
+ friend class b2ConstantVolumeJoint;



Box2D/Dynamics/Joints/b2Joint.h
---------
+ e_constantVolumeJoint



Box2D/Dynamics/Joints/b2Joint.cpp
---------
+ case e_constantVolumeJoint:
{
void* mem = allocator->Allocate(sizeof(b2ConstantVolumeJoint));
joint = new (mem) b2ConstantVolumeJoint(static_cast<const b2ConstantVolumeJointDef*>(def));
}
break;
...

+ case e_constantVolumeJoint:
allocator->Free(joint, sizeof(b2ConstantVolumeJoint));
break;


---------------------------
Box2D/Dynamics/Joints/
----------------------------
+ b2ConstantVolumeJoint.cpp
+ b2ConstantVolumeJoint.h



--------------------------------------------

Testbed/Tests/TestEntries.cpp
------------------------------
..
+ #include "Blob.h"
...
...
+ {"Blob", Blob::Create},


---------------
Testbed/Tests/
--------------
+ Blob.h

clam61
Posts: 44
Joined: Sun Aug 10, 2008 12:19 am

Re: Port b2ConstantVolumeJoint from jbox2d (java) to Box2d 2

Postby clam61 » Tue May 05, 2015 11:19 pm

Thanks for this. Where did you get Box2d 2.3.2 ?

I am running 2.3.0 as that is the latest version I can find.

When I run I seem to get two extra joints displayed, and I am not sure why. Not sure what type they are, because they don't seem to affect the blob's movement

Image

clam61
Posts: 44
Joined: Sun Aug 10, 2008 12:19 am

Re: Port b2ConstantVolumeJoint from jbox2d (java) to Box2d 2

Postby clam61 » Wed May 06, 2015 10:43 am

Here is a better example that is easier to see. I made a b2ConstantVolumeJoint with only 3 bodies.

There should be 3 joints between the bodies and the b2ConstantVolumeJoint itself, for a total of 4 joints. That is what we see in the statistics.

However on screen we see two other things that look like joints, but apparently aren't.

Image

doananhtai
Posts: 2
Joined: Mon Mar 02, 2015 7:56 pm

Re: Port b2ConstantVolumeJoint from jbox2d (java) to Box2d 2

Postby doananhtai » Thu May 07, 2015 11:49 pm

clam61 wrote:Thanks for this. Where did you get Box2d 2.3.2 ?

I am running 2.3.0 as that is the latest version I can find.

When I run I seem to get two extra joints displayed, and I am not sure why. Not sure what type they are, because they don't seem to affect the blob's movement




Constant Volumne Joint is not the joint between two (or more) bodies (it is the "special joint" making some bodies keep "the constant volume" inside them.
so "drawing" the joint is NO MEANING. ==> don't draw this joint
open b2World.cpp

Code: Select all

...
void b2World::DrawJoint(b2Joint* joint)
{
..
switch (joint->GetType())
{
      ..
       
        //add "don's draw the joint" with the case of constant Volumne Joint (just like in case of e_mouseJoint)
        case e_constantVolumeJoint:
                // don't draw this
                break;

   case e_mouseJoint:
      // don't draw this
      break;


   default:
      m_debugDraw->DrawSegment(x1, p1, color);
      m_debugDraw->DrawSegment(p1, p2, color);
      m_debugDraw->DrawSegment(x2, p2, color);
   }
...

clam61
Posts: 44
Joined: Sun Aug 10, 2008 12:19 am

Re: Port b2ConstantVolumeJoint from jbox2d (java) to Box2d 2

Postby clam61 » Fri May 08, 2015 9:17 am

Thanks. I will try this!

Another issue I have had is sometimes creating a blob (circle shaped blob composed of circle segments like yours), the simulator either slows down due to cpu usage skyrocketing OR the blob has tremendous force from the center almost like an explosion and you see the blob bits going kind of crazy everywhere.

This happens only sometimes despite the same code executing.
The only difference is the x,y of the blob

clam61
Posts: 44
Joined: Sun Aug 10, 2008 12:19 am

Re: Port b2ConstantVolumeJoint from jbox2d (java) to Box2d 2

Postby clam61 » Fri May 08, 2015 1:55 pm

I found a two memory leaks in the code.

In InitVelocityConstraints and SolveVelocityConstraints you need to add

Code: Select all

delete d


at the end of the functions to delete the allocated memory;

It's not apparent unless you have a lot of b2ConstantVolumeJoints on the screen.

Also, if you change these two functions like so:

Code: Select all

b2Vec2 b2ConstantVolumeJoint::GetAnchorA() const
{
    return m_bodyA->GetWorldPoint(b2Vec2(0,0));
}

b2Vec2 b2ConstantVolumeJoint::GetAnchorB() const
{
    return m_bodyB->GetWorldPoint(b2Vec2(0,0));
}


you can get rid of those nasty extra lines being drawn by box2d debug mode without modifying the framework.


Return to “Tutorials and Samples”



Who is online

Users browsing this forum: No registered users and 1 guest