public void AbsTest() { float actual = MathHelper.Abs(-1f); float expected = 1f; Assert.AreEqual(expected, actual); }
protected override void UpdateInternal(AIStateInfo aIState) { if (distances != null) { int length = directions.Length; float distance = aIState.VectorToTarget.Magnitude - (aIState.Target.BoundingRadius + source.BoundingRadius); float angle = aIState.VectorToTarget.Angle; this.aimAngle = MathHelper.PI + angle; float aimAngle; for (int pos = 0; pos < length; ++pos) { if (distance < distances[pos] * 2) { aimAngle = angle - directions[pos]; if (MathHelper.Abs(MathHelper.GetAngleDifference(angle, aimAngle)) < MathHelper.Abs(MathHelper.GetAngleDifference(angle, this.aimAngle))) { this.shouldAim = true; this.aimAngle = aimAngle; } if (distance < distances[pos] && Logic.InArc(source.DirectionAngle - directions[pos], angle, distance, aIState.Target.BoundingRadius, .1f)) { this.shouldActivate = true; return; } } } } }
public override ControlInput GetControlInput(float dt, ControlInput original) { if (dt == 0 || this.host == null || CheckTarget()) { return(original); } float Velocity = host.Current.Velocity.Angular; float AbsVelocity = MathHelper.Abs(Velocity); if (AbsVelocity > host.MovementInfo.MaxLinearVelocity) { return(original); } else { float Accel = host.MovementInfo.MaxAngularAcceleration * dt; float DifferenceA = GetAngleDifference(); float breakpoint = (AbsVelocity * AbsVelocity) / (2 * host.MovementInfo.MaxAngularAcceleration.Value); float AbsDifferenceA = MathHelper.Abs(DifferenceA); if (AbsDifferenceA > breakpoint) { if (DifferenceA > 0) { original[InputAction.RotateLeft] = true; } else { original[InputAction.RotateRight] = true; } original.TorquePercent = 1; original[InputAction.MoveForward] = true; original.ThrustPercent = 1; return(original); } else { float dv = Velocity; original.TorquePercent = 1; original[InputAction.MoveForward] = true; original.ThrustPercent = .5f; if (AbsVelocity < Accel) { original.TorquePercent = AbsVelocity / Accel; original.ThrustPercent += (1 - original.TorquePercent) * .5f; } if (0 < -dv) { original[InputAction.RotateLeft] = true; } else { original[InputAction.RotateRight] = true; } } } return(original); }
public static bool InArc(float centerAngle, float currentAngle, float distance, float radius, float tolerance) { float anglediff = MathHelper.GetAngleDifference(centerAngle, currentAngle); float tanthing = MathHelper.Atan(radius / distance); return (MathHelper.Abs(anglediff) < MathHelper.Abs(tanthing) + tolerance); }
protected override void UpdateInternal(AIStateInfo aIState) { float distance = aIState.VectorToTarget.Magnitude; float aimAngle = 0; float angle = aIState.VectorToTarget.Angle; int length = action.Weapons.Length; for (int pos = 0; pos < length; ++pos) { float range = action.Weapons[pos].LifeTime.TimeLeft * (action.Weapons[pos].MovementInfo.MaxLinearVelocity + aIState.VectorToTarget.Normalized * source.Current.Velocity.Linear) + (source.BoundingRadius + aIState.Target.BoundingRadius + action.Weapons[pos].BoundingRadius); if (range > distance) { if (action.Weapons[pos].IsFireAndForget) { this.aimAngle = angle - action.VelocityAngles[pos]; float diff = MathHelper.GetAngleDifference(this.aimAngle, source.DirectionAngle); this.shouldActivate = MathHelper.Abs(diff) < MathHelper.HALF_PI; this.shouldAim = !this.shouldActivate; return; } else { if (Logic.TrySolveInterceptAngle( source.Current.Position.Linear, aIState.Target.Current.Position.Linear, aIState.Target.Current.Velocity.Linear - source.Current.Velocity.Linear, action.Weapons[pos].MovementInfo.MaxLinearVelocity, out aimAngle)) { aimAngle += action.VelocityAngles[pos]; } if (MathHelper.Abs(MathHelper.GetAngleDifference(angle, aimAngle)) < MathHelper.Abs(MathHelper.GetAngleDifference(angle, this.aimAngle))) { this.shouldAim = true; this.aimAngle = aimAngle; } if (Logic.InArc(source.DirectionAngle + action.VelocityAngles[pos], aimAngle, distance, aIState.Target.BoundingRadius, GunActionAIInfo.AimTolerance + GetRandomVelocityAngle(pos))) { this.shouldActivate = true; return; } } } } }
/// <summary> /// Transforms this BoundingBox by a Matrix. /// </summary> /// <param name="transform">The matrix to use for the transformation.</param> public void Transform(ref Matrix transform) { Vector3 half = (Max - Min) / 2; Vector3 center = (Max + Min) / 2; Vector3.Transform(ref center, ref transform, out center); Matrix abs; MathHelper.Abs(ref transform, out abs); Vector3.Transform(ref half, ref abs, out half); Min = center - half; Max = center + half; }
protected override void UpdateInternal(AIStateInfo aIState) { if (action.CurrentWeapon == null) { float distance = aIState.VectorToTarget.Magnitude; float range = action.Weapon.LifeTime.TimeLeft * (action.Weapon.MovementInfo.MaxLinearVelocity + aIState.VectorToTarget.Normalized * source.Current.Velocity.Linear) + (source.BoundingRadius + aIState.Target.BoundingRadius + action.Weapon.BoundingRadius); if (range > distance) { if (action.Weapon.IsFireAndForget) { this.aimAngle = aIState.VectorToTarget.Angle + action.VelocityAngle; float diff = MathHelper.GetAngleDifference(this.aimAngle, source.DirectionAngle); this.shouldActivate = MathHelper.Abs(diff) < MathHelper.HALF_PI; this.shouldAim = !this.shouldActivate; } else if (Logic.TrySolveInterceptAngle( source.Current.Position.Linear, aIState.Target.Current.Position.Linear, aIState.Target.Current.Velocity.Linear - source.Current.Velocity.Linear, action.Weapon.MovementInfo.MaxLinearVelocity, out this.aimAngle)) { this.aimAngle += action.VelocityAngle; this.shouldAim = true; float angle = aIState.VectorToTarget.Angle; this.shouldActivate = Logic.InArc(source.DirectionAngle + action.VelocityAngle, aimAngle, distance, aIState.Target.BoundingRadius, GunActionAIInfo.AimTolerance); } } } else { Vector2D fromcw = aIState.Target.Current.Position.Linear - action.CurrentWeapon.Current.Position.Linear; if (fromcw * action.CurrentWeapon.Current.Velocity.Linear > 0) { this.shouldActivate = true; } } }
protected override float GetDesiredAngle() { Vector2D DifferenceL = target.Current.Position.Linear - host.Current.Position.Linear; float ADesired = orbitDistance / DifferenceL.Magnitude; float Desired; if (MathHelper.Abs(ADesired) > 1) { Desired = DifferenceL.Angle + (float)MathHelper.PI * .5f; } else { Vector2D TargetPoint = target.Current.Position.Linear + Vector2D.Rotate((float)Math.Asin(ADesired), DifferenceL.Normalized).RightHandNormal *orbitDistance; DifferenceL = TargetPoint - host.Current.Position.Linear; Desired = DifferenceL.Angle; } return(Desired); }
public static void ApplyThrust(float dt, IControlable host, Vector2D direction, float ThrustPercent) { //Increase Velocity in direction. float LAccel = host.MovementInfo.MaxLinearAcceleration * dt * ThrustPercent; if ((host.UQMFlags & ContFlags.NoMaxSpeed) == ContFlags.NoMaxSpeed) { ApplydLV(host, LAccel * direction, dt); } else { float VelocityInDirection = host.Current.Velocity.Linear * direction; if (VelocityInDirection < host.MovementInfo.MaxLinearVelocity) { float newLV = VelocityInDirection + LAccel; if (newLV > host.MovementInfo.MaxLinearVelocity) { LAccel = host.MovementInfo.MaxLinearVelocity - VelocityInDirection; } ApplydLV(host, LAccel * direction, dt); } } // now reduce movement sideways to the direction. Vector2D tanget = direction.RightHandNormal; float SidewaysVelocity = host.Current.Velocity.Linear * tanget; if (MathHelper.Abs(SidewaysVelocity) < LAccel) { //host.current.Velocity.Linear -= SidewaysVelocity * tanget; ApplydLV(host, -SidewaysVelocity * tanget, dt); } else { if (SidewaysVelocity > 0) { ApplydLV(host, -LAccel * tanget, dt); } else { ApplydLV(host, LAccel * tanget, dt); } } }
public void Render(IViewport viewport, Matrix view, Matrix projection, Matrix world) { var actualViewport = viewport as Viewport; var position = new Vector(0, 0, 0); var actualPosition = new Vector(world.data[12], world.data[13], world.data[14]); var transformedPosition = Vector.Transform(position, world, view); var translatedPosition = Vector.Translate(transformedPosition, projection, viewport.Width, viewport.Height); /* * * w = width passed to D3DXMatrixPerspectiveLH * h = height passed to D3DXMatrixPerspectiveLH * n = z near passed to D3DXMatrixPerspectiveLH * f = z far passed to D3DXMatrixPerspectiveLH * d = distance of sprite from camera along Z * qw = width of sprite quad * qh = height of sprite quad * vw = viewport height * vh = viewport width * scale = n / d * (i.e. near/distance, such that at d = n, scale = 1.0) * renderedWidth = vw * qw * scale / w * renderedHeight = vh * qh * scale / h * */ var distanceVector = actualViewport.Camera.Position - actualPosition; float distance = distanceVector.Length; float N = 30.0f; distance = MathHelper.Abs(distance); float scale = 0.0f + ((2 * N) / distance); if (scale <= 0) { scale = 0; } actualViewport.RenderImage(_frame, transformedPosition, translatedPosition, scale); }
public override void Render(IViewport viewport, Matrix view, Matrix projection) { /* From DirectX sample * w = width passed to D3DXMatrixPerspectiveLH * h = height passed to D3DXMatrixPerspectiveLH * n = z near passed to D3DXMatrixPerspectiveLH * f = z far passed to D3DXMatrixPerspectiveLH * d = distance of sprite from camera along Z * qw = width of sprite quad * qh = height of sprite quad * vw = viewport height * vh = viewport width * scale = n / d * (i.e. near/distance, such that at d = n, scale = 1.0) * renderedWidth = vw * qw * scale / w * renderedHeight = vh * qh * scale / h */ var position = new Vector(0, 0, 0); var actualPosition = new Vector(World.data[12], World.data[13], World.data[14]); var transformedPosition = Vector.Transform(position, World, view); var translatedPosition = Vector.Translate(transformedPosition, projection, viewport.Width, viewport.Height); var distanceVector = viewport.Camera.Position - actualPosition; var distance = distanceVector.Length; var n = 100.0f; distance = MathHelper.Abs(distance); var scale = 0.0f + ((2 * n) / distance); if (scale <= 0) { scale = 0; } var xscale = scale; var yscale = scale; _spriteContext.Render(viewport, this, view, projection, World, xscale, yscale, 0f); }
public override void Update(GPlacementComponent placement) { base.Update(placement); var lookAtDir2D = LookAtDir; lookAtDir2D.Y = 0; var velocity = mVelocity; velocity.Y = 0; velocity.Normalize(); if (mVelocity.LengthSquared() == 0) { MovementDir = (placement.Rotation * -Vector3.UnitZ).NormalizeValue; MovementSpeed = MathHelper.FloatLerp(MovementSpeed, mVelocity.Length(), 0.5f); if (MovementSpeed > 0 && MovementSpeed < 0.1f) { MovementSpeed = 0; } } else { var orignDir = (placement.Rotation * -Vector3.UnitZ).NormalizeValue; var sign = Vector3.Dot(orignDir, velocity); if (MathHelper.Abs(sign) <= MathHelper.Epsilon) { sign = 1; } else { sign = sign / MathHelper.Abs(sign); } MovementSpeed = MathHelper.FloatLerp(MovementSpeed, sign * mVelocity.Length(), 0.2f); if (MathHelper.Abs(MovementSpeed) > 0 && MathHelper.Abs(MovementSpeed) < 0.1f) { MovementSpeed = sign * 0.1f; } MovementDir = mVelocity.NormalizeValue; } }
/// <summary> /// Performs partial pivoting. /// </summary> /// <param name="m">The matrix.</param> /// <param name="column">Column.</param> static void PartialPivotMatrix(double[,] m, int column) { double absMax = 0.0; int index = -1, size = m.GetLength(0); // We calculate the biggest element. for (int i = column; i < size; i++) { double v = MathHelper.Abs(m[i, column]); if (v >= absMax) { index = i; absMax = v; } } // Perform pivoting. if (index != column) { InterchangeRows(m, column, index); } }
void UpdateAngularMovement(float dt, IControlable host) { if (dt == 0) { return; } ControlInput input = host.CurrentControlInput; float AV = angleVelocity; float AAccel = host.MovementInfo.MaxAngularAcceleration.Value * dt; float AbsAV = MathHelper.Abs(AV); bool right = input[InputAction.RotateRight]; bool left = input[InputAction.RotateLeft]; bool thirdtest = (AbsAV <= host.MovementInfo.MaxAngularVelocity); if ((right ^ left) && thirdtest) { float newAV; AAccel *= input.TorquePercent; if (left) { newAV = AV + AAccel; if (newAV < host.MovementInfo.MaxAngularVelocity) { //host.current.Velocity.Angular = newAV; ApplydAV(host, AAccel, dt); } else { //host.current.Velocity.Angular = host.MovementInfo.MaxAngularVelocity; ApplydAV(host, host.MovementInfo.MaxAngularVelocity - AV, dt); } } else { newAV = AV - AAccel; if (newAV > -host.MovementInfo.MaxAngularVelocity) { //host.current.Velocity.Angular = newAV; ApplydAV(host, -AAccel, dt); } else { //host.current.Velocity.Angular = -host.MovementInfo.MaxAngularVelocity; ApplydAV(host, -host.MovementInfo.MaxAngularVelocity - AV, dt); } } } else { if (AbsAV <= AAccel) { //host.current.Velocity.Angular = 0; ApplydAV(host, 0 - AV, dt); } else { //host.current.Velocity.Angular -= (float)Math.Sign(AV) * AAccel; ApplydAV(host, -(float)Math.Sign(AV) * AAccel, dt); } } }
public void AbsTest() { Assert.AreEqual(0f, MathHelper.Abs(0f)); Assert.AreEqual(5.75f, MathHelper.Abs(5.75f)); Assert.AreEqual(2.37f, MathHelper.Abs(-2.37f)); }
public float AbsDot(Vector3 v) { return(MathHelper.Abs(Dot(v))); }
public static float AbsDot(Vector3 v1, Vector3 v2) { return(MathHelper.Abs(Dot(v1, v2))); }
public bool DrawGraphics(Size ViewableAreaSize) { bool returnvalue = false; List <Vector2D> positions = new List <Vector2D>(); Vector2D cameraPosition = Vector2D.Zero; int count = 0; float scale = LastScale; int asteroidCount = 0; List <IControlable> Controllables = world.Collidables; foreach (IControlable controlable in Controllables) { if ((controlable.ControlableType & ControlableType.Ship) == ControlableType.Ship) { Vector2D position = controlable.Good.Position.Linear; positions.Add(position); cameraPosition += position; count++; } if ((controlable.ControlableType & ControlableType.Debris) == ControlableType.Debris) { asteroidCount++; } } float dt = (float)timed.GetElapsed().TotalSeconds; if (count > 1) { cameraPosition *= (1 / (float)count); Vector2D diff = positions[1] - positions[0]; bool didshift = false; if (MathHelper.Abs(diff.X) > ArenaXSize.Upper) { didshift = true; cameraPosition.X += ArenaXSize.Upper; } if (MathHelper.Abs(diff.Y) > ArenaYSize.Upper) { didshift = true; cameraPosition.Y += ArenaYSize.Upper; } if (didshift) { BindWorldScreen(cameraPosition); positions.Clear(); count = 0; cameraPosition = Vector2D.Zero; foreach (IControlable controlable in Controllables) { if (controlable.ControlableType == ControlableType.Ship) { Vector2D position = controlable.Good.Position.Linear; positions.Add(position); cameraPosition += position; count++; } } cameraPosition *= ((1 / (float)count)); } Vector2D maxDist = new Vector2D(ViewableAreaSize.Width, ViewableAreaSize.Height) * .4f;// *.25f; scale = 1; foreach (Vector2D position in positions) { Vector2D dist = position - cameraPosition; if (dist.X != 0) { scale = MathHelper.Min(scale, MathHelper.Abs(maxDist.X / (dist.X))); } if (dist.Y != 0) { scale = MathHelper.Min(scale, MathHelper.Abs(maxDist.Y / (dist.Y))); } } scale *= .75f; scale = ((float)((int)(MathHelper.Ceiling(MathHelper.Sqrt(scale) * 30)))) / 30; scale *= scale; scale = scaleTransition.CalcValue(scale, dt); cameraPosition.X = xTransition.CalcValue(cameraPosition.X, dt); cameraPosition.Y = yTransition.CalcValue(cameraPosition.Y, dt); // cameraPosition = (CamerasLastPosition * interpol2 + cameraPosition * interpol); // scale = (LastScale * interpol2 + scale * interpol); CamerasLastPosition = cameraPosition; LastScale = scale; } else if (count > 0) { cameraPosition.X = xTransition.CalcValue(cameraPosition.X, dt); cameraPosition.Y = yTransition.CalcValue(cameraPosition.Y, dt); scale = scaleTransition.CalcValue(LastScale, dt); /*cameraPosition = positions[0] * .02f + CamerasLastPosition * .98f; * CamerasLastPosition = cameraPosition; * xTransition.CurrentValue = cameraPosition.X; * yTransition.CurrentValue = cameraPosition.Y;*/ //scaleTransition.CurrentValue = scale; } else { cameraPosition = CamerasLastPosition; } GenerateAsteroids(cameraPosition, asteroidCount); WindowState state = new WindowState(ViewableAreaSize, scale, cameraPosition); dotbox.DrawDots(state); for (int pos = statusBoxs.Count - 1; pos > -1; --pos) { if (statusBoxs[pos].IsExpired) { statusBoxs.RemoveAt(pos); } else { statusBoxs[pos].Draw(state); } } DrawLines(state); DrawVertexes(state); return(returnvalue); }
// should be called when a unit state message for the player was received. public override bool SetConfirmedState(int xPosition, int yPosition, byte rotation, byte healthPercent, byte frame) { bool validPosition = IsFrameInFuture(frame, LastConfirmedFrame) || (LastConfirmedFrame > frame ? LastConfirmedFrame - frame : frame - LastConfirmedFrame) >= 30; // we already received more recent frame if (!validPosition) { return(true); } // oldest frame state int cursor = nextLocalPlayerFrameIndex; // just so it has a value, set to latest state, should never be used anyway. int lastestIndex = MathHelper.Modulo((nextLocalPlayerFrameIndex - 1), localPlayerFrameStateBuffer.Length); LocalPlayerFrameState lastUpdateFrameState = localPlayerFrameStateBuffer[lastestIndex]; // iterate from old stored frame to local present frame // update confirmed frame and all following frames to correct current position while (true) { // should be oldest and first frame that is updated here. if (localPlayerFrameStateBuffer[cursor].Frame == frame) { int xPositionDifference = MathHelper.Abs(MathHelper.Abs(localPlayerFrameStateBuffer[cursor].XPositionBase + localPlayerFrameStateBuffer[cursor].XPositionDelta) - MathHelper.Abs(xPosition)); int yPositionDifference = MathHelper.Abs(MathHelper.Abs(localPlayerFrameStateBuffer[cursor].YPositionBase + localPlayerFrameStateBuffer[cursor].YPositionDelta) - MathHelper.Abs(yPosition)); if (xPositionDifference > 6 || yPositionDifference > 6) { DIContainer.Logger.Warn(string.Format("Position inconsistency at frame: {0}. Local: X:{1}, Y:{2} Remote: X:{3} Y:{4}", frame, localPlayerFrameStateBuffer[cursor].XPositionBase + localPlayerFrameStateBuffer[cursor].XPositionDelta, localPlayerFrameStateBuffer[cursor].YPositionBase + localPlayerFrameStateBuffer[cursor].YPositionDelta, xPosition, yPosition)); DIContainer.Logger.Warn("Current buffer is: \n" + string.Join("\n", (object[])localPlayerFrameStateBuffer)); } localPlayerFrameStateBuffer[cursor].UpdateBaseValues(xPosition, yPosition); localPlayerFrameStateBuffer[cursor].Confirm(); lastUpdateFrameState = localPlayerFrameStateBuffer[cursor]; } else if (IsFrameInFuture(localPlayerFrameStateBuffer[cursor].Frame, frame)) { localPlayerFrameStateBuffer[cursor].UpdatePositionBase(lastUpdateFrameState, true); lastUpdateFrameState = localPlayerFrameStateBuffer[cursor]; } if (localPlayerFrameStateBuffer[cursor].Frame == lastLocalPlayerFrameState.Frame) { break; } cursor = MathHelper.Modulo(cursor + 1, localPlayerFrameStateBuffer.Length); } LastConfirmedFrame = frame; // update current position and rotation UpdateCurrentState(localPlayerFrameStateBuffer[cursor]); return(true); }
public static float Absolute(float v) => MathHelper.Abs(v);
public override ControlInput GetControlInput(float dt, ControlInput original) { if (rand.Next(0, 99) == 1) { base.target = null; base.CheckTarget(); } overideDesired = false; IControlable[] obstacles = this.obstacles.ToArray(); bool[] threats = new bool[obstacles.Length]; for (int pos = 0; pos < obstacles.Length; ++pos) { threats[pos] = obstacles[pos].IsThreatTo(this.host); } AIStateInfo stateInfo = null; int actionsCount = Math.Min(3, shipHost.Actions.Count); if (target != null) { stateInfo = new AIStateInfo( target, target.Current.Position.Linear - host.Current.Position.Linear, obstacles, threats ); for (int pos = 0; pos < actionsCount; ++pos) { IActionAIInfo actionAIInfo = shipHost.Actions[pos].AIInfo; if (actionAIInfo != null) { actionAIInfo.Update(stateInfo); if (!overideDesired && actionAIInfo.ShouldAim) { desiredAngle = actionAIInfo.AimAngle; overideDesired = true; } if (actionAIInfo.ShouldActivate) { original[InputAction.Action] = true; original.ActiveActions[pos] = true; } } } } if (!overideDesired) { for (int pos = 0; pos < obstacles.Length; ++pos) { if (threats[pos]) { IControlable obstacle = obstacles[pos]; Vector2D dir2 = obstacle.Current.Position.Linear - host.Current.Position.Linear; if ((obstacle.ControlableType & ControlableType.Ship) == ControlableType.Ship) { desiredAngle = (-host.DirectionVector).Angle; overideDesired = true; } else { desiredAngle = (dir2 ^ (host.DirectionVector ^ dir2)).Angle; overideDesired = true; } break; } } } if (target != null) { IShipAIInfo shipAIInfo = shipHost.AIInfo; if (shipAIInfo != null) { shipAIInfo.Update(stateInfo); if (shipAIInfo.ShouldSetAngle) { overideDesired = true; desiredAngle = shipAIInfo.DesiredAngle; } } } else if (!overideDesired) { if (host.Current.Velocity.Linear.Magnitude > host.MovementInfo.MaxLinearVelocity.Value * .05f) { desiredAngle = (-host.Current.Velocity.Linear).Angle; overideDesired = true; original = base.GetControlInput(dt, original); if (MathHelper.Abs(MathHelper.GetAngleDifference(desiredAngle, host.DirectionAngle)) < .1f) { original.ThrustPercent = 1; } else { original.ThrustPercent = 0; } return(original); } } return(base.GetControlInput(dt, original)); }