00001
00002
00003 namespace NewGamePhysics.GraphicalElements
00004 {
00005 using System;
00006 using System.Collections.Generic;
00007 using System.Text;
00008
00009 using Microsoft.Xna.Framework;
00010 using Microsoft.Xna.Framework.Content;
00011 using Microsoft.Xna.Framework.Graphics;
00012 using Microsoft.Xna.Framework.Input;
00013
00014 using NewGamePhysics.StateManager;
00015 using NewGamePhysics.Utilities;
00016 using NewGamePhysics.PhysicalElements;
00017 using NewGamePhysics.Mathematics;
00018 using NewGamePhysics.Physics;
00019
00023 public class DoubleSquarePendulum : GraphicalElementBase
00024 {
00028 DoubleSquarePendulumSimulation pendulum;
00029
00033 Vector2 screenOrigin;
00034
00038 double screenScale;
00039
00043 private static Texture2D blockTexture;
00044
00048 private static Texture2D rivetTexture;
00049
00060 public DoubleSquarePendulum(
00061 ScreenManager screenManager,
00062 Vector2 simulationOrigin,
00063 double l,
00064 double m,
00065 double g,
00066 RotationalFrictionType f,
00067 Vector2 screenOrigin,
00068 double screenScale)
00069 : base(screenManager)
00070 {
00071
00072 this.pendulum = new DoubleSquarePendulumSimulation(simulationOrigin, l, m, g, f);
00073 this.screenOrigin = screenOrigin;
00074 this.screenScale = screenScale;
00075
00076
00077 if (this.ScreenManager.Textures.ContainsKey("rivet"))
00078 {
00079 rivetTexture = this.ScreenManager.Textures["rivet"];
00080 }
00081
00082 if (this.ScreenManager.Textures.ContainsKey("metal"))
00083 {
00084 blockTexture = this.ScreenManager.Textures["metal"];
00085 }
00086
00087 }
00088
00092 public DoubleSquarePendulumSimulation Pendulum
00093 {
00094 get { return this.pendulum; }
00095 }
00096
00101 public void Draw(GameTime gameTime)
00102 {
00103
00104 double[] pendulumSizes = this.pendulum.GetSize(this.screenScale);
00105 double[] pendulumAngles = this.pendulum.GetAngle();
00106 Vector2[] pendulumPoints = this.pendulum.GetPosition(this.screenOrigin, this.screenScale);
00107
00108
00109 Rectangle dest = new Rectangle();
00110
00111
00112 if (null != blockTexture)
00113 {
00114 this.SpriteBatch.Begin(SpriteBlendMode.AlphaBlend);
00115 dest = new Rectangle();
00116 dest.X = Convert.ToInt32(Math.Round(pendulumPoints[0].X));
00117 dest.Y = Convert.ToInt32(Math.Round(pendulumPoints[0].Y));
00118 dest.Width = (int)pendulumSizes[0];
00119 dest.Height = dest.Width;
00120 this.SpriteBatch.Draw(
00121 blockTexture,
00122 dest,
00123 null,
00124 Color.White,
00125 (float)(Math.PI / 4 - pendulumAngles[0]),
00126 new Vector2(),
00127 SpriteEffects.None,
00128 (float)0.0);
00129 this.SpriteBatch.End();
00130 }
00131
00132
00133 this.PrimitiveBatch.Begin(PrimitiveType.LineList);
00134 this.PrimitiveBatch.AddVertex(pendulumPoints[0], Color.Black);
00135 this.PrimitiveBatch.AddVertex(pendulumPoints[1], Color.Black);
00136 this.PrimitiveBatch.AddVertex(pendulumPoints[1], Color.Black);
00137 this.PrimitiveBatch.AddVertex(pendulumPoints[2], Color.Black);
00138 this.PrimitiveBatch.AddVertex(pendulumPoints[2], Color.Black);
00139 this.PrimitiveBatch.AddVertex(pendulumPoints[3], Color.Black);
00140 this.PrimitiveBatch.AddVertex(pendulumPoints[3], Color.Black);
00141 this.PrimitiveBatch.AddVertex(pendulumPoints[0], Color.Black);
00142 this.PrimitiveBatch.End();
00143
00144
00145 if (null != rivetTexture)
00146 {
00147 this.SpriteBatch.Begin(SpriteBlendMode.AlphaBlend);
00148 dest.X -= 4;
00149 dest.Y -= 4;
00150 dest.Width = 8;
00151 dest.Height = 8;
00152 this.SpriteBatch.Draw(
00153 rivetTexture,
00154 dest,
00155 Color.White);
00156 this.SpriteBatch.End();
00157 }
00158
00159
00160 if (null != blockTexture)
00161 {
00162 this.SpriteBatch.Begin(SpriteBlendMode.AlphaBlend);
00163 dest.X = Convert.ToInt32(Math.Round(pendulumPoints[4].X));
00164 dest.Y = Convert.ToInt32(Math.Round(pendulumPoints[4].Y));
00165 dest.Width = (int)pendulumSizes[1];
00166 dest.Height = dest.Width;
00167 this.SpriteBatch.Draw(
00168 blockTexture,
00169 dest,
00170 null,
00171 Color.White,
00172 (float)(Math.PI / 4 - pendulumAngles[1]),
00173 new Vector2(),
00174 SpriteEffects.None,
00175 (float)0.0);
00176 this.SpriteBatch.End();
00177 }
00178
00179
00180 this.PrimitiveBatch.Begin(PrimitiveType.LineList);
00181 this.PrimitiveBatch.AddVertex(pendulumPoints[4], Color.Black);
00182 this.PrimitiveBatch.AddVertex(pendulumPoints[5], Color.Black);
00183 this.PrimitiveBatch.AddVertex(pendulumPoints[5], Color.Black);
00184 this.PrimitiveBatch.AddVertex(pendulumPoints[6], Color.Black);
00185 this.PrimitiveBatch.AddVertex(pendulumPoints[6], Color.Black);
00186 this.PrimitiveBatch.AddVertex(pendulumPoints[7], Color.Black);
00187 this.PrimitiveBatch.AddVertex(pendulumPoints[7], Color.Black);
00188 this.PrimitiveBatch.AddVertex(pendulumPoints[4], Color.Black);
00189 this.PrimitiveBatch.End();
00190
00191
00192 if (null != rivetTexture)
00193 {
00194 this.SpriteBatch.Begin(SpriteBlendMode.AlphaBlend);
00195 dest.X -= 4;
00196 dest.Y -= 4;
00197 dest.Width = 8;
00198 dest.Height = 8;
00199 this.SpriteBatch.Draw(
00200 rivetTexture,
00201 dest,
00202 Color.White);
00203 this.SpriteBatch.End();
00204 }
00205 }
00206 }
00207 }
00208
00209