public void Vector2dAddsCorrectly() { var v = new Vector2d(2, 3); Assert.False(v == v + 0); Assert.False(v == v.Add(0)); Assert.True(v.Equivalent(v + 0)); Assert.True(v.Equivalent(v.Add(0))); Assert.True((v + v).Equivalent(new Vector2d(4, 6))); Assert.True(v.Add(v).Equivalent(new Vector2d(4, 6))); Assert.True((v + .1).Equivalent(new Vector2d(2.1, 3.1))); Assert.True(v.Add(.1).Equivalent(new Vector2d(2.1, 3.1))); }
// Operations public virtual void Push(Vector2d force) { Vector2d force2; force2 = Vector2d.Divide(force, Mass); Vel = Vector2d.Add(Vel, force2); }
public Vector2d Pursue(Vector2d p, Vector2d vel) { Vector2d scaledVel = Vector2d.Multiply(vel, 10); Vector2d target = Vector2d.Add(p, scaledVel); return(Seek(target)); }
protected virtual void CalcPhysics(GameState g) { Loc = Vector2d.Add(Loc, Vel); Facing += RVel; ClampVel(Misc.PHYSICSMAXSPEED); g.GetLevel().Boundary(this); //Console.WriteLine("Loc: {0}, Vel: {1}, Facing: {2}, RVel: {3}", // Loc, Vel, Facing, RVel); }
public static void LerpVector2d(ref Vector2d fromm, ref Vector2d to, double amount, out Vector2d between) { Vector2d diff, diff2; Vector2d.Subtract(ref to, ref fromm, out diff); Vector2d.Multiply(ref diff, amount, out diff2); Vector2d.Add(ref fromm, ref diff2, out between); //Console.WriteLine("From: {0} to {1} by {2}, result: {3}", fromm, to, amount, between); }
protected virtual void CalcPhysics(GameState g) { if (!TouchingGround && !Floating) { Vel = Vector2d.Add(Vel, Misc.GRAVITY); //Console.WriteLine("Foo: {0}, {1}", TouchingGround, Floating); } Loc = Vector2d.Add(Loc, Vel); ClampVel(Misc.PHYSICSMAXSPEED); //g.GetLevel().Boundary(this); //Console.WriteLine("Loc: {0}, Vel: {1}, Facing: {2}, RVel: {3}", // Loc, Vel, Facing, RVel); }
public override void Calc(GameState g, ICollection <IGameObj> nearbyObjects) { //Vector2d playerOffset = Vector2d.Add(g.Player.Loc, offset); List <IGameObj> l = new List <IGameObj>(); l.Add(g.Player); Vector2d sum = Cohesion(l); sum = Vector2d.Add(sum, Vector2d.Multiply(Separation(nearbyObjects), 0.5)); Steer(sum); //Steer(Wander()); //Steer(Arrive(playerOffset, 30)); //Steer(Pursue(g.Player.Loc, g.Player.Vel)); }
// Have to move the billboard so the middle of the billboard is in the middle of the ray, // instead of the middle of the billboard being at the start of the ray. public override void Draw(double dt) { Vector2d lerploc = Misc.LerpVector2d(OldLoc, Loc, dt); double lerpfacing = Misc.LerpDouble(OldFacing, Facing, dt); double offsetrange = Range / 2; Vector2d offsetv = Misc.Vector2dFromDirection(lerpfacing); offsetv = Vector2d.Multiply(offsetv, offsetrange); Vector2d loc = Vector2d.Add(lerploc, offsetv); Vector3d v = new Vector3d(loc); Quaterniond q = Quaterniond.FromAxisAngle(Graphics.OutOfScreen, lerpfacing); Mesh.Draw(v, q); }
private void HandleMouseMove(object sender, MouseEventArgs e) { if (Mouse.GetState().IsButtonDown(MouseButton.Right) && DraggingWorld) { var mouseLoc = GetWorldCoordinates(e.Location.X, e.Location.Y); var displacement = Vector2d.Subtract(LastDraggingLocation, mouseLoc); WorldOrigin = Vector2d.Add(WorldOrigin, displacement); Vector2d newMouseLoc = GetWorldCoordinates(e.Location.X, e.Location.Y); LastDraggingLocation = newMouseLoc; } Refresh(); }
void Pan(int dt) { if (PanTime > 0) { double timePassed = (double)dt / 1000; double proportionOfPanDone = timePassed / PanTime; Vector2d panPath = Vector2d.Subtract(PanTarget, Target); Vector2d panIncrement = Vector2d.Multiply(panPath, proportionOfPanDone); Target = Vector2d.Add(Target, panIncrement); PanTime -= timePassed; } else { SetHold(Target); } }
public bool Contains(TPoint2D point) { if (IsFullPlane) { return(true); } var A = Vector2d.Add(Origin.AsVector(), new Vector2d(Math.Cos(StartAngle), Math.Sin(StartAngle))); var B = Vector2d.Add(Origin.AsVector(), new Vector2d(Math.Cos(EndAngle), Math.Sin(EndAngle))); var AC = Geometry2D.Orient(Origin.AsVector(), A, point.AsVector()); var BC = Geometry2D.Orient(Origin.AsVector(), B, point.AsVector()); //either orientation is opposite or on the line AB return(AC == 1 && BC == -1 || AC == 0 || BC == 0); }
public void Fire(GameState g, Vector2d loc, double facing, Vector2d vel) { Vector2d firePoint = Misc.Vector2dRotate(FirePoint, facing); firePoint = Vector2d.Add(firePoint, loc); for (int i = 0; i < BulletCount; i++) { double deviation = (Misc.Rand.NextDouble() * Deviation) - (Deviation / 2); double speedDeviation = (Misc.Rand.NextDouble() * Deviation * 5); Vector2d fireDir = Misc.Vector2dRotate(FireDirection, facing + deviation); IGameObj s = ShotFunc(firePoint, facing + deviation); s.Vel = Vector2d.Multiply(fireDir, ShotVel + speedDeviation); s.Vel = Vector2d.Add(s.Vel, vel); g.AddObj(s); } }
protected override void OnRenderFrame(FrameEventArgs e) { long now = gameTime.ElapsedMilliseconds; int dt = (int)(now - lastUpdate); double frameFraction = Math.Min(1.0, (double)dt / 50.0); g.NextGraphicFrame(dt); // Setup camera, centered on the target (instead of the target being at the bottom- // left). There are some constraints to prevent the camera from scrolling off the // background; they make it stop just short of doing so. // XXX: These hardcoded values should be taken from the level map, // somehow... const int xCenterOffset = 16; const int yCenterOffset = 12; Vector2d CameraTarget = g.Camera.Target; CameraTarget.X = Misc.Clamp(CameraTarget.X, xCenterOffset, g.GetLevel().Width - xCenterOffset - 1); CameraTarget.Y = Misc.Clamp(CameraTarget.Y, yCenterOffset, g.GetLevel().Height - yCenterOffset - 1); Graphics.StartDraw(CameraTarget); // Draw background Vector2d MapOffset = new Vector2d(-xCenterOffset, -yCenterOffset); Vector2d MapPoint = Vector2d.Add(CameraTarget, MapOffset); //Console.WriteLine("MapPoint: {0}, {1}", MapPoint.X, MapPoint.Y); g.Levels[g.CurrentLevel].Draw(MapPoint); // Draw actual objects foreach (IGameObj o in g.Objs) { o.Draw(frameFraction); } foreach (Particle p in g.Particles) { p.Draw(frameFraction); } //gui.Draw(g); SwapBuffers(); }
public virtual void Calc(GameState g) { OldLoc = Loc; Loc = Vector2d.Add(Loc, Vel); Lifetime -= 1; if (Lifetime > 0) { //g.AddParticle(this); } else { g.KillParticle(this); } Sprite.Animate(); }
void doMovement(ref Particle p, double dt) { //***Calculate the factors that will affect velocty*** //1. Applying gravity var _gravity = -1 * Vector2d.UnitY * (gravity); p.Velocity += _gravity * dt; //*(velocity * rand.NextDouble() * dt); var speed = p.Velocity * dt; var pos = Vector2d.Zero; //Vector2d.Multiply(ref vel, ref _gravity, out vel); //Final Step - add the calculated Velocity the particle's position Vector2d.Add(ref p.Position, ref speed, out pos); p.Position = pos; p.Life -= dt; }
public Vector2d Wander() { double wanderRadius = 16; double wanderDistance = 600; WanderTheta += Misc.Rand.NextDouble() - 0.5; Vector2d circle = Controlled.Vel; circle = Vector2d.NormalizeFast(circle); circle = Vector2d.Multiply(circle, wanderDistance); //circle = Vector2d.Add(circle, Controlled.Loc); Vector2d circleOffset = new Vector2d(wanderRadius * Math.Cos(WanderTheta), wanderRadius * Math.Sin(WanderTheta)); Vector2d target = Vector2d.Add(circle, circleOffset); return(target); }
public Vector2d Separation(ICollection <IGameObj> nearbyObjects) { double neighborDist = 500; Vector2d sum = Vector2d.Zero; int count = 0; foreach (IGameObj o in nearbyObjects) { double d = Vector2d.Subtract(Controlled.Loc, o.Loc).Length; if (d < neighborDist) { sum = Vector2d.Add(sum, o.Loc); count += 1; } } sum = Vector2d.Divide(sum, count); sum = Vector2d.Subtract(sum, Controlled.Loc); sum = Vector2d.NormalizeFast(sum); sum = Vector2d.Multiply(sum, -Controlled.MaxVel); return(sum); }
internal void SubdivideKoch(double height, out TetrahedronFace first, out TetrahedronFace second, out TetrahedronFace third, out TetrahedronFace fourth, out TetrahedronFace fifth, out TetrahedronFace sixth) { Vector3d CenterAB, CenterBC, CenterCA, CenterD; Vector2d TexCoordAB, TexCoordBC, TexCoordCA, TexCoordD; Vector3d.Lerp(ref this.APosition, ref this.BPosition, 0.5, out CenterAB); Vector3d.Lerp(ref this.BPosition, ref this.CPosition, 0.5, out CenterBC); Vector3d.Lerp(ref this.CPosition, ref this.APosition, 0.5, out CenterCA); CenterD = CenterAB; Vector3d.Add(ref CenterD, ref CenterBC, out CenterD); Vector3d.Add(ref CenterD, ref CenterCA, out CenterD); CenterD /= 3.0; Vector3d E = CenterD + (this.Normal * 0.5); Vector3d temp = this.Normal; temp *= height; Vector3d.Add(ref CenterD, ref temp, out CenterD); Vector2d.Lerp(ref this.ATexCoord, ref this.BTexCoord, 0.5, out TexCoordAB); Vector2d.Lerp(ref this.BTexCoord, ref this.CTexCoord, 0.5, out TexCoordBC); Vector2d.Lerp(ref this.CTexCoord, ref this.ATexCoord, 0.5, out TexCoordCA); TexCoordD = TexCoordAB; Vector2d.Add(ref TexCoordD, ref TexCoordBC, out TexCoordD); Vector2d.Add(ref TexCoordD, ref TexCoordCA, out TexCoordD); TexCoordD /= 3.0; #region 1 first.APosition = this.APosition; first.ATexCoord = this.ATexCoord; first.BPosition = CenterAB; first.BTexCoord = TexCoordAB; first.CPosition = CenterCA; first.CTexCoord = TexCoordCA; first.Normal = this.Normal; temp = (this.APosition + CenterAB + CenterCA); temp /= 3.0; temp += this.Normal * -1.0; first.DPosition = temp; #endregion 1 #region 2 second.APosition = CenterAB; second.ATexCoord = TexCoordAB; second.BPosition = this.BPosition; second.BTexCoord = this.BTexCoord; second.CPosition = CenterBC; second.CTexCoord = TexCoordBC; second.Normal = this.Normal; temp = CenterAB + this.BPosition + CenterBC; temp /= 3.0; temp += this.Normal * -1.0; second.DPosition = temp; #endregion 2 #region 3 third.APosition = CenterBC; third.ATexCoord = TexCoordBC; third.BPosition = this.CPosition; third.BTexCoord = this.CTexCoord; third.CPosition = CenterCA; third.CTexCoord = TexCoordCA; third.Normal = this.Normal; temp = CenterBC + this.CPosition + CenterCA; temp /= 3.0; temp += this.Normal * -1.0; third.DPosition = temp; #endregion 3 #region 4 fourth.APosition = CenterAB; fourth.ATexCoord = TexCoordAB; fourth.BPosition = CenterD; fourth.BTexCoord = TexCoordD; fourth.CPosition = CenterCA; fourth.CTexCoord = TexCoordCA; SierpinskiTetrahedron.FindNormal(ref CenterAB, ref CenterD, ref CenterCA, out fourth.Normal); fourth.DPosition = E; #endregion 4 #region 5 fifth.APosition = CenterAB; fifth.ATexCoord = TexCoordAB; fifth.BPosition = CenterBC; fifth.BTexCoord = TexCoordBC; fifth.CPosition = CenterD; fifth.CTexCoord = TexCoordD; SierpinskiTetrahedron.FindNormal(ref CenterAB, ref CenterBC, ref CenterD, out fifth.Normal); fifth.DPosition = E; #endregion 5 #region 6 sixth.APosition = CenterBC; sixth.ATexCoord = TexCoordBC; sixth.BPosition = CenterCA; sixth.BTexCoord = TexCoordCA; sixth.CPosition = CenterD; sixth.CTexCoord = TexCoordD; SierpinskiTetrahedron.FindNormal(ref CenterBC, ref CenterCA, ref CenterD, out sixth.Normal); sixth.DPosition = E; #endregion 6 }
// Wow, that was easy... public static Vector2d LeadObject(IGameObj g) { Vector2d diff = g.Loc - g.OldLoc; return(Vector2d.Add(g.Loc, diff)); }
public static Vector2d LerpVector2d(Vector2d fromm, Vector2d to, double amount) { Vector2d diff = Vector2d.Multiply(Vector2d.Subtract(to, fromm), amount); return(Vector2d.Add(fromm, diff)); }
private static void MoveFrameRelative(List <LineSelection> selectedLines, int direction, bool isCompleteAction) { RiderFrame flag = window.Track.GetFlag(); int currentFrame = window.Track.Offset; if (flag == null || currentFrame <= flag.FrameID) { // Behavior only defined if flag is set and current frame is ahead of it return; } Rider flagFrameRider = flag.State; Rider flagNextFrameRider = window.Track.Timeline.ExtractFrame(flag.FrameID + 1).State; // Where the Rider was at the flag frame, and the frame after that // This establishes the initial frame of reference // // flagNextFrameRider is considered to be the frame with 0 relative velocity to the reference frame // All frames after will look as if the rider has started falling within the reference frame, because gravity. // // This is all if the user-configured relative speeds are (0, 0). If the user changes these speeds, // the lines will be drawn accordingly. Vector2d flagFramePos = Game.Rider.GetBounds(flagFrameRider).Vector; Vector2d flagNextFramePos = Game.Rider.GetBounds(flagNextFrameRider).Vector; // The difference between where the rider was on frames 0 and 1 establishes a reference speed to apply Vector2d firstFrameDiff = Vector2d.Subtract(flagNextFramePos, flagFramePos); // Add the user-configurable speed offsets firstFrameDiff = Vector2d.Add(firstFrameDiff, new Vector2d(Settings.animationRelativeVelX, Settings.animationRelativeVelY)); int framesElapsed = currentFrame - flag.FrameID; // Apply the speed vector to the number of elapsed frames, and add it to the initial reference frame // to get an expected position for the current frame Vector2d currentFrameExpectedPos = Vector2d.Add(Vector2d.Multiply(firstFrameDiff, framesElapsed), flagFramePos); // Same for the next frame Vector2d nextFrameExpectedPos = Vector2d.Add(Vector2d.Multiply(firstFrameDiff, framesElapsed + direction), flagFramePos); if (isCompleteAction) { window.Invalidate(); window.Track.UndoManager.BeginAction(); } TrackWriter trackWriter = window.Track.CreateTrackWriter(); List <GameLine> newLines = new List <GameLine>(); foreach (LineSelection selection in selectedLines) { GameLine selectedLine = selection.line; Vector2d p1 = selectedLine.Position; Vector2d p2 = selectedLine.Position2; Vector2d diff1 = Vector2d.Subtract(p1, currentFrameExpectedPos); Vector2d diff2 = Vector2d.Subtract(p2, currentFrameExpectedPos); Vector2d nextP1 = Vector2d.Add(nextFrameExpectedPos, diff1); Vector2d nextP2 = Vector2d.Add(nextFrameExpectedPos, diff2); // Add a new line in the same position, then move the existing line to maintain the selection GameLine newLine; if (!Settings.forwardLinesAsScenery && (direction > 0 || !Settings.recededLinesAsScenery)) { switch (selection.line.Type) { case LineType.Red: newLine = new RedLine(nextP1, nextP2, ((RedLine)selectedLine).inv); break; case LineType.Blue: newLine = new StandardLine(nextP1, nextP2, ((StandardLine)selectedLine).inv); break; case LineType.Scenery: newLine = new SceneryLine(nextP1, nextP2); break; default: newLine = new SceneryLine(nextP1, nextP2); break; } } else { newLine = new SceneryLine(nextP1, nextP2); } newLines.Add(newLine); } var selectTool = CurrentTools.SelectTool; foreach (GameLine newLine in newLines) { trackWriter.AddLine(newLine); } selectTool.SelectLines(newLines); if (isCompleteAction) { window.Track.UndoManager.EndAction(); window.Track.NotifyTrackChanged(); } }