-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathPhysicsObject.java
More file actions
159 lines (137 loc) · 6.46 KB
/
PhysicsObject.java
File metadata and controls
159 lines (137 loc) · 6.46 KB
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import java.awt.Color;
public class PhysicsObject
{
public double diameter = 10;
public final Color defaultColor = Color.LIGHT_GRAY;
public Color color = defaultColor;
public Vector2 position = new Vector2(0, 0); //global position of the PhysicsObject as a vector
public Vector2 originalPosition = new Vector2(0, 0); //Position that was originally set for the PhysicsObject
//local position is always 0,0
public double mass = 10; //Mass of the PhysicsObject
public double originalMass = 10; //The mass that was originally set for the PhysicsObject
public double drag = 0.0001; //Coefficient of drag
public Vector2 velocity = new Vector2(0, 0); //Linear velocity of the PhysicsObject
public boolean isKinematic = false; //should this PhysicsObject be taken out of physics control?
public boolean kinOG = false;
public boolean freezeRotation = false; //freezes rotation if true
public boolean frzRotOG = false;
public boolean effectedByGravity = true; //should this PhysicsObject be effected by Gravity?
public boolean effctGvtyOG = true;
public boolean collisions = true;
public boolean collsnsOG = true;
public boolean gravitational = false; //should this PhysicsObject have its own graviational pull?
public boolean drawVelocity = true;
//#region constructors
public PhysicsObject()
{
GameManager.physicsObjects.add(this);
}
public PhysicsObject(Vector2 posIn, double massIn, boolean isKnemtcIn, boolean frzRotationIn, boolean effbyGravIn, boolean graviationalIn, boolean collisionsIn)
{
//adds this object to the list of physics objects in the game manager class when it is constructed
GameManager.physicsObjects.add(this);
position = new Vector2(posIn);
originalPosition = new Vector2(posIn);
mass = massIn;
originalMass = massIn;
isKinematic = kinOG = isKnemtcIn;
freezeRotation = frzRotOG = frzRotationIn;
effectedByGravity = effctGvtyOG = effbyGravIn;
gravitational = graviationalIn;
collisions = collsnsOG = collisionsIn;
}
public PhysicsObject(Vector2 posIn, double massIn)
{
GameManager.physicsObjects.add(this);
position = new Vector2(posIn);
originalPosition = new Vector2(posIn);
mass = massIn;
originalMass = massIn;
}
//#endregion
public void addForce(Vector2 forceIn, int forceType) //forceType 0 is force over time, forceType 1 is instant force applied
{
if (isKinematic) return;
if (forceType == 0)
{
velocity.x += (forceIn.x*GameManager.getDeltaTime())/mass;
velocity.y += (forceIn.y*GameManager.getDeltaTime())/mass;
}
else if (forceType == 1)
{
velocity.x += (forceIn.x)/mass;
velocity.y += (forceIn.y)/mass;
}
velocity.x *= (1 - drag);
velocity.y *= (1 - drag);
}
public Vector2[] getGravityVectors()
{
Vector2[] gravityVectors;
int counter = 0;
for (int i = 0; i < GameManager.physicsObjects.size(); i++) //applies gravity for each gravity object
{
if (Vector2.difference(position, GameManager.physicsObjects.get(i).position).getMagnitude() > 1)
{
counter ++;
}
}
gravityVectors = new Vector2[counter];
counter = 0;
for (int i = 0; i < GameManager.physicsObjects.size(); i++) //applies gravity for each gravity object
{
if (Vector2.difference(position, GameManager.physicsObjects.get(i).position).getMagnitude() > 1)
{
gravityVectors[counter] = Vector2.scaledDifference(position, GameManager.physicsObjects.get(i).position, -(GameManager.physicsObjects.get(i).mass*mass)/Math.pow(Vector2.distance(position, GameManager.physicsObjects.get(i).position), 2));
counter ++;
}
}
return gravityVectors;
}
//========================== METHODS USED IN PHYSICS UPDATES ============================
public void applyGravity() //applies gravity by adding a force twards each gravity object
{
if (!effectedByGravity) return;
int i = 0;
for (i = 0; i < GameManager.physicsObjects.size(); i++) //applies gravity for each gravity object
{
if (Vector2.difference(position, GameManager.physicsObjects.get(i).position).getMagnitude() > 1)
{
//System.out.print(Vector2.scaledDifference( position, GameManager.physicsObjects.get(i).position, -(GameManager.physicsObjects.get(i).mass*mass)/Math.pow(Vector2.distance(position, GameManager.physicsObjects.get(i).position), 2)).toString());
addForce( //adds force to this object based off the vector
Vector2.scaledDifference( //returns a vector that's angle is equal to the one inputed, then scaled proportionally to the specified magnitude
position, //position of this object's center of mass
GameManager.physicsObjects.get(i).position, //position of the gravity object
-(GameManager.physicsObjects.get(i).mass*mass)/Math.pow(Vector2.distance(position, GameManager.physicsObjects.get(i).position), 2)), 0);
//^ the magnitude of the force of gravity, calculated by the formula (m1*m2)/d^2
}
}
}
public void updatePosition() //changes the position of the opject based off velocity
{
position = Vector2.add(position, Vector2.multiply(velocity, GameManager.getDeltaTime()));
}
public void checkCollisions()
{
int i = 0;
for (i = 0; i < GameManager.physicsObjects.size(); i++) //checks collisions against each gravity object
{
if (GameManager.physicsObjects.get(i).collisions && GameManager.physicsObjects.get(i) != this)
{
//true if the distance between this object and the gravity object is less than the radius of gravity object
if (Vector2.distance(position, GameManager.physicsObjects.get(i).position) <= GameManager.physicsObjects.get(i).diameter/2)
{
//freezes the object
velocity.x = 0;
velocity.y = 0;
isKinematic = true;
}
else
{
//un freezes
isKinematic = false;
}
}
}
}
}