Commits

Juha Komulainen committed 48dbc31

Added ballistic-demo.

  • Participants
  • Parent commits f6068bd

Comments (0)

Files changed (9)

 syntax: glob
 Libs
 bin
+.DS_Store
+*.pidb
+*.userprefs

CycloNet.Physics.Demos/AssemblyInfo.cs

+using System.Reflection;
+using System.Runtime.CompilerServices;
+
+// Information about this assembly is defined by the following attributes. 
+// Change them to the values specific to your project.
+
+[assembly: AssemblyTitle("CycloNet.Physics.Demos")]
+[assembly: AssemblyDescription("")]
+[assembly: AssemblyConfiguration("")]
+[assembly: AssemblyCompany("")]
+[assembly: AssemblyProduct("")]
+[assembly: AssemblyCopyright("")]
+[assembly: AssemblyTrademark("")]
+[assembly: AssemblyCulture("")]
+
+// The assembly version has the format "{Major}.{Minor}.{Build}.{Revision}".
+// The form "{Major}.{Minor}.*" will automatically update the build and revision,
+// and "{Major}.{Minor}.{Build}.*" will update just the revision.
+
+[assembly: AssemblyVersion("1.0.*")]
+
+// The following attributes are used to specify the signing key for the assembly, 
+// if desired. See the Mono documentation for more information about signing.
+
+//[assembly: AssemblyDelaySign(false)]
+//[assembly: AssemblyKeyFile("")]
+

CycloNet.Physics.Demos/Ballistic/BallisticDemo.cs

