Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using Microsoft.ParallelArrays;
- using FPA = Microsoft.ParallelArrays.FloatParallelArray;
- using PA = Microsoft.ParallelArrays.ParallelArrays;
- namespace Bodies
- {
- public class Test : IDisposable
- {
- public float[] xPos { get; private set; }
- public float[] yPos { get; private set; }
- public float[] xVel { get; private set; }
- public float[] yVel { get; private set; }
- public float[] mass { get; private set; }
- public float dec = 0.9999f;
- public float g = 1.0f;
- DX9Target target;
- public int n { get; private set; }
- public Test(int n)
- {
- target = new DX9Target();
- this.n = n;
- Random r = new Random();
- xPos = new float[n];
- yPos = new float[n];
- xVel = new float[n];
- yVel = new float[n];
- mass = new float[n];
- for (int i = 0; i < n; i++)
- {
- xPos[i] = r.Next(-100, 100);
- yPos[i] = r.Next(-100, 100);
- xVel[i] = r.Next(-10, 10);
- yVel[i] = r.Next(-10, 10);
- mass[i] = (float)(r.NextDouble() + 0.5);
- }
- }
- public void Dispose()
- {
- target.Dispose();
- }
- public void Tick()
- {
- FPA fxPos = new FPA(xPos);
- FPA fyPos = new FPA(yPos);
- FPA fxVel = new FPA(xVel);
- FPA fyVel = new FPA(yVel);
- FPA fMass = new FPA(mass);
- float[] xUpd = new float[n]; // x-update for velocity
- float[] yUpd = new float[n]; // y-update for velocity
- for (int i = 0; i < n; i++)
- {
- // x- and y-pos about point i:
- FPA ixPos = PA.Subtract(fxPos, xPos[i]);
- FPA iyPos = PA.Subtract(fyPos, yPos[i]);
- // radius from point i:
- FPA iRad = PA.Sqrt(PA.Add(PA.Multiply(ixPos, ixPos), PA.Multiply(iyPos, iyPos)));
- // x- and y-pos are now unit vectors:
- ixPos = PA.Divide(ixPos, iRad);
- iyPos = PA.Divide(iyPos, iRad);
- // vectors are now scaled by mass:
- ixPos = PA.Multiply(fMass, ixPos);
- iyPos = PA.Multiply(fMass, iyPos);
- // vectors are now scaled by G and deceleration:
- ixPos = PA.Multiply(dec * g, ixPos);
- iyPos = PA.Multiply(dec * g, iyPos);
- // sum to get ith update value:
- xUpd[i] = target.ToArray1D(PA.Sum(ixPos))[0];
- yUpd[i] = target.ToArray1D(PA.Sum(iyPos))[0];
- }
- FPA fxUpd = new FPA(xUpd);
- FPA fyUpd = new FPA(yUpd);
- // update velocities:
- fxVel = PA.Add(fxUpd, fxVel);
- fyVel = PA.Add(fyUpd, fyVel);
- // update positions:
- fxPos = PA.Add(fxVel, fxPos);
- fyPos = PA.Add(fyVel, fyPos);
- xPos = target.ToArray1D(fxPos);
- yPos = target.ToArray1D(fyPos);
- xVel = target.ToArray1D(fxVel);
- yVel = target.ToArray1D(fyVel);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement