public virtual void PreTick() { if (!CanAttach()) { Reset(); return; } Hits = new TraceResult[DirectionsOfTravel.Length]; for (int i = 0; i < DirectionsOfTravel.Length; i++) { // Translate local direction to world space Vector3 direction = Controller.Rotation * DirectionsOfTravel[i]; Vector3 origin = Controller.Position + Vector3.Up * 5f; var tr = Trace.Ray(origin, origin + direction * WallMaxDistance) .Ignore(Controller.Pawn) .Run(); // Cache result Hits[i] = tr; if (Hits[i].Entity != null) { DebugOverlay.Sphere(Hits[i].EndPos, 3, Color.Green); DebugOverlay.Line(Controller.Position, origin + direction * WallMaxDistance, Color.Green); } else { DebugOverlay.Line(Controller.Position, origin + direction * WallMaxDistance, Color.Red); } } Hits = Hits.ToList().Where(h => h.Entity != null).OrderBy(h => h.Distance).ToArray(); if (Hits.Length > 0) { PerformWallRun(ref Hits[0]); } else { Reset(); } }
public override void OnPlayerControlTick(TankPlayer player, bool allowDebug) { var extents = CollisionBounds.Maxs; var cannonEndLocal = extents.WithY(0) + Vector3.Down * 4; var cannonStart = player.Base.Position + cannonEndLocal.WithX(0); var cannonEnd = player.Base.Position + player.Head.Rotation * cannonEndLocal; var cannonTr = Trace.Ray(cannonStart, cannonEnd).Radius(4).WithoutTags("rocket").Run(); if (allowDebug) { DebugOverlay.Sphere(cannonTr.EndPos, 4, cannonTr.Hit ? Color.Red : Color.Green, true); DebugOverlay.Line(cannonStart, cannonEnd, Color.Blue, 0, false); } float retractTime = CannonCooldown * 0.1f, expandTime = CannonCooldown * 0.6f, cannonRecoilDistance = 16; float cannonRecoilOffset = 0; if (timeSinceCannonFire < retractTime) // Retracting { cannonRecoilOffset = cannonRecoilDistance * (timeSinceCannonFire / retractTime); } else if (timeSinceCannonFire < expandTime + retractTime) // Expanding { cannonRecoilOffset = cannonRecoilDistance - cannonRecoilDistance * ((timeSinceCannonFire - retractTime) / expandTime); } cannonRecoilOffset *= MathF.Pow(cannonTr.Distance / extents.x, 2); // Reduces recoil the more the cannon is pushed in due to collision float cannonOffset = (cannonRecoilOffset - cannonTr.Distance).Clamp(float.MinValue, -28.5f); LocalPosition = Vector3.Backward * (extents.x + cannonOffset); if (Input.Down(InputButton.Attack1) && timeSinceCannonFire > CannonCooldown) { cannonEnd = Position + player.Head.Rotation * cannonEndLocal; // Recalc cannon end after recoil and retracting was applied FireRocket(player, cannonEnd); } }
protected override void Update(GameTime gameTime) { #region Input. if (!IsActive) { return; } if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape)) { this.Exit(); } #endregion ///////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////// // Let's draw some stuff... // Draw code can be called from anywhere in the application - // even in separate threads! Use it for AI, Physics, // Gameplay, Graphics, etc. // Lines. for (int i = 0; i < 25; ++i) { DebugOverlay.Line(new Vector3(.5f * i, 5, 5), new Vector3(.5f * i, 5, -5), Color.Tomato); DebugOverlay.Line(new Vector3(.5f * i, 3, 5), new Vector3(.5f * i, 3, -5), Color.Violet); DebugOverlay.Line(new Vector3(.5f * i, 1, 5), new Vector3(.5f * i, 1, -5), Color.SeaGreen); } // Points. float radius = 4; for (int i = 0; i < 360; i += 2) { float rads = i * MathHelper.Pi / 180; DebugOverlay.Point(new Vector3( (float)Math.Cos(rads) * radius, -5, (float)Math.Sin(rads) * radius), Color.Black); } // Spheres. DebugOverlay.Sphere(new Vector3(-10, 0, 5), 3, Color.Brown); DebugOverlay.Sphere(new Vector3(-5, 0, 5), 2, Color.DarkGreen); DebugOverlay.Sphere(new Vector3(-2, 0, 5), 1, Color.Crimson); DebugOverlay.Sphere(new Vector3(-.5f, 0, 5), .5f, Color.DarkSalmon); // Bounding box. DebugOverlay.BoundingBox(new BoundingBox(new Vector3(-10, 0, -10), new Vector3(-5, 5, -5)), Color.RoyalBlue); // Screen text. DebugOverlay.ScreenText("FPS: " + mFps.FramesPerSecond, new Vector2(5, 7), Color.DimGray); DebugOverlay.ScreenText("FPS: " + mFps.FramesPerSecond, new Vector2(7, 5), Color.White); // Arrows. DebugOverlay.Arrow(Vector3.Zero, Vector3.UnitX * 10, 1, Color.Red); DebugOverlay.Arrow(Vector3.Zero, Vector3.UnitY * 10, 1, Color.Green); DebugOverlay.Arrow(Vector3.Zero, Vector3.UnitZ * 10, 1, Color.Blue); ///////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////// #region Update. mCamera.Update((float)gameTime.ElapsedGameTime.TotalSeconds); mFps.Tick(); base.Update(gameTime); #endregion }
public override void OnPlayerControlTick(TankPlayer player, bool allowDebug) { float throttle = Input.Forward; float steering = Input.Left * (throttle >= 0 ? 1 : -1) * TurnSpeed; var moveDelta = Rotation.Forward * throttle * MoveSpeed; // Corner world position var frontLeft = moveDelta + Position + Rotation * (extents * new Vector3(1, 1, 0.5f)); var frontRight = moveDelta + Position + Rotation * (extents * new Vector3(1, -1, 0.5f)); var backLeft = moveDelta + Position + Rotation * (extents * new Vector3(-1, 1, 0.5f)); var backRight = moveDelta + Position + Rotation * extents * new Vector3(-1, -1, 0.5f); var center = moveDelta + Position + extents * new Vector3(0, 0, 0.5f); // Base world directions var frontDir = Rotation.Forward; var leftDir = Rotation.Left; var backDir = Rotation.Backward; var rightDir = Rotation.Right; var frontLeftDir = (frontDir + leftDir).Normal; var frontRightDir = (frontDir + rightDir).Normal; var backLeftDir = (backDir + leftDir).Normal; var backRightDir = (backDir + rightDir).Normal; float testDistance = 2 * MoveSpeed; if (allowDebug) { /*DebugOverlay.Axis(frontLeft, Rotation); * DebugOverlay.Axis(frontRight, Rotation); * DebugOverlay.Axis(backLeft, Rotation); * DebugOverlay.Axis(backRight, Rotation);*/ DebugOverlay.Sphere(frontLeft, testDistance, Color.Red); DebugOverlay.Sphere(frontRight, testDistance, Color.Red); DebugOverlay.Sphere(backLeft, testDistance, Color.Red); DebugOverlay.Sphere(backRight, testDistance, Color.Red); } var cornerResults = new TraceResult[12]; cornerResults[0] = Trace.Ray(frontLeft, frontLeft + frontDir * testDistance).Run(); // Forward cornerResults[1] = Trace.Ray(frontLeft, frontLeft + leftDir * testDistance).Run(); // Side cornerResults[2] = Trace.Ray(frontLeft, frontLeft + frontRightDir * testDistance).Run(); // Diagonal inward cornerResults[3] = Trace.Ray(frontRight, frontRight + frontDir * testDistance).Run(); cornerResults[4] = Trace.Ray(frontRight, frontRight + rightDir * testDistance).Run(); cornerResults[5] = Trace.Ray(frontRight, frontRight + frontLeftDir * testDistance).Run(); cornerResults[6] = Trace.Ray(backLeft, backLeft + backDir * testDistance).Run(); cornerResults[7] = Trace.Ray(backLeft, backLeft + leftDir * testDistance).Run(); cornerResults[8] = Trace.Ray(backLeft, backLeft + backRightDir * testDistance).Run(); cornerResults[9] = Trace.Ray(backRight, backRight + backDir * testDistance).Run(); cornerResults[10] = Trace.Ray(backRight, backRight + rightDir * testDistance).Run(); cornerResults[11] = Trace.Ray(backRight, backRight + backLeftDir * testDistance).Run(); // Turn off movement and don't collide if both front or back corners are colliding (but only if pushing against an axis-aligned wall) bool noMove = (cornerResults[0].Hit && cornerResults[3].Hit) || (cornerResults[6].Hit && cornerResults[9].Hit); float yaw = Rotation.Yaw(); noMove &= yaw.SnapToGrid(90).AlmostEqual(yaw, 2); if (noMove) { moveDelta = 0; } for (int i = 0; i < cornerResults.Length; i++) { var result = cornerResults[i]; if (!result.Hit) { if (allowDebug) { DebugOverlay.Line(result.StartPos, result.StartPos + result.Direction * testDistance * 8, Color.Green, 0, false); } continue; } if (i % 3 != 0 && cornerResults[i - (i % 3)].Hit) // Skip the side and diagonal tests if the primary one already hit { if (allowDebug) { DebugOverlay.Line(result.StartPos, result.StartPos + result.Direction * testDistance * 8, Color.Yellow, 0, false); } continue; } if (i % 3 == 0 && noMove) { continue; } if (allowDebug) { DebugOverlay.Line(result.StartPos, result.StartPos + result.Direction * testDistance * 8, Color.Red, 0, false); } Position += result.Normal * (testDistance - result.Distance); float torque = Vector3.Cross(result.StartPos - center, result.Normal).z *Time.Delta; Rotation *= Rotation.FromYaw(torque); if (allowDebug) { DebugOverlay.Line(result.EndPos, result.EndPos + result.Normal * MathF.Abs(torque) * 50, Color.Blue, 0, false); } } var edgeResults = new TraceResult[8]; edgeResults[0] = Trace.Ray(frontLeft, frontRight).Run(); edgeResults[2] = Trace.Ray(backLeft, backRight).Run(); edgeResults[4] = Trace.Ray(frontLeft, backLeft).Run(); edgeResults[6] = Trace.Ray(frontRight, backRight).Run(); for (int i = 0; i < edgeResults.Length; i += 2) { var result = edgeResults[i]; if (result.Hit) // Only run the partner trace if the first one on that edge hits { if (i == 0) { edgeResults[i + 1] = Trace.Ray(frontRight, frontLeft).Run(); } else if (i == 2) { edgeResults[i + 1] = Trace.Ray(backRight, backLeft).Run(); } else if (i == 4) { edgeResults[i + 1] = Trace.Ray(backLeft, frontLeft).Run(); } else if (i == 6) { edgeResults[i + 1] = Trace.Ray(backRight, frontRight).Run(); } } else { continue; } if (allowDebug) { DebugOverlay.Line(result.StartPos, result.EndPos, 0, false); } Vector3 correctionDir = Vector3.Zero; if (i == 0) { correctionDir = frontDir; } else if (i == 2) { correctionDir = backDir; } else if (i == 4) { correctionDir = leftDir; } else if (i == 6) { correctionDir = rightDir; } float distance = Vector3.DistanceBetween(result.EndPos, edgeResults[i + 1].EndPos); float angle = Vector3.GetAngle(result.EndPos - result.StartPos, result.Normal) - 90; float correction = distance * 0.5f * MathF.Sin(2 * angle.DegreeToRadian()); // How much to push out of the corner, finds the altitude of a right triangle Position -= correctionDir * correction; var avgPos = (result.EndPos + edgeResults[i + 1].EndPos) * 0.5f; float torque = Vector3.Cross(avgPos - center, -correctionDir).z *Time.Delta; Rotation *= Rotation.FromYaw(torque); if (allowDebug) { DebugOverlay.Line(avgPos, avgPos - correctionDir * MathF.Abs(torque) * 50, Color.Cyan, 0, false); DebugOverlay.Line(result.EndPos, edgeResults[i + 1].EndPos, Color.Red, 0, false); DebugOverlay.Line(result.EndPos, result.EndPos + correctionDir * correction * 10, Color.Green, 0, false); DebugOverlay.Line(edgeResults[i + 1].EndPos, edgeResults[i + 1].EndPos + correctionDir * correction * 10, Color.Green, 0, false); } } moveDelta = moveDelta.WithZ(0); Position += moveDelta; Rotation *= Rotation.FromYaw(steering); }
public void OnPrePhysicsStep() { if (!IsServer) { return; } var selfBody = PhysicsBody; if (!selfBody.IsValid()) { return; } var body = selfBody.SelfOrParent; if (!body.IsValid()) { return; } var dt = Time.Delta; body.DragEnabled = false; var rotation = selfBody.Rotation; accelerateDirection = currentInput.throttle.Clamp(-1, 1) * (1.0f - currentInput.breaking); TurnDirection = TurnDirection.LerpTo(currentInput.turning.Clamp(-1, 1), 1.0f - MathF.Pow(0.001f, dt)); airRoll = airRoll.LerpTo(currentInput.roll.Clamp(-1, 1), 1.0f - MathF.Pow(0.0001f, dt)); airTilt = airTilt.LerpTo(currentInput.tilt.Clamp(-1, 1), 1.0f - MathF.Pow(0.0001f, dt)); float targetTilt = 0; float targetLean = 0; var localVelocity = rotation.Inverse * body.Velocity; if (backWheelsOnGround || frontWheelsOnGround) { var forwardSpeed = MathF.Abs(localVelocity.x); var speedFraction = MathF.Min(forwardSpeed / 500.0f, 1); targetTilt = accelerateDirection.Clamp(-1.0f, 1.0f); targetLean = speedFraction * TurnDirection; } AccelerationTilt = AccelerationTilt.LerpTo(targetTilt, 1.0f - MathF.Pow(0.01f, dt)); TurnLean = TurnLean.LerpTo(targetLean, 1.0f - MathF.Pow(0.01f, dt)); if (backWheelsOnGround) { var forwardSpeed = MathF.Abs(localVelocity.x); var speedFactor = 1.0f - (forwardSpeed / 5000.0f).Clamp(0.0f, 1.0f); var acceleration = speedFactor * (accelerateDirection < 0.0f ? car_accelspeed * 0.5f : car_accelspeed) * accelerateDirection * dt; var impulse = rotation * new Vector3(acceleration, 0, 0); body.Velocity += impulse; } RaycastWheels(rotation, true, out frontWheelsOnGround, out backWheelsOnGround, dt); var onGround = frontWheelsOnGround || backWheelsOnGround; var fullyGrounded = (frontWheelsOnGround && backWheelsOnGround); Grounded = onGround; if (fullyGrounded) { body.Velocity += Map.Physics.Gravity * dt; } body.GravityScale = fullyGrounded ? 0 : 1; bool canAirControl = false; var v = rotation * localVelocity.WithZ(0); var vDelta = MathF.Pow((v.Length / 1000.0f).Clamp(0, 1), 5.0f).Clamp(0, 1); if (vDelta < 0.01f) { vDelta = 0; } if (debug_car) { DebugOverlay.Line(body.MassCenter, body.MassCenter + rotation.Forward.Normal * 100, Color.White, 0, false); DebugOverlay.Line(body.MassCenter, body.MassCenter + v.Normal * 100, Color.Green, 0, false); } var angle = (rotation.Forward.Normal * MathF.Sign(localVelocity.x)).Normal.Dot(v.Normal).Clamp(0.0f, 1.0f); angle = angle.LerpTo(1.0f, 1.0f - vDelta); grip = grip.LerpTo(angle, 1.0f - MathF.Pow(0.001f, dt)); if (debug_car) { DebugOverlay.ScreenText(new Vector2(200, 200), $"{grip}"); } var angularDamping = 0.0f; angularDamping = angularDamping.LerpTo(5.0f, grip); body.LinearDamping = 0.0f; body.AngularDamping = fullyGrounded ? angularDamping : 0.5f; if (onGround) { localVelocity = rotation.Inverse * body.Velocity; WheelSpeed = localVelocity.x; var turnAmount = frontWheelsOnGround ? (MathF.Sign(localVelocity.x) * 25.0f * CalculateTurnFactor(TurnDirection, MathF.Abs(localVelocity.x)) * dt) : 0.0f; body.AngularVelocity += rotation * new Vector3(0, 0, turnAmount); airRoll = 0; airTilt = 0; var forwardGrip = 0.1f; forwardGrip = forwardGrip.LerpTo(0.9f, currentInput.breaking); body.Velocity = VelocityDamping(Velocity, rotation, new Vector3(forwardGrip, grip, 0), dt); } else { var s = selfBody.Position + (rotation * selfBody.LocalMassCenter); var tr = Trace.Ray(s, s + rotation.Down * 50) .Ignore(this) .Run(); if (debug_car) { DebugOverlay.Line(tr.StartPosition, tr.EndPosition, tr.Hit ? Color.Red : Color.Green); } canAirControl = !tr.Hit; } if (canAirControl && (airRoll != 0 || airTilt != 0)) { var offset = 50 * Scale; var s = selfBody.Position + (rotation * selfBody.LocalMassCenter) + (rotation.Right * airRoll * offset) + (rotation.Down * (10 * Scale)); var tr = Trace.Ray(s, s + rotation.Up * (25 * Scale)) .Ignore(this) .Run(); if (debug_car) { DebugOverlay.Line(tr.StartPosition, tr.EndPosition); } bool dampen = false; if (currentInput.roll.Clamp(-1, 1) != 0) { var force = tr.Hit ? 400.0f : 100.0f; var roll = tr.Hit ? currentInput.roll.Clamp(-1, 1) : airRoll; body.ApplyForceAt(selfBody.MassCenter + rotation.Left * (offset * roll), (rotation.Down * roll) * (roll * (body.Mass * force))); if (debug_car) { DebugOverlay.Sphere(selfBody.MassCenter + rotation.Left * (offset * roll), 8, Color.Red); } dampen = true; } if (!tr.Hit && currentInput.tilt.Clamp(-1, 1) != 0) { var force = 200.0f; body.ApplyForceAt(selfBody.MassCenter + rotation.Forward * (offset * airTilt), (rotation.Down * airTilt) * (airTilt * (body.Mass * force))); if (debug_car) { DebugOverlay.Sphere(selfBody.MassCenter + rotation.Forward * (offset * airTilt), 8, Color.Green); } dampen = true; } if (dampen) { body.AngularVelocity = VelocityDamping(body.AngularVelocity, rotation, 0.95f, dt); } } localVelocity = rotation.Inverse * body.Velocity; MovementSpeed = localVelocity.x; }