Jitter.Collision.CollisionSystem collisionSystem = new Jitter.Collision.CollisionSystemPersistentSAP(); // SAP = Scan and Prune (good for large scenes, bruteforce might be fine for small scenes too) public PhysicsSystem(Project2Game game) : base(game) { this.game = game; World = new JitterWorld(collisionSystem); // whole_new_world.wav // gravity defaults to -9.8 m.s^-2 // World.Gravity = new JVector(0f, -20, 0); accuracy = PersistentStateManager.physicsAccuracy; // lower this for higher FPS (accuracy = 1 still seems to work okay, it's just not ideal) }
public PhysicsCharacterController(JitterWorld world, RigidBody body) : base(body, null) { this.World = world; this.JumpVelocity = 0.5f; this.Stiffness = 0.02f; // determine the position of the feets of the character // this can be done by supportmapping in the down direction. // (furthest point away in the down direction) JVector vec = JVector.Down; JVector result = JVector.Zero; // Note: the following works just for normal shapes, for multishapes (compound for example) // you have to loop through all sub-shapes -> easy. body.Shape.SupportMapping(ref vec, out result); // feet position is now the distance between body.Position and the feets // Note: the following '*' is the dot product. feetPosition = result * JVector.Down; }
public void UpdateWorld(JitterWorld world, IGameContext gameContext, IUpdateContext updateContext) { world.Step((float)gameContext.GameTime.ElapsedGameTime.TotalSeconds, true); }
public void Update(ComponentizedEntity entity, IGameContext gameContext, IUpdateContext updateContext) { if (!Enabled) { return; } if (_jitterWorld != _physicsEngine.GetInternalPhysicsWorld()) { // TODO: Deregister rigid bodies from old world. if (_jitterWorld != null && _physicsControllerConstraint != null) { _jitterWorld.RemoveConstraint(_physicsControllerConstraint); _physicsControllerConstraint = null; } _jitterWorld = _physicsEngine.GetInternalPhysicsWorld(); } if (_physicalComponent.RigidBodies.Length > 0 && _physicalComponent.RigidBodies[0] != _rigidBody) { if (_physicsControllerConstraint != null) { _jitterWorld.RemoveConstraint(_physicsControllerConstraint); _physicsControllerConstraint = null; } } if (_physicalComponent.RigidBodies.Length > 0) { if (_physicsControllerConstraint == null) { _physicsControllerConstraint = new PhysicsControllerConstraint( _jitterWorld, _physicalComponent.RigidBodies[0]); _jitterWorld.AddConstraint(_physicsControllerConstraint); } _physicsControllerConstraint.TargetVelocity = TargetVelocity.ToJitterVector(); _physicsControllerConstraint.TryJump = TryJump; _physicsControllerConstraint.JumpVelocity = JumpVelocity; _physicsControllerConstraint.Stiffness = Stiffness; if (TargetVelocity.LengthSquared() > 0f) { // Wake up the rigid body. _physicalComponent.RigidBodies[0].IsActive = true; } } }
public PhysicsControllerConstraint(JitterWorld world, RigidBody body) : base(body, null) { _world = world; var vec = JVector.Down; var result = JVector.Zero; body.Shape.SupportMapping(ref vec, out result); _feetPosition = result*JVector.Down; }
protected void RecalculateObject(IGameContext gameContext, IRenderContext renderContext) { // Recalculate vertex caches. this.m_CachedHitTestVertexPositionNormalTextures = this.CalculateVertexPositionNormalTextures(true).Select(x => x.Position).ToArray(); this.m_CachedNoHitTestVertexPositionNormalTextures = this.CalculateVertexPositionNormalTextures(false); // Recalculate vertex and index buffers. if (this.m_IndexBuffer != null) { this.m_IndexBuffer.Dispose(); } if (this.m_VertexBuffer != null) { this.m_VertexBuffer.Dispose(); } this.m_IndexBuffer = new IndexBuffer( renderContext.GraphicsDevice, typeof(short), this.MeshIndicies.Length, BufferUsage.None); this.m_IndexBuffer.SetData(this.MeshIndicies); var vertexes = this.m_CachedNoHitTestVertexPositionNormalTextures; this.m_VertexBuffer = new VertexBuffer( renderContext.GraphicsDevice, typeof(VertexPositionNormalTexture), vertexes.Length, BufferUsage.None); this.m_VertexBuffer.SetData(vertexes); // Recalculate physics object. var world = gameContext.World as RoomEditorWorld; if (world != null) { if (this.m_RigidBody != null) { this.m_JitterWorld.RemoveBody(this.m_RigidBody); } var shape = new ConvexHullShape(this.GetVertexes().Select(x => x.ToJitterVector()).ToList()); this.m_JitterWorld = world.JitterWorld; this.m_RigidBody = new RigidBody(shape) { IsStatic = true, Position = new JVector(0, 0, 0) - shape.Shift }; // The shape vertexes include X, Y, Z position offset, so // the shift moves the object completely into the correct // position. this.m_JitterWorld.AddBody(this.m_RigidBody); this.m_PendRecalculation = false; } else { // We can't update the physics entity, so pend until we can. this.m_PendRecalculation = true; } }
public void OnDelete() { if (this.m_JitterWorld != null && this.m_RigidBody != null) { this.m_JitterWorld.RemoveBody(this.m_RigidBody); this.m_JitterWorld = null; this.m_RigidBody = null; } if (this.Deleted != null) { this.Deleted(this, new EventArgs()); } }
public RoomEditorWorld( IFactory factory, I2DRenderUtilities twoDRenderUtilities, IAssetManagerProvider assetManagerProvider, IPhysicsEngine physicsEngine, IRoomTool[] roomTools, ShipEditorWorld previousWorld, Room room) { this.PreviousWorld = previousWorld; this.Entities = new List<IEntity>(); this.m_2DRenderUtilities = twoDRenderUtilities; this.m_AssetManager = assetManagerProvider.GetAssetManager(); this.m_RoomTools = roomTools; this.m_DefaultFont = this.m_AssetManager.Get<FontAsset>("font.Default"); this.m_LightingEffect = this.m_AssetManager.Get<EffectAsset>("effect.Light"); this.m_LightingEffect.Effect.Parameters["Ambient"].SetValue(new Vector3(0.2f, 0.2f, 0.2f)); this.m_LightingEffect.Effect.Parameters["AmbientRemaining"].SetValue(new Vector3(0.8f, 0.8f, 0.8f)); var ship = factory.CreateShipEntity(); var player = factory.CreatePlayerEntity(); player.ParentArea = ship; player.X = 5; player.Z = 5; this.m_Room = room; foreach (var obj in this.m_Room.Objects) { obj.Reinitalize(); } this.m_RoomEditorEntity = factory.CreateRoomEditorEntity(this.m_Room); this.Entities.Add(ship); this.Entities.Add(player); this.Entities.Add(this.m_RoomEditorEntity); this.m_PhysicsEngine = physicsEngine; this.m_JitterWorld = new JitterWorld( new CollisionSystemSAP { EnableSpeculativeContacts = true }) { Gravity = new JVector(0, -50, 0) }; this.m_JitterWorld.ContactSettings.MaterialCoefficientMixing = ContactSettings.MaterialCoefficientMixingType.TakeMinimum; var shape = new BoxShape(new JVector(2, 2, 2)); var body = new RigidBody(shape); this.m_JitterWorld.AddBody(body); body.Position = new JVector(20, 10, 20); body.IsActive = true; this.m_RigidBody = body; this.SetupRoomPhysics(); }