+using System;
+using System.Collections.Generic;
+using System.Linq;
+using OpenTK;
+using OpenTK.Graphics;
+using OpenTK.Graphics.OpenGL;
+using Tao.FreeGlut;
+using CycloNet.Physics.Particles;
+
+namespace CycloNet.Physics.Demos.Ballistic
+{
+    enum ShotType
+    {
+        Pistol, Artillery, Fireball, Laser
+    };
+
+    /// <summary>
+    /// Holds a single ammunition round record.
+    /// </summary>
+    class AmmoRound
+    {
+        public readonly Particle Particle = new Particle();
+        public readonly ShotType type;
+        public DateTime StartTime;
+
+        public AmmoRound(ShotType type)
+        {
+            this.type = type;
+
+            switch (type)
+            {
+            case ShotType.Pistol:
+                Particle.Mass = 2.0f; // 2.0kg
+                Particle.Velocity = new Vector3(0.0f, 0.0f, 35.0f); // 35m/s
+                Particle.Acceleration = new Vector3(0, -1, 0);
+                Particle.Damping = 0.99f;
+                break;
+
+            case ShotType.Artillery:
+                Particle.Mass = 200.0f; // 200.0kg
+                Particle.Velocity = new Vector3(0.0f, 30.0f, 40.0f); // 50m/s
+                Particle.Acceleration = new Vector3(0, -20, 0);
+                Particle.Damping = 0.99f;
+                break;
+
+            case ShotType.Fireball:
+                Particle.Mass = 1.0f; // 1.0kg - mostly blast damage
+                Particle.Velocity = new Vector3(0.0f, 0.0f, 10.0f); // 5m/s
+                Particle.Acceleration = new Vector3(0, 0.6f, 0); // floats up
+                Particle.Damping = 0.9f;
+                break;
+
+            case ShotType.Laser:
+                // Note that this is the kind of laser bolt seen in films,
+                // not a realistic laser beam!
+                Particle.Mass = 0.1f;  // 0.1kg - almost no weight
+                Particle.Velocity = new Vector3(0.0f, 0.0f, 100.0f); // 100m/s
+                Particle.Acceleration = new Vector3(0.0f, 0.0f, 0.0f); // No gravity
+                Particle.Damping = 0.99f;
+                break;
+            }
+
+            // Set the data common to all particle types
+            Particle.Position = new Vector3(0.0f, 1.5f, 0.0f);
+            StartTime = TimingData.LastFrameTimestamp;
+
+            // Clear the force accumulators
+            Particle.ClearAccumulator();
+        }
+
+        public void Render()
+        {
+            var position = Particle.Position;
+
+            GL.Color4(Color4.Black);
+            GL.PushMatrix();
+            GL.Translate(position);
+
+            Glut.glutSolidSphere(0.3f, 5, 4);
+
+            GL.PopMatrix();
+
+            GL.Color4(new Color4(0.75f, 0.75f, 0.75f, 1.0f));
+            GL.PushMatrix();
+
+            GL.Translate(position.X, 0, position.Z);
+            GL.Scale(1, 0.1f, 1);
+
+            Glut.glutSolidSphere(0.6f, 5, 4);
+
+            GL.PopMatrix();
+        }
+    };
+
+    public class BallisticDemo : DemoApplication
+    {
+        List<AmmoRound> ammo = new List<AmmoRound>();
+
+        public BallisticDemo()
+        {
+            Title = "CycloNet > Ballistic Demo";
+        }
+
+        void Fire(ShotType shotType)
+        {
+            if (ammo.Count < 64)
+                ammo.Add(new AmmoRound(shotType));
+        }
+
+        protected override void DoUpdate(float duration)
+        {
+            // Update the physics of each particle in turn
+            foreach (var shot in ammo)
+            {
+                // Run the physics
+                shot.Particle.Integrate(duration);
+            }
+
+            // Check if the particle is now invalid
+            ammo.RemoveAll(shot =>
+                    shot.Particle.Position.Y < 0.0f ||
+                    shot.Particle.Position.Z > 200f ||
+                    false //shot.StartTime+5000 < TimingData::get().lastFrameTimestamp);
+                               );
+        }
+
+        protected override void DoRender()
+        {
+            GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
+
+            GL.MatrixMode(MatrixMode.Modelview);
+            var camera = Matrix4.LookAt(-25.0f, 8.0f, 5.0f,
+                                        0.0f, 5.0f, 22.0f,
+                                        0.0f, 1.0f, 0.0f);
+            GL.LoadMatrix(ref camera);
+
+            // Draw a sphere at the firing point, and add a shadow projected
+            // onto the ground plane.
+            GL.Color4(Color4.Black);
+            GL.PushMatrix();
+
+            GL.Translate(0.0f, 1.5f, 0.0f);
+            Glut.glutSolidSphere(0.1f, 5, 5);
+            GL.Translate(0.0f, -1.5f, 0.0f);
+            GL.Color3(0.75f, 0.75f, 0.75f);
+            GL.Scale(1.0f, 0.1f, 1.0f);
+            Glut.glutSolidSphere(0.1f, 5, 5);
+            GL.PopMatrix();
+
+
+            // Draw some scale lines
+            GL.Color3(0.75f, 0.75f, 0.75f);
+            GL.Begin(BeginMode.Lines);
+            for (int i = 0; i < 200; i += 10)
+            {
+                GL.Vertex3(-5.0f, 0.0f, i);
+                GL.Vertex3(5.0f, 0.0f, i);
+            }
+            GL.End();
+
+            // Render each particle in turn
+            foreach (var shot in ammo)
+                shot.Render();
+        }
+
+        protected override void OnKeyPress(KeyPressEventArgs e)
+        {
+            switch (e.KeyChar)
+            {
+            case '1':
+                Fire(ShotType.Pistol);
+                break;
+            case '2':
+                Fire(ShotType.Artillery);
+                break;
+            case '3':
+                Fire(ShotType.Fireball);
+                break;
+            case '4':
+                Fire(ShotType.Laser);
+                break;
+            }
+        }
+    }
+
+#if false
+
+
+void BallisticDemo::display()
+{
+
+    // Render the description
+    glColor3f(0.0f, 0.0f, 0.0f);
+    renderText(10.0f, 34.0f, "Click: Fire\n1-4: Select Ammo");
+
+    // Render the name of the current shot type
+    switch(currentShotType)
+    {
+    case PISTOL: renderText(10.0f, 10.0f, "Current Ammo: Pistol"); break;
+    case ARTILLERY: renderText(10.0f, 10.0f, "Current Ammo: Artillery"); break;
+    case FIREBALL: renderText(10.0f, 10.0f, "Current Ammo: Fireball"); break;
+    case LASER: renderText(10.0f, 10.0f, "Current Ammo: Laser"); break;
+    }
+}
+
+void BallisticDemo::mouse(int button, int state, int x, int y)
+{
+    // Fire the current weapon.
+    if (state == GLUT_DOWN) fire();
+}
+
+void BallisticDemo::key(unsigned char key)
+{
+    switch(key)
+    {
+    case '1': currentShotType = PISTOL; break;
+    case '2': currentShotType = ARTILLERY; break;
+    case '3': currentShotType = FIREBALL; break;
+    case '4': currentShotType = LASER; break;
+    }
+}
+#endif
+}
+

