bool World::makeCollision(const b2Fixture& fix, Collision& c) const { if (!mBounds) return false; if (fix.GetBody() != mBounds) return false; if (fix.GetUserData() == BOUNDS_LEFT_PTR) c.setToWorldBounds(Collision::LEFT); else if (fix.GetUserData() == BOUNDS_TOP_PTR) c.setToWorldBounds(Collision::TOP); else if (fix.GetUserData() == BOUNDS_RIGHT_PTR) c.setToWorldBounds(Collision::RIGHT); else if (fix.GetUserData() == BOUNDS_BOTTOM_PTR) c.setToWorldBounds(Collision::BOTTOM); else return false; return true; }
void Box2DDebugRenderer::drawAABB (const b2Fixture& fixture, const b2Transform& transform) { if (fixture.GetType() == b2Shape::e_circle) { b2CircleShape* shape = (b2CircleShape*)fixture.GetShape(); vertices[0] = shape->m_p; vertices[0] = b2Mul(transform.q, vertices[0]) + transform.p; mLower.Set(vertices[0].x - shape->m_radius, vertices[0].y - shape->m_radius); mUpper.Set(vertices[0].x + shape->m_radius, vertices[0].y + shape->m_radius); // define vertices in ccw fashion... vertices[0].Set(mLower.x, mLower.y); vertices[1].Set(mUpper.x, mLower.y); vertices[2].Set(mUpper.x, mUpper.y); vertices[3].Set(mLower.x, mUpper.y); drawSolidPolygon(vertices, 4, AABB_COLOR); } else if (fixture.GetType() == b2Shape::e_polygon) { b2PolygonShape* shape = (b2PolygonShape*)fixture.GetShape(); int vertexCount = shape->GetVertexCount(); vertices[0] = shape->GetVertex(0); mUpper = mLower = b2Mul(transform, vertices[0]); for (int i = 1; i < vertexCount; i++) { vertices[i] = shape->GetVertex(i); vertices[i] = b2Mul(transform,vertices[i]); mLower.x = std::min(mLower.x, vertices[i].x); mLower.y = std::min(mLower.y, vertices[i].y); mUpper.x = std::max(mUpper.x, vertices[i].x); mUpper.y = std::max(mUpper.y, vertices[i].y); } // define vertices in ccw fashion... vertices[0].Set(mLower.x, mLower.y); vertices[1].Set(mUpper.x, mLower.y); vertices[2].Set(mUpper.x, mUpper.y); vertices[3].Set(mLower.x, mUpper.y); drawSolidPolygon(vertices, 4, AABB_COLOR); } }
void Box2DDebugRenderer::drawShape (const b2Fixture& fixture, const b2Transform& transform, const Color& color) { if (fixture.GetType() == b2Shape::e_circle) { b2CircleShape* circle = (b2CircleShape*)fixture.GetShape(); t = b2Mul(transform, circle->m_p); drawSolidCircle(t, circle->m_radius, transform.q.GetXAxis() , color); } else if (fixture.GetType() == b2Shape::e_edge) { b2EdgeShape* edge = (b2EdgeShape*)fixture.GetShape(); vertices[0] = edge->m_vertex1; vertices[1] = edge->m_vertex2; vertices[0] = b2Mul(transform, vertices[0]); vertices[1] = b2Mul(transform, vertices[1]); drawSolidPolygon(vertices, 2, color); } else if (fixture.GetType() == b2Shape::e_polygon) { b2PolygonShape* chain = (b2PolygonShape*)fixture.GetShape(); int vertexCount = chain->GetVertexCount(); for (int i = 0; i < vertexCount; i++) { vertices[i] = chain->GetVertex(i); vertices[i] = b2Mul(transform, vertices[i]); } drawSolidPolygon(vertices, vertexCount, color); } else if (fixture.GetType() == b2Shape::e_chain) { b2ChainShape* chain = (b2ChainShape*)fixture.GetShape(); int vertexCount = chain->m_count; for (int i = 0; i < vertexCount; i++) { vertices[i] = chain->m_vertices[i]; vertices[i] = b2Mul(transform, vertices[i]); } drawSolidPolygon(vertices, vertexCount, color); } }
//------------------------------------------------------ // create a transformed shape from a body shape //------------------------------------------------------ TPtr<TLMaths::TShape> TLPhysics::GetShapeFromBodyShape(b2Fixture& BodyShape,const TLMaths::TTransform& Transform) { // gr: I figured the accurate/fast verison would transform by box2d. // but if we use the box2D transform, then it's out of date if the // node is disabled(body frozen) and the transform[on the node] is changed as the // body's transform cannot be changed until it's enabled (body is unfrozen). // gr: change this to use box2D when ENABLED and use our node transform when DISABLED. //#define TRANSFORM_BY_BOX2D // b2Body& Body = *BodyShape.GetBody(); b2Shape* pBodyShape = BodyShape.GetShape(); if ( !pBodyShape ) { TLDebug_Break("Fixture missing shape"); return NULL; } if ( BodyShape.GetType() == b2_polygonShape ) { b2PolygonShape& PolyShape = static_cast<b2PolygonShape&>( *pBodyShape ); #ifdef TRANSFORM_BY_BOX2D const b2XForm& Bodyxf = Body.GetXForm(); #endif // get a list of the points TFixedArray<float2,100> Points; for ( s32 p=0; p<PolyShape.GetVertexCount(); p++ ) { #ifdef TRANSFORM_BY_BOX2D // transform by bodys transform b2Vec2 WorldPos = b2Mul( Bodyxf, PolyShape.GetVertex(p) ); // gr: transform by ourtransform for scale? or instead of the box2d one? as box2d lacks scale float2 WorldPos2( WorldPos.x, WorldPos.y ); #else // transform by OUR transform float2 WorldPos2( PolyShape.GetVertex(p).x, PolyShape.GetVertex(p).y ); Transform.Transform( WorldPos2 ); #endif Points.Add( WorldPos2 ); } // create shape return new TLMaths::TShapePolygon2D( Points ); } else if ( BodyShape.GetType() == b2_circleShape ) { b2CircleShape& CircleShape = static_cast<b2CircleShape&>( *pBodyShape ); #ifdef TRANSFORM_BY_BOX2D // transform by bodys transform to put shape in world space const b2XForm& Bodyxf = Body.GetXForm(); b2Vec2 WorldPos = b2Mul( Bodyxf, CircleShape.m_p ); #else // transform by OUR transform float2 WorldPos( CircleShape.m_p.x, CircleShape.m_p.y ); Transform.Transform( WorldPos ); #endif // make circle TLMaths::TSphere2D Circle( float2( WorldPos.x, WorldPos.y ), CircleShape.m_radius ); return new TLMaths::TShapeSphere2D( Circle ); } else if ( BodyShape.GetType() == b2_edgeShape ) { b2EdgeShape& EdgeShape = static_cast<b2EdgeShape&>( *pBodyShape ); // make line #ifdef TRANSFORM_BY_BOX2D // transform by bodys transform to put shape in world space const b2XForm& Bodyxf = Body.GetXForm(); b2Vec2 v1 = b2Mul( Bodyxf, EdgeShape.GetVertex1() ); b2Vec2 v2 = b2Mul( Bodyxf, EdgeShape.GetVertex2() ); TLMaths::TLine2D Line( float2( v1.x, v1.y ), float2( v2.x, v2.y ) ); #else // transform by OUR transform const b2Vec2& v1 = EdgeShape.GetVertex1(); const b2Vec2& v2 = EdgeShape.GetVertex2(); TLMaths::TLine2D Line( float2( v1.x, v1.y ), float2( v2.x, v2.y ) ); Line.Transform( Transform ); #endif return new TLMaths::TShapeLine2D( Line ); } else { TLDebug_Break("Invalid body shape"); } return NULL; }
float2 Pos() { return B2ToFloat2(fixture->GetBody()->GetPosition()); }
~PhysicsObject() { auto body = fixture->GetBody(); body->DestroyFixture(fixture); if (!body->GetFixtureList()) world->DestroyBody(body); if (particle_contacts) delete particle_contacts; }
const b2Vec2 Box2dService::retrieveTopRightVertex(const b2Body &objectBody, const b2Fixture &fixture) { return objectBody.GetWorldPoint(dynamic_cast<const b2PolygonShape*>(fixture.GetShape())->GetVertex(2)); }