-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPhysicsObject.cpp
62 lines (54 loc) · 1.56 KB
/
PhysicsObject.cpp
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
#include "PhysicsObject.h"
#include "terrain.h"
PhysicsObject::PhysicsObject(PhysicsCollection* phys)
{
m_phys = phys;
m_velocity = {0, 0, 0};
m_angleVelocity = {0, 0, 0};
m_meshScale = 1.0;
m_physicsRadius = 5.0;
m_angle = {0.0, 0.0, 0.0};
}
PhysicsObject::~PhysicsObject()
{
}
void PhysicsObject::setPosition(vec3 pos)
{
transform().setToIdentity();
transform().translate(pos);
transform().scale(m_meshScale);
transform().rotate(m_angle.x(), {1.0, 0.0, 0.0});
transform().rotate(m_angle.y(), {0.0, 1.0, 0.0});
transform().rotate(m_angle.z(), {0.0, 0.0, 1.0});
}
vec3 PhysicsObject::getWorldPosition()
{
return transform().column(3).toVector3D();
}
bool PhysicsObject::collideGround(vec3 pos)
{
float yter = m_phys->terrain->height(pos.x(), pos.z());
return pos.y() <= yter + m_physicsRadius;
}
bool PhysicsObject::collideObject(PhysicsObject *obj)
{
vec3 objPos= obj->getWorldPosition();
float diff = (getWorldPosition() - objPos).length();
float bound = m_physicsRadius + obj->getPhysicsRadius();
return diff < bound;
}
void PhysicsObject::update(float dt)
{
vec3 next = getWorldPosition() + m_velocity * dt;
if(collideGround(next)){
// Teleport above the ground!
float yter = m_phys->terrain->height(next.x(), next.z());
next.setY(yter + m_physicsRadius);
m_velocity = {0.0, 0.0, 0.0};
m_angleVelocity = {0.0, 0.0, 0.0};
setGravity(false);
}
// Update m_angle based on angle velocity!
m_angle += (m_angleVelocity * dt);
setPosition(next);
}