-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTransformObject.cpp
More file actions
120 lines (100 loc) · 3.33 KB
/
TransformObject.cpp
File metadata and controls
120 lines (100 loc) · 3.33 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
//
// Created by Parth on 12/20/2019.
//
#include <cmath>
#include "TransformObject.h"
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
//TransformObject::TransformObject() {
//}
TransformObject::TransformObject(Shader objectShader) : shader(objectShader) {
hasPhysics = false;
elasticity = 1.0;
calculateCenterOfMass();
}
TransformObject::TransformObject(Polygon* shape, Shader objectShader) : shader(objectShader) {
polygon = shape;
elasticity = 1.0;
calculateCenterOfMass();
}
void TransformObject::setRotation(float angle) {
rotation = angle;
polygon->rotation = angle;
polygon->setVertices(angle);
}
void TransformObject::setPosition(float x, float y) {
position[0] = x;
position[1] = y;
polygon->setPosition(x, y);
shader.use();
glm::mat4 transform = glm::mat4(1.0f);
transform = glm::translate(transform, glm::vec3(position[0], position[1], 0.0f));
transform = glm::rotate(transform, glm::degrees(rotation), glm::vec3(0, 0, 1));
shader.setMat4("transform", transform);
glBindVertexArray(polygon->VAO);
if (polygon->getType() == "Circle") {
glDrawArrays(GL_TRIANGLE_FAN, 0, 150);
} else if (polygon->getType() == "Rectangle") {
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
}
}
float* TransformObject::getPosition() {
return position;
}
void TransformObject::setVelocity(float x, float y) {
velocity[0] = x;
velocity[1] = y;
}
float* TransformObject::getVelocity() {
return velocity;
}
void TransformObject::setAcceleration(float x, float y) {
acceleration[0] = x;
acceleration[1] = y;
}
float* TransformObject::getAcceleration() {
return acceleration;
}
Polygon* TransformObject::getPolygon() {
return polygon;
}
void TransformObject::addPhysics() {
hasPhysics = true;
mass = 1.0;
}
void TransformObject::addForce(const float force[2]) {
float xAcceleration = ((mass * acceleration[0]) + force[0]) / mass;
float yAcceleration = ((mass * acceleration[1]) + force[1]) / mass;
setAcceleration(xAcceleration, yAcceleration);
}
void TransformObject::addForceAtPosition(float forcePosition[2], float force[2]) {
float r[2] = {forcePosition[0] - centerOfMass[0], forcePosition[1] - centerOfMass[1]};
float torque[2] = {r[0] * force[1], -r[1] * force[0]};
addTorque(forcePosition, torque);
}
void TransformObject::addTorque(float torquePosition[2], float torque[2]) {
float T = sqrt(pow(torque[0], 2) + pow(torque[1], 2));
float r = sqrt(pow(torquePosition[0] - centerOfMass[0], 2) + pow(torquePosition[1] - centerOfMass[1], 2));
float I = polygon->getMomentOfInertia() + (mass * pow(r, 2));
angularAcceleration = T / I;
}
void TransformObject::calculateCenterOfMass() {
float* vertices = polygon->getVertices();
float xPositions = 0.0;
float yPositions = 0.0;
int vertexIndex = 0;
for (int i = 0; i < polygon->numberOfVertices; i++) {
xPositions += vertices[vertexIndex];
yPositions += vertices[vertexIndex + 1];
vertexIndex += 3;
}
centerOfMass[0] = xPositions / polygon->numberOfVertices;
centerOfMass[1] = yPositions / polygon->numberOfVertices;
}
void TransformObject::setCenterOfMass(float x, float y) {
centerOfMass[0] = x;
centerOfMass[1] = y;
}
float* TransformObject::getCenterOfMass() {
return centerOfMass;
}