public virtual void DoSelfCollision(CollisionDetectedHandler collision) { if (!selfCollision) { return; } JVector point, normal; float penetration; for (int i = 0; i < points.Count; i++) { queryList.Clear(); this.dynamicTree.Query(queryList, ref points[i].boundingBox); for (int e = 0; e < queryList.Count; e++) { Triangle t = this.dynamicTree.GetUserData(queryList[e]); if (!(t.VertexBody1 == points[i] || t.VertexBody2 == points[i] || t.VertexBody3 == points[i])) { if (XenoCollide.Detect(points[i].Shape, t, ref points[i].orientation, ref JMatrix.InternalIdentity, ref points[i].position, ref JVector.InternalZero, out point, out normal, out penetration)) { int nearest = CollisionSystem.FindNearestTrianglePoint(this, queryList[e], ref point); collision(points[i], points[nearest], point, point, normal, penetration); } } } } }
void FixedUpdate() { if (IsTrigger) { JMatrix otherOrientation; JVector otherPosition, point, normal; float penetration; foreach (var body in JPhysics.Bodies) { if (body.Key.Shape is Multishape) { continue; } otherPosition = body.Key.Position; otherOrientation = body.Key.Orientation; bool collide = XenoCollide.Detect(shape, body.Key.Shape, ref orientation, ref otherOrientation, ref position, ref otherPosition, out point, out normal, out penetration); var i = new TriggerInfo { Body = body.Value, Point = point, Normal = normal, Penetration = penetration }; if (collide && !collidingBodies.ContainsKey(body.Key)) { collidingBodies.Add(body.Key, body.Value); if (TriggerEnter != null) { TriggerEnter(i); } } else if (!collide && collidingBodies.ContainsKey(body.Key)) { collidingBodies.Remove(body.Key); if (TriggerExit != null) { TriggerExit(i); } } else if (collide && collidingBodies.ContainsKey(body.Key)) { if (TriggerStay != null) { TriggerStay(i); } } } } }
private void PostStep(float timeStep) { foreach (RigidBody body in physics.World.RigidBodies) { if (body.Shape is Multishape) { continue; // no multishape support! } if (body.IsStaticOrInactive) { continue; // not interested in static bodies like terrain, and inactive objects don't change their trigger state } var collide = physics.World.CollisionSystem.CheckBoundingBoxes(physics.Body, body); if (collide) { var otherPosition = body.Position; var otherOrientation = body.Orientation; var sensorOrientation = physics.Body.Orientation; var sensorPosition = physics.Body.Position; JVector point, normal; float penetration; collide = XenoCollide.Detect(physics.Body.Shape, body.Shape, ref sensorOrientation, ref otherOrientation, ref sensorPosition, ref otherPosition, out point, out normal, out penetration); if (collide && !CollidingBodies.Contains(body)) { // okay, we detected a collision, but it's not already in the list! CollidingBodies.Add(body); if (TriggerEntered != null) { TriggerEntered(this, new SensorEventArgs(Entity.Find(body.Tag.ToString()))); } } } if (!collide && CollidingBodies.Contains(body)) { // okay, we detected no collisions, but is in the list! CollidingBodies.Remove(body); if (TriggerExited != null) { TriggerExited(this, new SensorEventArgs(Entity.Find(body.Tag.ToString()))); } } } }
public static JCollision DetectCollision(RigidBody body1, RigidBody body2) { System.Diagnostics.Debug.Assert(body1 != body2, "body1 == body2"); var position1 = body1.Position; var position2 = body2.Position; var orientation1 = body1.Orientation; var orientation2 = body2.Orientation; JVector point; JVector normal; float penetration; var collisionDetected = XenoCollide.Detect(body1.Shape, body2.Shape, ref orientation1, ref orientation2, ref position1, ref position2, out point, out normal, out penetration); if (collisionDetected == false) { return(null); } return(new JCollision(body1, body2, point.ToVector3(), normal.ToVector3(), penetration)); }
public static IEnumerable <JCollision> DetectCollisions(RigidBody body) { var position1 = body.Position; var orientation1 = body.Orientation; foreach (RigidBody body2 in World.RigidBodies) { if (body == body2) { continue; } var position2 = body2.Position; var orientation2 = body2.Orientation; JVector point; JVector normal; float penetration; var collisionDetected = XenoCollide.Detect(body.Shape, body2.Shape, ref orientation1, ref orientation2, ref position1, ref position2, out point, out normal, out penetration); if (collisionDetected) { yield return(new JCollision(body, body2, point.ToVector3(), normal.ToVector3(), penetration)); } } }
protected override void Update(GameTime gameTime) { // Allows the game to exit if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed) { this.Exit(); } KeyboardState keys = Keyboard.GetState(); JVector moveVector = JVector.Zero; float amountOfMovement = 0.05f; if (keys.IsKeyDown(Keys.Right)) { moveVector.X += amountOfMovement; } if (keys.IsKeyDown(Keys.Left)) { moveVector.X -= amountOfMovement; } if (keys.IsKeyDown(Keys.Down)) { moveVector.Y -= amountOfMovement; } if (keys.IsKeyDown(Keys.Up)) { moveVector.Y += amountOfMovement; } body1.Position += moveVector; body1.Orientation += 0.001f; body2.Orientation -= 0.001f; JMatrix o1 = JMatrix.CreateRotationZ(body1.Orientation); JMatrix o2 = JMatrix.CreateRotationZ(body2.Orientation); JVector pos1 = body1.Position; JVector pos2 = body2.Position; JVector point2; sw.Start(); hit = XenoCollide.Detect(body1.Shape, body2.Shape, ref o1, ref o2, ref pos1, ref pos2, out point, out normal, out penetration); sw.Stop(); ticks = sw.ElapsedTicks; sw.Reset(); DebugDrawer.DrawLine(point, point + normal); //DebugDrawer.DrawPoint(point2); DebugDrawer.DrawPoint(point); DebugDrawer.Color = Color.Red; DebugDrawer.Color = Color.Black; DebugDrawer.DrawLine(JVector.Up, JVector.Down); DebugDrawer.DrawLine(JVector.Left, JVector.Right); body1.DebugDraw(DebugDrawer); body2.DebugDraw(DebugDrawer); if (hit) { var oldPosition = body1.Position; body1.Position += normal; body1.DebugDraw(DebugDrawer); body1.Position = oldPosition; } base.Update(gameTime); }