public static void RunCharacterTest() { Core core = new Core(); StillDesign.PhysX.Scene scene = core.CreateScene(); ControllerManager manager = scene.CreateControllerManager(); CapsuleControllerDescription desc = new CapsuleControllerDescription(2, 10); CapsuleController capsuleController = manager.CreateController <CapsuleController>(desc); BoxShapeDescription boxShapeDesc = new BoxShapeDescription(1, 1, 1); ActorDescription actorDesc = new ActorDescription(boxShapeDesc); actorDesc.BodyDescription = new BodyDescription(1f); Actor actor = scene.CreateActor(actorDesc); //capsuleController.Move( Vector3.Up ); // Update Physics scene.Simulate(1.0f / 60.0f); scene.FlushStream(); scene.FetchResults(SimulationStatus.RigidBodyFinished, true); capsuleController.Move(Vector3.Up); core.Dispose(); }
protected override void ProcessKeyboard(Key[] pressedKeys) { // Create a rotation matrix around the y-axis Matrix rotation = Matrix.RotationAxis(Vector3.UnitY, _rotation); // Compute the forwards and right movement vectors // The controller will move in its own space, so rotate the usual forwards and right vectors Vector3 forward = Vector3.TransformNormal(Vector3.UnitZ, rotation); Vector3 right = Vector3.TransformNormal(Vector3.UnitX, rotation); Vector3 moveDelta = Vector3.Zero; if (pressedKeys.Contains(Key.W)) { moveDelta += forward; } if (pressedKeys.Contains(Key.S)) { moveDelta += -forward; } if (pressedKeys.Contains(Key.A)) { moveDelta += -right; } if (pressedKeys.Contains(Key.D)) { moveDelta += right; } // Normalize the distance vector (as we may of added two or more components together) Vector3 d = Vector3.Normalize(moveDelta); // Move the controller in the intended direction * a speed multiplier _controller.Move(d.AsPhysX() * _controllerSpeed, this.Engine.FrameTime); }
public void Update(TimeSpan elapsed) { Scene.Simulate((float)elapsed.TotalSeconds); Scene.FetchResults(true); _controller.Move(new Vector3(1, 0, 1), elapsed); Console.WriteLine(_controller.Position); }
public static void TestPhysicsError() { Game game1 = new Game(); game1.Run(); /*XNAGame game = new XNAGame(); * bool flag = false; * game.UpdateEvent += delegate * { * if ( flag ) game.Exit(); * flag = true; * }; * * game.Run();*/ using (PhysicsEngine engine = new PhysicsEngine()) { StillDesign.PhysX.Scene scene; CapsuleController controller = null; ControllerManager manager; /* * game.InitializeEvent += * delegate * {*/ engine.Initialize(null); scene = engine.Scene; manager = scene.CreateControllerManager(); CapsuleControllerDescription capsuleControllerDesc = new CapsuleControllerDescription(0.5f, 1); CapsuleController capsuleController = manager.CreateController <CapsuleController>(capsuleControllerDesc); controller = capsuleController; ActorDescription actorDesc; Actor actor; BoxShapeDescription boxShapeDesc = new BoxShapeDescription(1, 1, 1); actorDesc = new ActorDescription(boxShapeDesc); actorDesc.BodyDescription = new BodyDescription(1f); actor = engine.Scene.CreateActor(actorDesc); actor.GlobalPosition = new Vector3(1, 4, 0); controller.Move(Vector3.Up); engine.Update(null); controller.Move(Vector3.Up); /* }; * * game.UpdateEvent += delegate { if (flag) game.Exit(); * flag = true; }; * * game.Run();*/ } }
private void updateBody(float deltaTime) { goalDirection = Mogre.Vector3.ZERO; // we will calculate this if (keyDirection != Mogre.Vector3.ZERO && baseAnim.Type != ChaAnimType.CAT_CUSTOME_BLOCK) { // calculate actually goal direction in world based on player's key directions goalDirection += keyDirection.z * cameraNode.Orientation.ZAxis; goalDirection += keyDirection.x * cameraNode.Orientation.XAxis; goalDirection.y = 0; goalDirection.Normalise(); Quaternion toGoal = entityNode.Orientation.ZAxis.GetRotationTo(goalDirection); // calculate how much the character has to turn to face goal direction float yawToGoal = toGoal.Yaw.ValueDegrees; // this is how much the character CAN turn this frame float yawAtSpeed = yawToGoal / Mogre.Math.Abs(yawToGoal) * deltaTime * TURN_SPEED; // reduce "turnability" if we're in midair if (baseAnim.Type == ChaAnimType.CAT_JUMP_LOOP) { yawAtSpeed *= 0.2f; } // turn as much as we can, but not more than we need to if (yawToGoal < 0) { yawToGoal = System.Math.Min(0, System.Math.Max(yawToGoal, yawAtSpeed)); //yawToGoal = Math::Clamp<Real>(yawToGoal, yawAtSpeed, 0); } else if (yawToGoal > 0) { yawToGoal = System.Math.Max(0, System.Math.Min(yawToGoal, yawAtSpeed)); //yawToGoal = Math::Clamp<Real>(yawToGoal, 0, yawAtSpeed); } entityNode.Yaw(new Degree(yawToGoal)); // move in current body direction (not the goal direction) var dist = deltaTime * RUN_SPEED /* * baseAnim.AnimationState.Weight*/; Mogre.Vector3 displacement = new Mogre.Vector3(0, 0, dist); entityNode.Translate(displacement.x, displacement.y, displacement.z, Node.TransformSpace.TS_LOCAL); /*Get world movement*/ displacement = entityNode.Orientation * displacement; ControllerFlags flag; physicsController.Move(displacement, 0, 0.01f, out flag); entityNode.Position = new Mogre.Vector3( physicsController.Actor.GlobalPosition.x, entityNode.Position.y, physicsController.Actor.GlobalPosition.z); } if (baseAnim.Type == ChaAnimType.CAT_JUMP_LOOP) { // if we're jumping, add a vertical offset too, and apply gravity entityNode.Translate(0, verticalVelocity * deltaTime, 0, Node.TransformSpace.TS_LOCAL); verticalVelocity -= GRAVITY * deltaTime; Mogre.Vector3 pos = entityNode.Position; if (pos.y <= CHAR_HEIGHT * lastPosition.y) { // if we've hit the ground, change to landing state pos.y = CHAR_HEIGHT * lastPosition.y; entityNode.Position = pos; ControllerFlags flag; physicsController.Move(pos, 1 << 3, 0.01f, out flag); entityNode.Position = physicsController.Actor.GlobalPosition; SetBaseAnimation(anims.Where(o => o.Type == ChaAnimType.CAT_JUMP_END).First(), true); timer = 0; } } }
public void Update(DX11Game _game) { var game = TW.Graphics; if (movementDisabled) { frameMovement = Vector3.Zero; } if (velocity.LengthSquared() < 0.001f) { velocity = Vector3.Zero; } // Calculate component in the direction of the velocity if (frameMovement.LengthSquared() > 0.00001f) { var dot = Vector3.Dot(Vector3.Normalize(frameMovement), Vector3.Normalize(velocity)); if (dot < 0) { velocity -= velocity * 0.1f * -dot; frameMovement -= Vector3.Normalize(velocity) * Vector3.Dot(frameMovement, Vector3.Normalize(velocity)); } } if (velocity.LengthSquared() > 0.001f) { while (controllerHitReport.HitQueue.Count > 0) { var hit = controllerHitReport.HitQueue.Dequeue(); var frictionDot = Vector3.Dot(velocity, hit.WorldNormal); if (frictionDot > 0) { continue; // moving away from surface } var tangentVelocity = velocity - hit.WorldNormal * frictionDot; if (tangentVelocity.LengthSquared() < (1f * 1f)) { //Static friction velocity -= tangentVelocity; } else { //Dynamic friction velocity -= tangentVelocity * game.Elapsed; // this integrates the friction force. } } } controllerHitReport.HitQueue.Clear(); // This is bad practice ( 2-way dependancy ) (no clue what this means) player.Position = controller.Position.dx(); //Gravity var gravity = Vector3.Down * 10; if (!DisableGravity) { velocity += gravity * game.Elapsed; } frameMovement += velocity * game.Elapsed; var oldPos = controller.Position; controller.ReportSceneChanged(); // Apply fake gravity. Downward speed is constant. controller.Move(frameMovement); frameMovement = Vector3.Zero; // Clamp to 0 for now Vector3 position = controller.Position; if (position.Y < controller.Height + GroundHeight) { controller.Position = position + Vector3.UnitY * (controller.Height - position.Y + GroundHeight); } if (Math.Abs((controller.Position - oldPos).Y) < 0.001) { velocity.Y = 0; } player.Position = controller.Position.dx(); //Console.WriteLine(velocity); if (DisableGravity) { velocity = Vector3.Zero; // Apply infinite friction } }