CycloNet.Physics.Demos/CycloNet.Physics.Demos.csproj

+<?xml version="1.0" encoding="utf-8"?>
+<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+  <PropertyGroup>
+    <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
+    <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
+    <ProductVersion>10.0.0</ProductVersion>
+    <SchemaVersion>2.0</SchemaVersion>
+    <ProjectGuid>{9B40EE9A-9B6B-4B47-8DFC-7495A3B4EDF1}</ProjectGuid>
+    <OutputType>Exe</OutputType>
+    <RootNamespace>CycloNet.Physics.Demos</RootNamespace>
+    <AssemblyName>CycloNet.Physics.Demos</AssemblyName>
+    <TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
+  </PropertyGroup>
+  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
+    <DebugSymbols>true</DebugSymbols>
+    <DebugType>full</DebugType>
+    <Optimize>false</Optimize>
+    <OutputPath>bin\Debug</OutputPath>
+    <DefineConstants>DEBUG</DefineConstants>
+    <ErrorReport>prompt</ErrorReport>
+    <WarningLevel>4</WarningLevel>
+    <Externalconsole>true</Externalconsole>
+  </PropertyGroup>
+  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
+    <DebugType>none</DebugType>
+    <Optimize>false</Optimize>
+    <OutputPath>bin\Release</OutputPath>
+    <ErrorReport>prompt</ErrorReport>
+    <WarningLevel>4</WarningLevel>
+    <Externalconsole>true</Externalconsole>
+  </PropertyGroup>
+  <ItemGroup>
+    <Reference Include="System" />
+    <Reference Include="OpenTK, Version=1.0.0.0, Culture=neutral, PublicKeyToken=bad199fe84eb3df4">
+      <SpecificVersion>False</SpecificVersion>
+      <HintPath>..\Libs\OpenTK.dll</HintPath>
+    </Reference>
+    <Reference Include="System.Drawing" />
+    <Reference Include="System.Core" />
+    <Reference Include="Tao.FreeGlut, Version=2.4.0.2, Culture=neutral, PublicKeyToken=6e602a6ad6c0d06d">
+      <SpecificVersion>False</SpecificVersion>
+      <HintPath>..\Libs\Tao.FreeGlut.dll</HintPath>
+    </Reference>
+  </ItemGroup>
+  <ItemGroup>
+    <Compile Include="Main.cs" />
+    <Compile Include="AssemblyInfo.cs" />
+    <Compile Include="DemoApplication.cs" />
+    <Compile Include="Ballistic\BallisticDemo.cs" />
+    <Compile Include="TimingData.cs" />
+  </ItemGroup>
+  <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
+  <ItemGroup>
+    <ProjectReference Include="..\CycloNet.Physics\CycloNet.Physics.csproj">
+      <Project>{25F61EE2-0710-4376-A40F-EC3BA1236080}</Project>
+      <Name>CycloNet.Physics</Name>
+    </ProjectReference>
+  </ItemGroup>
+  <ItemGroup>
+    <Folder Include="Ballistic\" />
+  </ItemGroup>
+</Project>

CycloNet.Physics.Demos/DemoApplication.cs

