-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParticle3D.cs
More file actions
172 lines (150 loc) · 4.91 KB
/
Particle3D.cs
File metadata and controls
172 lines (150 loc) · 4.91 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
160
161
162
163
164
165
166
167
168
169
170
171
172
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Particle3D : MonoBehaviour
{
public float speed;
public Vector3 position;
public Vector3 velocity;
public Vector3 acceleration;
public Vector3 force;
public Quaternion Rotation;
//public Vector3 norm;
public float angle;
public Vector3 torque;
public Vector3 angularVelocity;
public Vector3 angularAcceleration;
//public float invInertia;
//public Vector2 applyForce;
//public Vector2 positionOfForce;
public enum shape
{
circle,
rect,
}
public shape Shape = shape.rect;
public bool circulation;
public bool verticalOscillate;
public bool horizontalOscillate;
public bool applyDrag;
public bool applyGravity;
public bool kineticFriction;
public bool staticFriction;
public bool isSliding;
public bool isSpring;
private const float GRAVITY = -10;
private Vector2 GRAVITY_VEC = new Vector2(0, GRAVITY);
[Range(0, Mathf.Infinity)]
public float mass;
public enum EUpdateMethod
{
Euler,
Kinematic
};
// MASS AND FORCE
private float invMass;
private float Mass
{
set
{
mass = mass > 0.0f ? mass : 0.0f;
invMass = mass > 0.0f ? 1.0f / mass : 0.0f;
}
get
{
return mass;
}
}
private void AddForce(Vector3 newForce)
{
force += newForce;
}
// POSITION AND ROTATION UPDATE FUNCTIONS
/*
public void UpdateAcceleration()
{
acceleration = force * invMass;
force.Set(0.0f, 0.0f, 0.0f);
}
*/
void updatePositionEulerExplicit(float deltaTime)
{
position += velocity * deltaTime;
velocity += acceleration * deltaTime;
}
void updatePositionKinematic(float deltaTime)
{
position += (velocity * deltaTime) + ((acceleration * deltaTime * deltaTime)/2);
velocity += acceleration * deltaTime;
}
void updateRotationEulerExplicit(float deltaTime)
{
//angle += deltaTime * angularVelocity;
Quaternion newVel = new Quaternion(angularVelocity.x * deltaTime / 2f, angularVelocity.y * deltaTime / 2f, angularVelocity.z * deltaTime / 2f, 0.0f);
Quaternion rotation = newVel * Rotation;
Rotation.x += rotation.x;
Rotation.y += rotation.y;
Rotation.z += rotation.z;
Rotation.w += rotation.w;
angularVelocity += angularAcceleration * deltaTime;
Rotation = Rotation.normalized;
//Quaternion rotation = new Quaternion(i,j,k,w);
}
/*
void updateRotationKinematic(float deltaTime)
{
angle += (angularVelocity * deltaTime) + ((angularAcceleration * deltaTime * deltaTime) / 2);
angularVelocity += angularAcceleration * deltaTime;
}
*/
/*
void applyForceAtLocation(Vector2 pointOfForce, Vector2 newForce)
{
if (Shape == shape.rect)
invInertia = (1 / 12) * mass * ((pointOfForce.x * pointOfForce.x) + (pointOfForce.y * pointOfForce.y));
else if (Shape == shape.circle)
invInertia = (1 / 2) * mass * GetComponent<SphereCollider>().radius* GetComponent<SphereCollider>().radius; // *radius squared
torque = Vector3.Cross(pointOfForce, newForce).z;
angularVelocity += torque;
applyForce = new Vector3(0.0f, 0.0f);
positionOfForce = new Vector3(0.0f, 0.0f);
}
*/
// TIME LOOPS
void Start()
{
position = transform.position;
Mass = mass;
//circulate = false;
}
// Update is called once per frame
void Update()
{
/*
if(applyGravity)
AddForce(ForceGenerator.GenerateForce_gravity(Vector2.up, GRAVITY, Mass));
if (applyDrag)
AddForce(ForceGenerator.GenerateForce_drag(velocity, new Vector2(0f, 5f), 1.0f, 1.0f, 0.05f));
norm = ForceGenerator.GenerateForce_normal(new Vector2(0, GRAVITY), new Vector2(-1f, 1f));
if (kineticFriction)
AddForce(ForceGenerator.GenerateForce_friction_kinetic(norm , velocity, 0.55f));
if (staticFriction)
AddForce(ForceGenerator.GenerateForce_friction_static(norm, velocity, 0.60f));
if (isSliding)
AddForce(ForceGenerator.GenerateForce_sliding(GRAVITY_VEC, -velocity));
if (isSpring)
AddForce(ForceGenerator.GenerateForce_spring(position, new Vector2(1.0f, 0.0f), 2.5f, 100f));
*/
}
private void FixedUpdate()
{
//UpdateAcceleration();
//applyForceAtLocation(positionOfForce, applyForce);
updatePositionKinematic(Time.fixedDeltaTime);
updateRotationEulerExplicit(Time.fixedDeltaTime);
//updateRotationKinematic(Time.fixedDeltaTime);
transform.position = position;
this.transform.rotation = Rotation;
// transform.eulerAngles = new Vector3(0.0f, 0.0f, angle);
}
}