31 world.addObject(this); |
31 world.addObject(this); |
32 } |
32 } |
33 |
33 |
34 void PhysicsObject::updatePosition () { |
34 void PhysicsObject::updatePosition () { |
35 |
35 |
36 // Check if the player is moving on the ground |
|
37 /*if (this->velocity.y == 0 && (position.y >= world.dimensions.y - 3)) { |
|
38 position.x += 50 * velocity.x * (PHYSICS_TICK_MS / 1000.0); |
|
39 velocity.x = 0; |
|
40 return; |
|
41 }*/ |
|
42 |
|
43 // If not moving on the ground, apply normal physics |
|
44 |
|
45 // Calculate gravity's influence on the velocity vector |
36 // Calculate gravity's influence on the velocity vector |
46 this->velocity += world.gravity * (PHYSICS_TICK_MS / 1000.0); |
37 this->velocity += world.gravity * (PHYSICS_TICK_MS / 1000.0); |
47 |
38 |
48 Vector newPosition = position + velocity * (PHYSICS_TICK_MS / 1000.0); |
39 Vector newPosition = position + velocity * (PHYSICS_TICK_MS / 1000.0); |
49 |
40 |
50 //TODO Handle the object as a square or a polygon |
41 //TODO Handle the object as a square or a polygon |
51 |
42 |
52 // Engine::log(DEBUG, "physics.update_position") << "position=" << newPosition << ", velocity=" << velocity; |
|
53 /* |
|
54 bool collided = false; |
43 bool collided = false; |
55 |
44 |
56 //goes 1 unit forward every step and check if has hit anything |
45 //goes 1 unit forward every step and check if has hit anything |
57 Vector unitVector = (newPosition-position) / (newPosition-position).length(); |
46 Vector unitVector = (newPosition-position) / (newPosition-position).length(); |
58 Vector tmpVector = position; |
47 |
|
48 Vector tmpVector = position; |
59 Vector reached = position; |
49 Vector reached = position; |
60 for(int i = 0; i < newPosition.length(); i++) { |
50 int steps = (int) (newPosition-position).length(); |
|
51 for(int i = 0; i < steps; i++) { |
61 tmpVector += unitVector; |
52 tmpVector += unitVector; |
62 if(world.getType(tmpVector) != EMPTY) { |
53 if(world.getType(tmpVector) != EMPTY) { |
63 //Engine::log(DEBUG, "physics.update_position") << "hit something"; |
54 //Engine::log(DEBUG, "physics.update_position") << "hit something"; |
64 // Then we have hit something |
55 // Then we have hit something |
65 reached = position + unitVector*(i-1); |
56 reached = position + unitVector*(i-1); |
82 newPosition = reached; |
73 newPosition = reached; |
83 this->velocity = Vector(0, 0); |
74 this->velocity = Vector(0, 0); |
84 //TODO: it shouldn't just stop on collision |
75 //TODO: it shouldn't just stop on collision |
85 } |
76 } |
86 this->position = newPosition; |
77 this->position = newPosition; |
87 |
|
88 */ |
|
89 bool collided = false; |
|
90 |
|
91 if (newPosition.x < 0 || (newPosition.x > world.dimensions.x)) { |
|
92 // CRASH! |
|
93 this->velocity.x *= -0.5; |
|
94 |
|
95 // If the velocity drops under some fixed constant we decide it is zero. |
|
96 // This is to prevent the object from bouncing eternally. |
|
97 if (abs(this->velocity.x) < 0.1) |
|
98 this->velocity.x = 0; |
|
99 |
|
100 collided = true; |
|
101 } else { |
|
102 this->position.x = newPosition.x; |
|
103 } |
|
104 |
|
105 if (newPosition.y <= 0 || (newPosition.y >= world.dimensions.y)) { |
|
106 this->velocity.y *= -0.3; |
|
107 |
|
108 |
|
109 |
|
110 if (abs(this->velocity.y) < 0.1) { |
|
111 this->velocity.y = 0; |
|
112 // Static friction |
|
113 this->velocity.x *= 0.95; |
|
114 } else { |
|
115 // Kinetic friction |
|
116 this->velocity.x *= 0.75; |
|
117 } |
|
118 |
|
119 collided = true; |
|
120 } else { |
|
121 this->position.y = newPosition.y; |
|
122 } |
|
123 |
|
124 if(!collided) { |
|
125 this->position = newPosition; |
|
126 } |
|
127 } |
78 } |
128 |
79 |
129 bool PhysicsWorld::collided (Vector oldPos, Vector newPos) { |
80 bool PhysicsWorld::collided (Vector oldPos, Vector newPos) { |
130 int deltaX = oldPos.x - newPos.x; |
81 int deltaX = oldPos.x - newPos.x; |
131 int deltaY = oldPos.y - newPos.y; |
82 int deltaY = oldPos.y - newPos.y; |