private void KeyboardControl() { if (Input.Y == 1) { Velocity += ACCELERATION * Direction; } else if (Input.Y == -1) { Velocity -= ACCELERATION * Direction; } if (Input.X == 1) { Velocity += ACCELERATION * Right; } else if (Input.X == -1) { Velocity -= ACCELERATION * Right; } else if (InputManager.IsKeyDown(Keys.Space)) { Velocity += Up * ACCELERATION; } else if (InputManager.IsKeyDown(Keys.LeftShift)) { Velocity -= Up * ACCELERATION; } if (Input.Y == 0) { var dir = Direction; dir.Y = 0; var dot = Vector3.Dot(Velocity, Vector3.Normalize(Direction)); Velocity -= Math.Sign(dot) * Direction * DECELERATION; } if (Input.X == 0) { var dir = Direction; dir.Y = 0; var vel = Velocity; vel.Y = 0; var dot = Vector3.Dot(Velocity, Vector3.Normalize(Right)); Velocity -= Math.Sign(dot) * Right * DECELERATION; } if (Velocity.Length() < 1f && Input.X == 0 && Input.Y == 0) { Velocity = new Vector3(0); } // MaxSpeed if (Velocity.Length() > MAX_SPEED) { Velocity = Vector3.Normalize(Velocity) * MAX_SPEED; } }
internal void UpdateForwardVelocity() { var forwardVector = this.RotationMatrix.Right; var projected = Vector3.Dot(forwardVector, Velocity); this.currentSpeedRatio = projected / MaxSpeed; }
bool RenderPrimitive(UInt16 address, PrimitiveInstruction opCode) { float shade = 0; // check if first vertex is a normal vector VertexInstruction vertex = Memory[address]; if (vertex.IsNormalVector) { // get the normal vector Vector3 normal = GetVertexFromTable(vertex); normal = Vector3.Transform(normal, Parent.WorldRotation); // get the first coordinate Vector3 pt = GetVertexFromTable(Memory[address + 1]); pt = Vector3.Transform(pt, Parent.D3DTS_WORLD); // check if surface is visible if (Vector3.Dot(normal, pt) <= 0) { return(false); // not visible } // if shading enabled if (opCode.IsShaded) { // both values are 14 bit fractional precision // so dot product must be dividied by 2^(14+14) to normalize from 0.0 - 1.0 // but then we multiply by 8 (2^3) to convert to a pallete index // so we divide by 2^(14+14-3) const float scale = 1.0f / (1 << 25); shade = Vector3.Dot(normal, Parent.Light) * scale; shade = Math.Min(7, Math.Max(0, shade)); } } // should we skip rendering? if (opCode.SkipRender) { return(true); // would have been visible } // prepare the vertex buffer BuildPrimitive(address, Parent.GetColor(opCode.ColorIndex, shade), opCode.RenderMode); return(true); }
protected override void Execute(List <CommandEntity> entities) { foreach (var e in entities) { var playerEntities = m_contexts.game.GetEntitiesWithPlayerId(e.commandOwner.value); if (playerEntities.Count == 0) { continue; } var playerEntity = playerEntities.SingleEntity(); if (e.moveCommand.value.LengthSquared() > 0.1f) { float angularSpeed = playerEntity.angularSpeed.value; Vector3 dir = playerEntity.direction.value; Vector3 targetDir = e.moveCommand.value; // dot(dir, rot90CCW(targetDir)) float angle = 0f; float dot = dir.X * -targetDir.Y + dir.Y * targetDir.X; // parallel/antiparallel if (Math.Abs(dot) <= ANGLE_TOLERANCE) { Services.Get <ILogService>().LogInfo("parallel/antiparallel"); if (Vector3.Dot(dir, targetDir) < 0f) { angle = angularSpeed * m_timeService.FrameInterval; } } // targetDir is in right of dir, else if (dot > float.Epsilon) { Services.Get <ILogService>().LogInfo("targetDir is in right of dir " + dot.ToString()); angle = -angularSpeed * m_timeService.FrameInterval; float d = MathHelper.ToDegrees((float)Math.Acos(dot)) - 90f; // over rotation if (Math.Abs(angle) > Math.Abs(d)) { angle = d; } } // targetDir is in left of dir else { Services.Get <ILogService>().LogInfo("targetDir is in left of dir" + dot.ToString()); angle = angularSpeed * m_timeService.FrameInterval; float d = MathHelper.ToDegrees((float)Math.Acos(dot)) - 90f; // over rotate if (Math.Abs(angle) > Math.Abs(d)) { angle = d; } } var quat = Quaternion.CreateFromAxisAngle(Vector3.Backward, MathHelper.ToRadians(angle)); var targetQuat = Quaternion.Multiply(playerEntity.rotation.value, quat); playerEntity.ReplaceRotation(targetQuat); playerEntity.ReplaceDirection(Vector3.Normalize(Vector3.Transform(Vector3.Right, targetQuat))); playerEntity.ReplaceSpeed(playerEntity.speed.maxValue, playerEntity.speed.maxValue); } else { playerEntity.ReplaceSpeed(0f, playerEntity.speed.maxValue); } } }