Advertisement
Kaltinril

Movement Pattern

Apr 12th, 2012
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.91 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Microsoft.Xna.Framework;
  6. using Microsoft.Xna.Framework.Content;
  7. using Microsoft.Xna.Framework.Graphics;
  8.  
  9. namespace TopFalling3
  10. {
  11.     abstract class Movement
  12.     {
  13.  
  14.         public Movement()
  15.         {
  16.  
  17.         }
  18.         public abstract void Update(GameTime gameTime,
  19.                            Rectangle gameViewPort,
  20.                            GameObject movingObject,
  21.                            Vector2 newPosition);
  22.  
  23.         //Standard moving along the rotation
  24.         public void Update(GameTime gameTime, Rectangle gameViewPort, GameObject movingObject)
  25.         {
  26.             //Add the velocity * the direction
  27.             movingObject.position.X += movingObject.Velocity.X * (float)(Math.Cos(movingObject.rotation));
  28.             movingObject.position.Y += movingObject.Velocity.Y * (float)(Math.Sin(movingObject.rotation));
  29.  
  30.         }
  31.  
  32.         public bool ReachedWaypoint(GameObject movingObject,
  33.                                     Vector2 newPosition)
  34.         {
  35.             if (Math.Abs((newPosition.X - movingObject.position.X)) < (movingObject.Velocity.X))
  36.             {//within X
  37.                 if (Math.Abs((newPosition.Y - movingObject.position.Y)) < (movingObject.Velocity.Y))
  38.                 {//within Y
  39.                     return true;
  40.                 }
  41.             }
  42.             return false;
  43.         }
  44.  
  45.     }
  46. }
  47.  
  48.  
  49.  
  50. using System;
  51. using System.Collections.Generic;
  52. using System.Linq;
  53. using System.Text;
  54. using Microsoft.Xna.Framework;
  55. using Microsoft.Xna.Framework.Content;
  56. using Microsoft.Xna.Framework.Graphics;
  57.  
  58. namespace TopFalling3
  59. {
  60.     class InitialVector : Movement
  61.     {
  62.         public InitialVector()
  63.         {
  64.             //This movement is based soley off the inital rotation/vector of the bullet.
  65.         }
  66.  
  67.         //move from old point, to new point.
  68.         public override void Update(GameTime gameTime,
  69.                            Rectangle gameViewPort,
  70.                            GameObject movingObject,
  71.                            Vector2 newPosition)
  72.         {
  73.             //Add the velocity * the direction
  74.             movingObject.position.X += movingObject.Velocity.X * (float)(Math.Cos(movingObject.rotation));
  75.             movingObject.position.Y += movingObject.Velocity.Y * (float)(Math.Sin(movingObject.rotation));
  76.         }
  77.     }
  78. }
  79.  
  80.  
  81.  
  82.  
  83. using System;
  84. using System.Collections.Generic;
  85. using System.Linq;
  86. using System.Text;
  87. using Microsoft.Xna.Framework;
  88. using Microsoft.Xna.Framework.Content;
  89. using Microsoft.Xna.Framework.Graphics;
  90.  
  91. namespace TopFalling3
  92. {
  93.     class DM_MaxTurning : Movement
  94.     {
  95.  
  96.         public DM_MaxTurning()
  97.         {
  98.  
  99.         }
  100.  
  101.         //move from old point, to new point.
  102.         public override void Update(GameTime gameTime,
  103.                            Rectangle gameViewPort,
  104.                            GameObject movingObject,
  105.                            Vector2 newPosition)
  106.         {
  107.             //Get the vector to move from 1 point to the other
  108.             float dist_to_waypoint_x = newPosition.X - movingObject.position.X;//currentPosition.X - newPosition.X;
  109.             float dist_to_waypoint_y = newPosition.Y - movingObject.position.Y; //currentPosition.Y - newPosition.Y;
  110.  
  111.             //Atan is the Arc-Tangent
  112.             //Tan is the Tangent (angle in radians with the given y/x)
  113.             //Store the new rotation
  114.             float adjustToRotation =  (float)Math.Atan2(dist_to_waypoint_y,dist_to_waypoint_x);
  115.  
  116.             float diffBetweenRotations = adjustToRotation - movingObject.rotation;
  117.  
  118.             if (diffBetweenRotations > 0)
  119.             {
  120.                 movingObject.rotation += movingObject.maxRotationSpeed;
  121.                 diffBetweenRotations = adjustToRotation - movingObject.rotation;
  122.                 if (diffBetweenRotations < 0) //If the rotation changed past the adjustToRotation, set it equal to it
  123.                     movingObject.rotation = adjustToRotation;
  124.                
  125.             }
  126.             else if (diffBetweenRotations < 0)
  127.             {
  128.                 movingObject.rotation -= movingObject.maxRotationSpeed;
  129.                 diffBetweenRotations = adjustToRotation - movingObject.rotation;
  130.                 if (diffBetweenRotations > 0) //If the rotation changed past the adjustToRotation, set it equal to it
  131.                     movingObject.rotation = adjustToRotation;
  132.             }
  133.             else
  134.             { } //Do nothing
  135.  
  136.             //movingObject.rotation += -(float)Math.PI / 2;
  137.  
  138.             //Add the velocity * the direction
  139.             movingObject.position.X += movingObject.Velocity.X * (float)(Math.Cos(movingObject.rotation));
  140.             movingObject.position.Y += movingObject.Velocity.Y * (float)(Math.Sin(movingObject.rotation));
  141.            
  142.             //movingObject.rotation += (float)Math.PI / 2;
  143.            
  144.  
  145.         }
  146.  
  147.  
  148.  
  149.     }
  150. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement