public void HandleInput(GestureSample gestureSample) { if (gestureSample.Delta.Length() != 0) { fixture.GetBody().ApplyForce(new Vector2(gestureSample.Delta.X / 100.0f, gestureSample.Delta.Y / 100.0f), fixture.GetBody().Position); } }
// Implement contact listener. public override void BeginContact(Contact contact) { Fixture fixtureA = contact.GetFixtureA(); Fixture fixtureB = contact.GetFixtureB(); if (fixtureA == _sensor && fixtureB.GetBody().GetUserData() != null) { _touching[(int)(fixtureB.GetBody().GetUserData())] = true; } if (fixtureB == _sensor && fixtureA.GetBody().GetUserData() != null) { _touching[(int)(fixtureA.GetBody().GetUserData())] = true; } }
void Break() { // Create two bodies from one. Body body1 = _piece1.GetBody(); Vector2 center = body1.GetWorldCenter(); body1.DestroyFixture(_piece2); _piece2 = null; BodyDef bd = new BodyDef(); bd.type = BodyType.Dynamic; bd.position = body1.GetPosition(); bd.angle = body1.GetAngle(); Body body2 = _world.CreateBody(bd); _piece2 = body2.CreateFixture(_shape2, 1.0f); // Compute consistent velocities for new bodies based on // cached velocity. Vector2 center1 = body1.GetWorldCenter(); Vector2 center2 = body2.GetWorldCenter(); Vector2 velocity1 = _velocity + MathUtils.Cross(_angularVelocity, center1 - center); Vector2 velocity2 = _velocity + MathUtils.Cross(_angularVelocity, center2 - center); body1.SetAngularVelocity(_angularVelocity); body1.SetLinearVelocity(velocity1); body2.SetAngularVelocity(_angularVelocity); body2.SetLinearVelocity(velocity2); }
public override void Step(Framework.Settings settings) { base.Step(settings); // Traverse the contact results. Apply a force on shapes // that overlap the sensor. for (int i = 0; i < e_count; ++i) { if (_touching[i] == false) { continue; } Body body = _bodies[i]; Body ground = _sensor.GetBody(); CircleShape circle = (CircleShape)_sensor.GetShape(); Vector2 center = ground.GetWorldPoint(circle._p); Vector2 position = body.GetPosition(); Vector2 d = center - position; if (d.LengthSquared < Alt.Box2D.Settings.b2_epsilon * Alt.Box2D.Settings.b2_epsilon) { continue; } d.Normalize(); Vector2 F = 100.0f * d; body.ApplyForce(F, position); } }
public override float ReportFixture(Fixture fixture, Vec2 point, Vec2 normal, float fraction) { Body body = fixture.GetBody(); if (body.UserData != null) { int index = (int)body.UserData; if (index == 0) { // By returning -1, we instruct the calling code to ignore this fixture // and continue the ray-cast to the next fixture. return(-1.0f); } } Utilities.Assert(m_count < e_maxCount); m_points[m_count] = point; m_normals[m_count] = normal; ++m_count; if (m_count == e_maxCount) { // At this point the buffer is full. // By returning 0, we instruct the calling code to terminate the ray-cast. return(0.0f); } // By returning 1, we instruct the caller to continue without clipping the ray. return(1.0f); }
// Query the world for overlapping shapes. bool GetBodyCallback(Fixture F) { Shape S = F.GetShape(); if (F.GetBody().GetType() != BodyType.Static || includeStatic) { Transform trans; F.GetBody().GetTransform(out trans); bool inside = S.TestPoint(ref trans, mousePVec); if (inside) { B = F.GetBody(); return(false); } } return(true); }
public bool ShouldCollide(Fixture fixtureA, Fixture fixtureB) { Entity ent1 = fixtureA.GetBody().GetUserData() as Entity; Entity ent2 = fixtureB.GetBody().GetUserData() as Entity; if (ent1 == null || ent2 == null) { return(true); } return(ent1.shouldCollide(ent2) && ent2.shouldCollide(ent1)); }
// Implement contact listener. public void EndContact(Contact contact) { Fixture fixtureA = contact.FixtureA; Fixture fixtureB = contact.FixtureB; if (fixtureA == m_sensor) { if (fixtureB.GetBody().UserData != null) { fixtureB.GetBody().UserData = false; } } if (fixtureB == m_sensor) { if (fixtureA.GetBody().UserData != null) { fixtureA.GetBody().UserData = false; } } }
public virtual void MouseDown(Vector2 p) { _mouseWorld = p; if (_mouseJoint != null) { return; } // Make a small box. AABB aabb; Vector2 d = new Vector2(0.001, 0.001); aabb.lowerBound = p - d; aabb.upperBound = p + d; Fixture _fixture = null; // Query the world for overlapping shapes. _world.QueryAABB( (fixtureProxy) => { var fixture = fixtureProxy.fixture; Body body = fixture.GetBody(); if (body.GetType() == BodyType.Dynamic) { bool inside = fixture.TestPoint(p); if (inside) { _fixture = fixture; // We are done, terminate the query. return(false); } } // Continue the query. return(true); }, ref aabb); if (_fixture != null) { Body body = _fixture.GetBody(); MouseJointDef md = new MouseJointDef(); md.bodyA = _groundBody; md.bodyB = body; md.target = p; md.maxForce = 1000.0f * body.GetMass(); _mouseJoint = (MouseJoint)_world.CreateJoint(md); body.SetAwake(true); } }
float ReportFixture(Fixture fixture, Vector2 point, Vector2 normal, float fraction) { if (fraction < last_ray_distance) { //check shadow logic if (((PhysicsObject)fixture.GetBody().GetUserData()).cast_shadow) { last_ray_distance = fraction; } } return(1); }
/// Called for each fixture found in the query AABB. /// @return false to terminate the query. public override bool ReportFixture(Fixture fixture) { if (m_count == e_maxCount) { return(false); } Body body = fixture.GetBody(); Shape shape = fixture.GetShape(); bool overlap = Collision.TestOverlap(shape, 0, m_circle, 0, body.GetTransform(), m_transform); if (overlap) { DrawFixture(fixture); ++m_count; } return(true); }
public void Draw(GameTime gameTime, Camera camera) { Vector2 dif = this.Fixture.GetBody().Position - camera.Target.fixture.GetBody().Position; dif.X *= 480 * camera.Scale.X; dif.Y *= 480 * camera.Scale.Y; Vector2 pos = camera.DrawCenter + dif; Vector2 Center = camera.Target.fixture.GetBody().Position; float coef = GameWorld.ScreenManager.GraphicsDevice.Viewport.Width; Rectangle targetRect = new Rectangle( (int)Math.Round(pos.X), (int)Math.Round(pos.Y), (int)Math.Round(width * 480 * camera.Scale.X), (int)Math.Round(height * 480 * camera.Scale.Y)); if (GameWorld.DebugDraw) { //Matrix.CreateTranslation() PrimitiveBatch.Begin(PrimitiveType.TriangleList); PrimitiveBatch.AddVertex(new Vector2(targetRect.X - targetRect.Width / 2, targetRect.Y - targetRect.Height / 2), Color.Green); PrimitiveBatch.AddVertex(new Vector2(targetRect.X + targetRect.Width / 2, targetRect.Y - targetRect.Height / 2), Color.Green); PrimitiveBatch.AddVertex(new Vector2(targetRect.X + targetRect.Width / 2, targetRect.Y + targetRect.Height / 2), Color.Green); PrimitiveBatch.AddVertex(new Vector2(targetRect.X + targetRect.Width / 2, targetRect.Y + targetRect.Height / 2), Color.Green); PrimitiveBatch.AddVertex(new Vector2(targetRect.X - targetRect.Width / 2, targetRect.Y + targetRect.Height / 2), Color.Green); PrimitiveBatch.AddVertex(new Vector2(targetRect.X - targetRect.Width / 2, targetRect.Y - targetRect.Height / 2), Color.Green); PrimitiveBatch.End(); } else { if (Texture != null) { SpriteBatch.Draw(Texture, targetRect, null, Color.White, Fixture.GetBody().Rotation, textureCenter, SpriteEffects.None, 0); } } Draw(gameTime); }
public void DrawFixture(Fixture fixture) { Color color = Color.FromArgb(245, 245, 150); Transform xf = fixture.GetBody().GetTransform(); switch (fixture.GetShapeType()) { case ShapeType.Circle: { CircleShape circle = (CircleShape)fixture.GetShape(); Vec2 center = Utilities.Mul(xf, circle.m_p); float radius = circle.m_radius; m_debugDraw.DrawCircle(center, radius, color); } break; case ShapeType.Polygon: { PolygonShape poly = (PolygonShape)fixture.GetShape(); int vertexCount = poly.m_count; Utilities.Assert(vertexCount <= Settings._maxPolygonVertices); Vec2[] vertices = new Vec2[Settings._maxPolygonVertices]; for (int i = 0; i < vertexCount; ++i) { vertices[i] = Utilities.Mul(xf, poly.m_vertices[i]); } m_debugDraw.DrawPolygon(vertices, vertexCount, color); } break; default: break; } }
// Implement contact listener. public void BeginContact(Contact contact) { Fixture fixtureA = contact.FixtureA; Fixture fixtureB = contact.FixtureB; if (fixtureA == m_sensor) { object userData = fixtureB.GetBody().UserData; if (userData != null) { userData = true; } } if (fixtureB == m_sensor) { object userData = fixtureA.GetBody().UserData; if (userData != null) { userData = true; } } }
public override float ReportFixture(Fixture fixture, Vec2 point, Vec2 normal, float fraction) { Body body = fixture.GetBody(); if (body.UserData != null) { int index = (int)body.UserData; if (index == 0) { // By returning -1, we instruct the calling code to ignore this fixture // and continue the ray-cast to the next fixture. return(-1.0f); } } m_hit = true; m_point = point; m_normal = normal; // At this point we have a hit, so we know the ray is obstructed. // By returning 0, we instruct the calling code to terminate the ray-cast. return(0.0f); }
void DrawFixture(Fixture fixture) { Color color = new ColorR(0.95f, 0.95f, 0.6f); Alt.Box2D.Transform xf; fixture.GetBody().GetTransform(out xf); switch (fixture.ShapeType) { case ShapeType.Circle: { CircleShape circle = (CircleShape)fixture.GetShape(); Vector2 center = MathUtils.Multiply(ref xf, circle._p); double radius = circle._radius; _debugDraw.DrawCircle(center, radius, color); } break; case ShapeType.Polygon: { PolygonShape poly = (PolygonShape)fixture.GetShape(); int vertexCount = poly._vertexCount; Debug.Assert(vertexCount <= Alt.Box2D.Settings.b2_maxPolygonVertices); FixedArray8 <Vector2> vertices = new FixedArray8 <Vector2>(); for (int i = 0; i < vertexCount; ++i) { vertices[i] = MathUtils.Multiply(ref xf, poly._vertices[i]); } _debugDraw.DrawPolygon(ref vertices, vertexCount, color); } break; } }
public override float ReportFixture(Fixture fixture, Vec2 point, Vec2 normal, float fraction) { Body body = fixture.GetBody(); if (body.UserData != null) { int index = (int)body.UserData; if (index == 0) { // By returning -1, we instruct the calling code to ignore this fixture and // continue the ray-cast to the next fixture. return(-1.0f); } } m_hit = true; m_point = point; m_normal = normal; // By returning the current fraction, we instruct the calling code to clip the ray and // continue the ray-cast to the next fixture. WARNING: do not assume that fixtures // are reported in order. However, by clipping, we can always get the closest fixture. return(fraction); }
/// Called for each fixture found in the query AABB. /// @return false to terminate the query. public bool ReportFixture(Fixture fixture) { if (_count == e_maxCount) { return(false); } Body body = fixture.GetBody(); Shape shape = fixture.GetShape(); Transform xf; body.GetTransform(out xf); bool overlap = AABB.TestOverlap(shape, _circle, ref xf, ref _transform); if (overlap) { DrawFixture(fixture); ++_count; } return(true); }
public override void PreSolve(Contact contact, ref Manifold oldManifold) { base.PreSolve(contact, ref oldManifold); Fixture fixtureA = contact.GetFixtureA(); Fixture fixtureB = contact.GetFixtureB(); if (fixtureA != _platform && fixtureA != _character) { return; } if (fixtureB != _character && fixtureB != _character) { return; } Vector2 position = _character.GetBody().GetPosition(); if (position.Y < _top + _radius - 3.0 * Alt.Box2D.Settings.b2_linearSlop) { contact.SetEnabled(false); } }
void DrawFixture(Fixture fixture) { Color color = new Color(0.95f, 0.95f, 0.6f); Transform xf; fixture.GetBody().GetTransform(out xf); switch (fixture.ShapeType) { case ShapeType.Circle: { CircleShape circle = (CircleShape)fixture.GetShape(); Vector2 center = MathUtils.Multiply(ref xf, circle._p); float radius = circle._radius; _debugDraw.DrawCircle(center, radius, color); } break; case ShapeType.Polygon: { PolygonShape poly = (PolygonShape)fixture.GetShape(); int vertexCount = poly._vertexCount; Debug.Assert(vertexCount <= Settings.b2_maxPolygonVertices); FixedArray8<Vector2> vertices = new FixedArray8<Vector2>(); for (int i = 0; i < vertexCount; ++i) { vertices[i] = MathUtils.Multiply(ref xf, poly._vertices[i]); } _debugDraw.DrawPolygon(ref vertices, vertexCount, color); } break; } }
float fired(Fixture fixture, Vector2 point, Vector2 normal, float fraction) { isFired = true; Body affectedBody = fixture.GetBody(); PolygonShape affectedPolygon = (PolygonShape)fixture.GetShape(); int fixtureIndex = listAfterLaser.IndexOf(affectedBody); if (fixtureIndex == -1) { listAfterLaser.Add(affectedBody); listEntryPoint.Add(point); } else { listExitPoint.Add(point); Vector2 pointCenter = new Vector2((point.X + listEntryPoint[fixtureIndex].X) / 2, (point.Y + listEntryPoint[fixtureIndex].Y) / 2); pointCollideCenter.Add(pointCenter); float rayAngle = (float)Math.Atan2(listEntryPoint[fixtureIndex].Y - point.Y, listEntryPoint[fixtureIndex].X - point.X); int numVt = affectedPolygon.GetVertexCount(); Vector2[] polyVertices = new Vector2[numVt]; for (int i = 0; i < numVt; i++) { polyVertices[i] = affectedPolygon.GetVertex(i); } List <Vector2> newPolyVertices1 = new List <Vector2>(); List <Vector2> newPolyVertices2 = new List <Vector2>(); int currentPoly = 0; bool cutPlace1 = false; bool cutPlace2 = false; for (int i = 0; i < polyVertices.Length; i++) { Vector2 worldPoint = affectedBody.GetWorldPoint(polyVertices[i]); float cutAngel = (float)Math.Atan2(worldPoint.Y - pointCenter.Y, worldPoint.X - pointCenter.X) - rayAngle; //if (cutAngel < Math.PI*-1) //{ // cutAngel += (float)(2 * Math.PI); //} //if (cutAngel > 0 && cutAngel <= Math.PI) //{ // listPoint1.Add(worldPoint); //} //else //{ // listPoint2.Add(worldPoint); //} if (cutAngel < Math.PI * -1) { cutAngel += (float)(2 * Math.PI); } if (cutAngel > 0 && cutAngel <= Math.PI) { if (currentPoly == 2) { cutPlace1 = true; newPolyVertices1.Add(point); newPolyVertices1.Add(listEntryPoint[fixtureIndex]); } newPolyVertices1.Add(worldPoint); currentPoly = 1; } else { if (currentPoly == 1) { cutPlace2 = true; newPolyVertices2.Add(listEntryPoint[fixtureIndex]); newPolyVertices2.Add(point); } newPolyVertices2.Add(worldPoint); currentPoly = 2; } } if (!cutPlace1) { newPolyVertices1.Add(point); newPolyVertices1.Add(listEntryPoint[fixtureIndex]); } if (!cutPlace2) { newPolyVertices2.Add(listEntryPoint[fixtureIndex]); newPolyVertices2.Add(point); } createSlice(convertListToArray(newPolyVertices1), newPolyVertices1.Count); createSlice(convertListToArray(newPolyVertices2), newPolyVertices2.Count); physicsWorld.DestroyBody(affectedBody); } return(1.0f); }
private void AddPair(object proxyUserDataA, object proxyUserDataB) { var proxyA = (FixtureProxy)proxyUserDataA; var proxyB = (FixtureProxy)proxyUserDataB; Fixture fixtureA = proxyA.fixture; Fixture fixtureB = proxyB.fixture; int indexA = proxyA.childIndex; int indexB = proxyB.childIndex; Body bodyA = fixtureA.Body; Body bodyB = fixtureB.Body; // Are the fixtures on the same body? if (bodyA == bodyB) { return; } // TODO_ERIN use a hash table to remove a potential bottleneck when both // bodies have a lot of contacts. // Does a contact already exist? ContactEdge edge = bodyB.GetContactList(); while (edge != null) { if (edge.other == bodyA) { Fixture fA = edge.contact.GetFixtureA(); Fixture fB = edge.contact.GetFixtureB(); int iA = edge.contact.GetChildIndexA(); int iB = edge.contact.GetChildIndexB(); if (fA == fixtureA && fB == fixtureB && iA == indexA && iB == indexB) // A contact already exists. { return; } if (fA == fixtureB && fB == fixtureA && iA == indexB && iB == indexA) // A contact already exists. { return; } } edge = edge.next; } // Does a joint override collision? Is at least one body dynamic? if (bodyB.ShouldCollide(bodyA) == false) { return; } // Check user filtering. if (m_contactFilter != null && m_contactFilter.ShouldCollide(fixtureA, fixtureB) == false) { return; } // Call the factory. var c = Contact.Create(fixtureA, indexA, fixtureB, indexB); if (c == null) { return; } // Contact creation may swap fixtures. fixtureA = c.GetFixtureA(); fixtureB = c.GetFixtureB(); indexA = c.GetChildIndexA(); indexB = c.GetChildIndexB(); bodyA = fixtureA.GetBody(); bodyB = fixtureB.GetBody(); // Insert into the world. c.m_prev = null; c.m_next = m_contactList; if (m_contactList != null) { m_contactList.m_prev = c; } m_contactList = c; // Connect to island graph. // Connect to body A c.m_nodeA.contact = c; c.m_nodeA.other = bodyB; c.m_nodeA.prev = null; c.m_nodeA.next = bodyA.m_contactList; if (bodyA.m_contactList != null) { bodyA.m_contactList.prev = c.m_nodeA; } bodyA.m_contactList = c.m_nodeA; // Connect to body B c.m_nodeB.contact = c; c.m_nodeB.other = bodyA; c.m_nodeB.prev = null; c.m_nodeB.next = bodyB.m_contactList; if (bodyB.m_contactList != null) { bodyB.m_contactList.prev = c.m_nodeB; } bodyB.m_contactList = c.m_nodeB; ++m_contactCount; }