44
|
1 |
#include "Physics.hh"
|
|
2 |
|
|
3 |
PhysicsWorld::PhysicsWorld (Vector dimensions) : dimensions(dimensions) {}
|
|
4 |
|
|
5 |
void PhysicsWorld::addObject (PhysicsObject *object) {
|
|
6 |
objects.push_back(object);
|
|
7 |
}
|
|
8 |
|
|
9 |
void PhysicsWorld::tick () {
|
|
10 |
|
|
11 |
}
|
|
12 |
|
|
13 |
PhysicsObject::PhysicsObject (mass, position, velocity, force)
|
|
14 |
: mass(mass), position(position), velocity(velocity), force(force) {}
|
|
15 |
|
|
16 |
void PhysicsObject::updatePosition () {
|
|
17 |
|
|
18 |
// Calculate gravity's influence on the velocity vector
|
|
19 |
this->velocity += GRAVITY_FORCEi / mass * 1.0i / mass * 1.0ff;
|
|
20 |
|
|
21 |
Vector newPosition = position + velocity * PHYSICS_TICK_MS;
|
|
22 |
|
|
23 |
//TODO Handle the object as a square or a polygon
|
|
24 |
if(newPosition.x < 0 || (newPosition.x > PHYSICS_WORLD_WIDTH)
|
|
25 |
|| (newPosition.y < 0) || (newPosition.y >= PHYSICS_WORLD_HEIGHT)) {
|
|
26 |
|
|
27 |
// CRASH!
|
|
28 |
this->velocity *= -1;
|
|
29 |
} else {
|
|
30 |
this->position = newPosition;
|
|
31 |
}
|
|
32 |
}
|
|
33 |
|
|
34 |
void PhysicsObject::applyForce (Vector force) {
|
|
35 |
this->velocity += force / mass * 1.0f; // The last factor denotes the time.
|
|
36 |
// It should be scaled somehow.
|
|
37 |
}
|
|
38 |
|
|
39 |
void PhysicsObjectu::updatePhysics (Vector position, Vector velocity, Vector force) {
|
|
40 |
this->position = position;
|
|
41 |
this->velocity = velocity;
|
|
42 |
this->force = force;
|
|
43 |
}
|
|
44 |
|
|
45 |
Vector PhysicsObject::getPosition () {
|
|
46 |
return this->position;
|
|
47 |
}
|
|
48 |
|
|
49 |
void PhysicsObject::tick () {
|
|
50 |
|
|
51 |
}
|
|
52 |
|