+using System;
+using OpenTK;
+using OpenTK.Graphics;
+using OpenTK.Input;
+using OpenTK.Graphics.OpenGL;
+
+namespace CycloNet.Physics.Demos
+{
+    public abstract class DemoApplication : GameWindow
+    {
+        public DemoApplication():
+            base(800, 600)
+        {
+            VSync = VSyncMode.On;
+        }
+
+        protected override void OnLoad(EventArgs e)
+        {
+            GL.ClearColor(new Color4(0.9f, 0.95f, 1.0f, 1.0f));
+            GL.Enable(EnableCap.DepthTest);
+            GL.ShadeModel(ShadingModel.Smooth);
+        }
+
+        protected override void OnResize(EventArgs e)
+        {
+            base.OnResize(e);
+
+            GL.Viewport(0, 0, Width, Height);
+
+            GL.MatrixMode(MatrixMode.Projection);
+
+            var aspect = (float) Width / (float) Height;
+
+            var projection = Matrix4.CreatePerspectiveFieldOfView(MathHelper.DegreesToRadians(60), aspect, 1.0f, 500.0f);
+            GL.LoadMatrix(ref projection);
+
+            GL.MatrixMode(MatrixMode.Modelview);
+        }
+
+        protected override void OnRenderFrame(FrameEventArgs e)
+        {
+            DoRender();
+
+            SwapBuffers();
+        }
+
+        protected override void OnUpdateFrame(FrameEventArgs e)
+        {
+            if (Keyboard[Key.Escape])
+            {
+                Exit();
+                return;
+            }
+
+            TimingData.Update();
+
+            DoUpdate((float) e.Time);
+        }
+
+        protected virtual void DoUpdate(float elapsed)
+        {
+        }
+
+        protected virtual void DoRender()
+        {
+            GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
+
+            GL.MatrixMode(MatrixMode.Modelview);
+            GL.Begin(BeginMode.Lines);
+            GL.Vertex2(1, 1);
+            GL.Vertex2(639, 319);
+            GL.End();
+        }
+    }
+}
+

CycloNet.Physics.Demos/Main.cs

+using System;
+
+namespace CycloNet.Physics.Demos
+{
+    class MainClass
+    {
+        public static void Main(string[] args)
+        {
+            using (var win = new Ballistic.BallisticDemo())
+            {
+                win.Run();
+            }
+        }
+    }
+}
+

CycloNet.Physics.Demos/TimingData.cs

+using System;
+namespace CycloNet.Physics.Demos
+{
+    public class TimingData
+    {
+        public static DateTime LastFrameTimestamp;
+
+        public static void Update()
+        {
+            LastFrameTimestamp = DateTime.Now;
+        }
+    }
+}
+

CycloNet.Physics/Particles/Particle.cs

             get { return InverseMass >= 0.0f; }
         }
 
-        internal void ClearAccumulator()
+        public void ClearAccumulator()
         {
             forceAccum = Vector3.Zero;
         }
 # Visual Studio 2010
 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CycloNet.Physics", "CycloNet.Physics\CycloNet.Physics.csproj", "{25F61EE2-0710-4376-A40F-EC3BA1236080}"
 EndProject
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CycloNet.Physics.Demos", "CycloNet.Physics.Demos\CycloNet.Physics.Demos.csproj", "{9B40EE9A-9B6B-4B47-8DFC-7495A3B4EDF1}"
+EndProject
 Global
 	GlobalSection(SolutionConfigurationPlatforms) = preSolution
 		Debug|Any CPU = Debug|Any CPU
 		{25F61EE2-0710-4376-A40F-EC3BA1236080}.Debug|Any CPU.Build.0 = Debug|Any CPU
 		{25F61EE2-0710-4376-A40F-EC3BA1236080}.Release|Any CPU.ActiveCfg = Release|Any CPU
 		{25F61EE2-0710-4376-A40F-EC3BA1236080}.Release|Any CPU.Build.0 = Release|Any CPU
+		{9B40EE9A-9B6B-4B47-8DFC-7495A3B4EDF1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+		{9B40EE9A-9B6B-4B47-8DFC-7495A3B4EDF1}.Debug|Any CPU.Build.0 = Debug|Any CPU
+		{9B40EE9A-9B6B-4B47-8DFC-7495A3B4EDF1}.Release|Any CPU.ActiveCfg = Release|Any CPU
+		{9B40EE9A-9B6B-4B47-8DFC-7495A3B4EDF1}.Release|Any CPU.Build.0 = Release|Any CPU
 	EndGlobalSection
 	GlobalSection(MonoDevelopProperties) = preSolution
-		StartupItem = CycloNet.Physics\CycloNet.Physics.csproj
+		StartupItem = CycloNet.Physics.Demos\CycloNet.Physics.Demos.csproj
 	EndGlobalSection
 EndGlobal