Reputation: 111
I'm making an archer game with LibGdx supported with Box2d. Many of the Box2D tutorials are in C++ so some methods are different with Java.
I want to do is stick the arrow when it collide with a box.I found a tutorial but it is in C++ and I couldn't find a way to solve the issue. Actually it's a Math question. Here is the C++ code
//start with standard positions as for normal arrow creation
b2Vec2 vertices[4];
vertices[0].Set( -1.4f, 0 );
vertices[1].Set( 0, -0.1f );
vertices[2].Set( 0.6f, 0 );
vertices[3].Set( 0, 0.1f );
//now multiply by difference between arrow and target transforms
b2Transform diffTransform = b2MulT( si.targetBody->GetTransform(), si.arrowBody->GetTransform() );
for (int i = 0; i < 4; i++)
vertices[i] = b2Mul(diffTransform, vertices[i]);
b2PolygonShape polygonShape;
polygonShape.Set(vertices, 4);
//create a new fixture in the target body
b2FixtureDef fixtureDef;
fixtureDef.shape = &polygonShape;
fixtureDef.density = 1;
si.targetBody->CreateFixture( &fixtureDef );
//discard the original arrow body
m_world->DestroyBody( si.arrowBody );
The aim is when the arrow collide with a box, create a new arrow fixture add it to the box's body then remove the old arrow body. All done except transformation of arrow.
The problem is there is no b2MulT or b2Mul functions in Java. I want to transform the arrow with a position and angle of the old arrow's values when the collision occurs.
Upvotes: 1
Views: 277
Reputation: 9521
I am not conversant with box2d
but given your program logic and a little bit of search it appears that b2MulT(x,y)
is matrix multiplication of matrix x transpose with matrix y.
xT * y
If Java does not offer you an exact implementation of box2d, search for an efficient Java implementation of matrix class with matrix transpose multiplication.
That should hopefully do the trick.
BTW did you check the java port of box2d - http://www.jbox2d.org/ I think it should have the matrix class implemented- as matrix multiplication is the very basis of all transformations.
Upvotes: 1