public bool CanJump(RigidBody body) { Body = WorldItems[body]; Jitter.Dynamics.RigidBody resultingBody = null; JVector normal; float fraction; var positions = new JVector[] { new JVector(body.Position.X, body.Position.Y, body.Position.Z), new JVector(body.Position.X + 0.5f, body.Position.Y, body.Position.Z), new JVector(body.Position.X - 0.5f, body.Position.Y, body.Position.Z), new JVector(body.Position.X, body.Position.Y, body.Position.Z - 0.5f), new JVector(body.Position.X, body.Position.Y, body.Position.Z + 0.5f)}; for (int i = 0; i < positions.Length; i++) { bool result = World.CollisionSystem.Raycast(new JVector(positions[i].X, positions[i].Y, positions[i].Z), new JVector(0, -1, 0), RaycastCallback, out resultingBody, out normal, out fraction); if (result && fraction <= 1.3f && Body.LinearVelocity.Y < 0.5f) { return true; } } return false; }
/// <summary> /// This is called when the game should draw itself. /// </summary> /// <param name="gameTime">Provides a snapshot of timing values.</param> protected override void Draw(GameTime gameTime) { if (terrain.RigidBody != null && !tDone) { tDone = true; #if BULLETXNA ((BulletXNAPhysicsComponent)Services.GetService(typeof(BulletXNAPhysicsComponent))).World.AddRigidBody(terrain.RigidBody as BulletXNA.BulletDynamics.RigidBody); #endif #if JITTER Jitter.Dynamics.RigidBody jitterTerrain = terrain.RigidBody as Jitter.Dynamics.RigidBody; jitterTerrain.IsStatic = true; ((JitterPhysicsComponent)Services.GetService(typeof(JitterPhysicsComponent))).World.AddBody(jitterTerrain); #endif } base.Draw(gameTime); spriteBatch.Begin(); spriteBatch.DrawString(font, "Esc - Exit", Vector2.Zero, Color.Gold); spriteBatch.DrawString(font, "F1 - Deferred Debug On/Off", new Vector2(0, font.LineSpacing), Color.Gold); spriteBatch.DrawString(font, "WASD - Translate Camera", new Vector2(0, font.LineSpacing * 2), Color.Gold); spriteBatch.DrawString(font, "Arrow Keys - Translate Camera", new Vector2(0, font.LineSpacing * 3), Color.Gold); spriteBatch.DrawString(font, "Space - Shadows On/Off", new Vector2(0, font.LineSpacing * 4), Color.Gold); spriteBatch.DrawString(font, "NumPad Arrows - Translate Sphere", new Vector2(0, font.LineSpacing * 5), Color.Gold); spriteBatch.DrawString(font, "NumPad 0 - Translate Sphere Up", new Vector2(0, font.LineSpacing * 6), Color.Gold); spriteBatch.DrawString(font, "P - Switch Physics On/Off", new Vector2(0, font.LineSpacing * 7), Color.Gold); spriteBatch.End(); }
public RigidBody(Shape shape) { this.shape = shape; rigidBody = new Jitter.Dynamics.RigidBody(shape.shape); material = new Material(rigidBody.Material); inertia = rigidBody.Inertia; UpdateMass(rigidBody.Mass); }
private void CollisionDetected(JRigidbody body1, JRigidbody body2, JVector point1, JVector point2, JVector normal, float penetration) { var b1 = body1 as Collider.Rigidbody; var b2 = body2 as Collider.Rigidbody; b1.collider.CollisionDetected(b2.collider, point1.ToVector3()); b2.collider.CollisionDetected(b1.collider, point2.ToVector3()); }
private static void CollisionDetected(JRigidBody b0, JRigidBody b1, JVector p0, JVector p1, JVector normal, float depth) { if (b0.Tag is RigidBody r0) { } if (b1.Tag is RigidBody r1) { } }
public void CreatePhysicsObjectJitter() { Color[] md = new Color[verts.Length]; heightMap.GetData <Color>(md); float[,] heightMapData = new float[width, height]; float maxHeight = 30f; int counter = 0; for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { heightMapData[x, y] = (md[counter++].R / 256f) * maxHeight; } } indices = new int[(width - 1) * (height - 1) * 6]; for (int x = 0; x < width - 1; x++) { for (int y = 0; y < height - 1; y++) { indices[(x + y * (width - 1)) * 6] = ((x + 1) + (y + 1) * width); indices[(x + y * (width - 1)) * 6 + 1] = ((x + 1) + y * width); indices[(x + y * (width - 1)) * 6 + 2] = (x + y * width); indices[(x + y * (width - 1)) * 6 + 3] = ((x + 1) + (y + 1) * width); indices[(x + y * (width - 1)) * 6 + 4] = (x + y * width); indices[(x + y * (width - 1)) * 6 + 5] = (x + (y + 1) * width); } } Jitter.Collision.Shapes.TerrainShape terrainShape = new Jitter.Collision.Shapes.TerrainShape(heightMapData, 1f, 1f); Jitter.Dynamics.RigidBody jitterRigidBody = new Jitter.Dynamics.RigidBody(terrainShape); //Position.Y += maxHeight / 2f; Vector3 heightAdjustedPosition = Position; //heightAdjustedPosition.Y -= maxHeight / 2f; jitterRigidBody.Position = JitterObject.ToJitterVector(heightAdjustedPosition); RigidBody = jitterRigidBody; }
public static void RemoveBody(JRigidbody rigidbody) { INSTANCE.world.RemoveBody(rigidbody); }
public static void AddBody(JRigidbody rigidbody) { INSTANCE.world.AddBody(rigidbody); }
internal static void Remove(JRigidBody rb) => JWorld.RemoveBody(rb);
internal static void Add(JRigidBody rb) => JWorld.AddBody(rb);
public virtual void SetUpBulletPhysicsBody() { m_rigidBody = new Jitter.Dynamics.RigidBody(Shape); jitterPhysics.World.AddBody(m_rigidBody); }
public void CreatePhysicsObjectJitter() { Color[] md = new Color[verts.Length]; heightMap.GetData<Color>(md); float[,] heightMapData = new float[width, height]; float maxHeight = 30f; int counter = 0; for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { heightMapData[x,y] = (md[counter++].R / 256f) * maxHeight; } } indices = new int[(width - 1) * (height - 1) * 6]; for (int x = 0; x < width - 1; x++) { for (int y = 0; y < height - 1; y++) { indices[(x + y * (width - 1)) * 6] = ((x + 1) + (y + 1) * width); indices[(x + y * (width - 1)) * 6 + 1] = ((x + 1) + y * width); indices[(x + y * (width - 1)) * 6 + 2] = (x + y * width); indices[(x + y * (width - 1)) * 6 + 3] = ((x + 1) + (y + 1) * width); indices[(x + y * (width - 1)) * 6 + 4] = (x + y * width); indices[(x + y * (width - 1)) * 6 + 5] = (x + (y + 1) * width); } } Jitter.Collision.Shapes.TerrainShape terrainShape = new Jitter.Collision.Shapes.TerrainShape(heightMapData, 1f, 1f); Jitter.Dynamics.RigidBody jitterRigidBody = new Jitter.Dynamics.RigidBody(terrainShape); //Position.Y += maxHeight / 2f; Vector3 heightAdjustedPosition = Position; //heightAdjustedPosition.Y -= maxHeight / 2f; jitterRigidBody.Position = JitterObject.ToJitterVector(heightAdjustedPosition); RigidBody = jitterRigidBody; }
private void AddRigidBodyToWorld(RigidBody item) { // Initialize the shape Jitter.Collision.Shapes.Shape shape = null; // Box shape if (item.Shape is BoxShape) { var shapeInfo = item.Shape as BoxShape; shape = new Jitter.Collision.Shapes.BoxShape(shapeInfo.Size.X, shapeInfo.Size.Y, shapeInfo.Size.Z); } // Capsule shape else if (item.Shape is CapsuleShape) { var shapeInfo = item.Shape as CapsuleShape; shape = new Jitter.Collision.Shapes.CapsuleShape(shapeInfo.Length, shapeInfo.Radius); } // Default shape else { throw new NotImplementedException("The specified shape has not been implemented"); } // Initialize the body with the specified shape var body = new Jitter.Dynamics.RigidBody(shape) { Position = new JVector(item.Position.X, item.Position.Y, item.Position.Z), IsStatic = item.IsStatic, DynamicFriction = 0, Mass = item.Mass }; // Add the body to the world world.AddBody(body); // Map the item to the world item worldItems.Add(item, body); // TODO: DEBUG code only if (item.Id == "Player") { var constraint = new CharacterController(world, body); world.AddConstraint(constraint); item.Tag = constraint; } }
//Jitter.Dynamics.RigidBody testBody; bool RaycastCallback(Jitter.Dynamics.RigidBody body, JVector normal, float fraction) { return(body == testBody); }
internal Body(Jitter.Dynamics.RigidBody rigidBody) { RigidBody = rigidBody; RigidBody.EnableSpeculativeContacts = true; }
public RigidBodyComponent() { Body = new Jitter.Dynamics.RigidBody(new BoxShape(0.0f, 0.0f, 0.0f)); }
private static void CreatePredator(Entity player) { var rigid = new Jitter.Dynamics.RigidBody(new CapsuleShape(2, 0.45f)) { Material = new Jitter.Dynamics.Material() { Restitution = 0, KineticFriction = 40 }, AllowDeactivation = false }; rigid.SetMassProperties(Jitter.LinearMath.JMatrix.Zero, 1.0f, true); player.AddComponent(new RigidBodyComponent(rigid)); //player.AddComponent( // new ConstraintComponent<CharacterController>( // new CharacterController(player.GetComponent<RigidBodyComponent>().Body) // { // MatchVelocity = false // })); //player.AddComponent(new PredatorControls(50, MathHelper.ToRadians(120), TimeSpan.FromSeconds(0.1))); player.AddComponent( new ConstraintComponent<CharacterController>( new CharacterController(player.GetComponent<RigidBodyComponent>().Body) { MatchVelocity = true })); player.AddComponent(new PointLight(10, Color.Blue)); player.AddComponent(new PlayerControls(50, MathHelper.ToRadians(50))); //RigidBodyComponent playerphysics = player.GetPhysicsComponent(); //player.AddComponent(new ParticleEmitter() //{ // Particles = new ParticleStateType[1000], // LifeSpan = 5, // EmitTime = 0.001f, // SpeedGenerator = item => 0.25f * playerphysics.Body.LinearVelocity.ToXNA() + 2 * new Vector3(Rand.TwoSided, Rand.TwoSided, Rand.TwoSided), // Acceleration = new Vector3(0, -0.25f, 0) //}); }