This repository has been archived by the owner on Oct 7, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SwarmMember.cs
95 lines (75 loc) · 2.68 KB
/
SwarmMember.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
using System;
using System.Drawing;
using System.Collections.Generic;
using System.Text;
namespace Oodles
{
public class SwarmMember
{
public float thrust = 2.5f;
private float angleAcceleration = 0.6f; // the smaller the swarm, the higher the acceleration (makes it look more compact)
private float angleVelocityDamp = 0.9f;
private float velocityDamp = 0.8f;
private float xPos = 0.0f;
private float yPos = 0.0f;
private float xVel = 0.0f;
private float yVel = 0.0f;
public float rotationAngle = 0.0f;
private float angleVelocity = 0.0f;
private float lifePercentage = 1.0f;
public void Update(PointF target)
{
// dx, dy (direction towards the target)
float dX = target.X - xPos;
float dY = target.Y - yPos;
// calculate angle in radians to target
double angle = Math.Atan2(dY, dX);
double angDiff = angle - (rotationAngle / (180 / Math.PI));
// find the shortest direction to rotate
if (angDiff > Math.PI)
angDiff -= Math.PI * 2;
if (angDiff < -Math.PI)
angDiff += Math.PI * 2;
// limit rotation (angDiff) speed to some max speed (0.0029 radians)
if (Math.Abs(angDiff) > angleAcceleration)
angDiff *= angleAcceleration / Math.Abs(angDiff);
// add angular velocity to rotation and damp angular velocity:
angleVelocity += (float)angDiff;
rotationAngle += (float)((angleVelocity *= angleVelocityDamp) * (180 / Math.PI));
// increace velocity in the direction headed for:
double rads = rotationAngle / (180 / Math.PI);
xVel += ((float)Math.Cos(rads) * thrust);
yVel += ((float)Math.Sin(rads) * thrust);
// add velocity to position and damp velocities
xPos += (xVel *= velocityDamp);
yPos += (yVel *= velocityDamp);
// shorten life of this member
lifePercentage -= 0.0005f;
}
public float X
{
get { return xPos; }
set { xPos = value; }
}
public float Y
{
get { return yPos; }
set { yPos = value; }
}
public float VelocityX
{
get { return xVel; }
set { xVel = value; }
}
public float VelocityY
{
get { return yVel; }
set { yVel = value; }
}
public float LifePercentage
{
get { return lifePercentage; }
set { lifePercentage = value; }
}
}
}