Advertisement
Guest User

Untitled

a guest
Sep 19th, 2012
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.24 KB | None | 0 0
  1. using System;
  2.  
  3. using Microsoft.ParallelArrays;
  4. using FPA = Microsoft.ParallelArrays.FloatParallelArray;
  5. using PA  = Microsoft.ParallelArrays.ParallelArrays;
  6.  
  7. namespace Bodies
  8. {
  9.     public class Test : IDisposable
  10.     {
  11.         public float[] xPos { get; private set; }
  12.         public float[] yPos { get; private set; }
  13.         public float[] xVel { get; private set; }
  14.         public float[] yVel { get; private set; }
  15.         public float[] mass { get; private set; }
  16.  
  17.         public float dec = 0.9999f;
  18.         public float g = 1.0f;
  19.  
  20.         DX9Target target;
  21.  
  22.         public int n { get; private set; }
  23.  
  24.         public Test(int n)
  25.         {
  26.             target = new DX9Target();
  27.  
  28.             this.n = n;
  29.             Random r = new Random();
  30.  
  31.             xPos = new float[n];
  32.             yPos = new float[n];
  33.             xVel = new float[n];
  34.             yVel = new float[n];
  35.             mass = new float[n];
  36.  
  37.             for (int i = 0; i < n; i++)
  38.             {
  39.                 xPos[i] = r.Next(-100, 100);
  40.                 yPos[i] = r.Next(-100, 100);
  41.                 xVel[i] = r.Next(-10, 10);
  42.                 yVel[i] = r.Next(-10, 10);
  43.                 mass[i] = (float)(r.NextDouble() + 0.5);
  44.             }
  45.         }
  46.  
  47.         public void Dispose()
  48.         {
  49.             target.Dispose();
  50.         }
  51.  
  52.         public void Tick()
  53.         {
  54.             FPA fxPos = new FPA(xPos);
  55.             FPA fyPos = new FPA(yPos);
  56.             FPA fxVel = new FPA(xVel);
  57.             FPA fyVel = new FPA(yVel);
  58.             FPA fMass = new FPA(mass);
  59.  
  60.             float[] xUpd = new float[n]; // x-update for velocity
  61.             float[] yUpd = new float[n]; // y-update for velocity
  62.  
  63.             for (int i = 0; i < n; i++)
  64.             {
  65.                 // x- and y-pos about point i:
  66.                 FPA ixPos = PA.Subtract(fxPos, xPos[i]);
  67.                 FPA iyPos = PA.Subtract(fyPos, yPos[i]);
  68.                
  69.                 // radius from point i:
  70.                 FPA iRad = PA.Sqrt(PA.Add(PA.Multiply(ixPos, ixPos), PA.Multiply(iyPos, iyPos)));
  71.  
  72.                 // x- and y-pos are now unit vectors:
  73.                 ixPos = PA.Divide(ixPos, iRad);
  74.                 iyPos = PA.Divide(iyPos, iRad);
  75.  
  76.                 // vectors are now scaled by mass:
  77.                 ixPos = PA.Multiply(fMass, ixPos);
  78.                 iyPos = PA.Multiply(fMass, iyPos);
  79.  
  80.                 // vectors are now scaled by G and deceleration:
  81.                 ixPos = PA.Multiply(dec * g, ixPos);
  82.                 iyPos = PA.Multiply(dec * g, iyPos);
  83.  
  84.                 // sum to get ith update value:
  85.                 xUpd[i] = target.ToArray1D(PA.Sum(ixPos))[0];
  86.                 yUpd[i] = target.ToArray1D(PA.Sum(iyPos))[0];
  87.             }
  88.  
  89.             FPA fxUpd = new FPA(xUpd);
  90.             FPA fyUpd = new FPA(yUpd);
  91.  
  92.             // update velocities:
  93.             fxVel = PA.Add(fxUpd, fxVel);
  94.             fyVel = PA.Add(fyUpd, fyVel);
  95.  
  96.             // update positions:
  97.             fxPos = PA.Add(fxVel, fxPos);
  98.             fyPos = PA.Add(fyVel, fyPos);
  99.  
  100.             xPos = target.ToArray1D(fxPos);
  101.             yPos = target.ToArray1D(fyPos);
  102.             xVel = target.ToArray1D(fxVel);
  103.             yVel = target.ToArray1D(fyVel);
  104.         }
  105.     }
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement