--- a/src/PhysicsObject.cc Sat Jan 24 00:47:54 2009 +0200
+++ b/src/PhysicsObject.cc Sat Jan 24 01:19:38 2009 +0200
@@ -26,10 +26,10 @@
}
PhysicsObject::~PhysicsObject (void) {
-// Engine::log(DEBUG, "PhysicsObject.destructor") << this /* << ": objects.size=" << ((int) world.objects.size()) */;
+
}
-/**
+/*
* Player walks on floor.
*/
Vector PhysicsObject::walk_one_step (float partial, bool right) {
@@ -134,31 +134,37 @@
// a force towards that
if (pivot != NULL) {
applyForce(getPivotForce());
+
if (pivot->type == PLAYER) {
- pivot->applyForce(getPivotForce()*(-1));
+ pivot->applyForce(-getPivotForce());
}
}
std::pair<Force, TimeMS> force;
std::queue<std::pair<Force, TimeMS> > newfq;
+
Force total;
+
while (!forceq.empty()) {
force = forceq.front();
+
if (force.second > dt) {
force.second -= dt;
newfq.push(force);
}
+
total += force.first;
forceq.pop();
}
+
forceq = newfq;
// If the player has stopped and there's some ground under some of the 3 some of the 3t
// set inAir false
if (this->velocity == Vector(0,0)) {
- this->inAir = !world.terrain.collides(this->position+shape[1]+Vector(0, 1))
- && !world.terrain.collides(this->position+shape[2]+Vector(0, 1))
- && !world.terrain.collides(this->position+shape[3]+Vector(0, 1));
+ this->inAir = !world.terrain.collides(this->position + shape[1] + Vector(0, 1))
+ && !world.terrain.collides(this->position + shape[2] + Vector(0, 1))
+ && !world.terrain.collides(this->position + shape[3] + Vector(0, 1));
// If, however, there's a force caused by a bomb, e.g., set it in air.
// Still, we have to be able to separate forces caused by walking attempts
// and bombs etc (+0.1 because float comparison can be dangerous)
@@ -166,7 +172,7 @@
this->inAir = true;
}
- if(!possibleLocation(position)) {
+ if (!possibleLocation(position)) {
//if we are trapped in ground form dirtball or something
//we might want to just return and set velocity to some value
//return;
@@ -200,7 +206,7 @@
const Vector diffVec = newPosition - position;
const Vector unitVector = diffVec / diffVec.length();
- if(unitVector == Vector(0, 0)) {
+ if (unitVector == Vector(0, 0)) {
return;
}
Vector reached = position;
@@ -239,15 +245,16 @@
if (!collided) {
if (!possibleLocation(newPosition)) {
newPosition = reached;
+
} else {
// This means everything was ok, so no need to do anything
}
- setPosition (newPosition);
+ setPosition(newPosition);
} else {
newPosition = reached;
- setPosition (newPosition);
+ setPosition(newPosition);
// the following may delete this object, so it must be the last thing called
onCollision(collisionPoint);
@@ -264,9 +271,11 @@
// normal.sqrLength can't be 0 when got from getNormal()
if (normal.sqrLength() != 0) {
Vector nvel = velocity;
+
// We project the velocity on normal and remove twice that much from velocity
- nvel = nvel - ((2)*((nvel*normal)/(normal*normal))*normal);
+ nvel = nvel - (2 * ((nvel * normal) / (normal * normal)) * normal);
velocity = nvel;
+
// We lose some of our speed on collision
this->velocity *= this->collision_elasticity;
}
@@ -278,38 +287,32 @@
* @param force Force vector.
* @param dt The time the force is applied (<=PHYSICS_TICK_MS)
*/
-void PhysicsObject::integrate(Force force, TimeMS dt, Vector &posAfterTick, Vector &velAfterTick) {
+void PhysicsObject::integrate (Force force, TimeMS dt, Vector &posAfterTick, Vector &velAfterTick) {
posAfterTick = position;
velAfterTick = velocity;
+
Derivative tmpd;
Derivative k1 = evaluate(force, 0, tmpd, posAfterTick, velAfterTick);
Derivative k2 = evaluate(force, dt / 2, k1, posAfterTick, velAfterTick);
Derivative k3 = evaluate(force, dt / 2, k2, posAfterTick, velAfterTick);
Derivative k4 = evaluate(force, dt, k3, posAfterTick, velAfterTick);
-
- const Vector dxdt = (k1.dx + (k2.dx + k3.dx) * 2.0f + k4.dx) * 1.0f/6.0f;
- const Vector dvdt = (k1.dv + (k2.dv + k3.dv) * 2.0f + k4.dv) * 1.0f/6.0f;
+ const Vector dxdt = (k1.dx + (k2.dx + k3.dx) * 2.0f + k4.dx) * 1.0f / 6.0f;
+ const Vector dvdt = (k1.dv + (k2.dv + k3.dv) * 2.0f + k4.dv) * 1.0f / 6.0f;
- // Engine::log(DEBUG, "PhysicsObject.integrate") << "Changes: "<< dxdt << " " << dvdt << " Time: " <<dt;
- posAfterTick = posAfterTick + (dxdt * dt)/1000;
- velAfterTick = velAfterTick + (dvdt * dt)/1000;
- //Engine::log(DEBUG, "PhysicsObject.integrate") << "velAfterTick: " << velAfterTick;
+ posAfterTick = posAfterTick + (dxdt * dt) / 1000;
+ velAfterTick = velAfterTick + (dvdt * dt) / 1000;
}
-Derivative PhysicsObject::evaluate(Force force, TimeMS dt, Derivative &d, const Vector &posAfterTick, const Vector &velAfterTick) {
- Vector curPos = posAfterTick + (d.dx*dt)/1000;
- Vector curVel = velAfterTick + (d.dv*dt)/1000;
+Derivative PhysicsObject::evaluate (Force force, TimeMS dt, const Derivative &d, const Vector &posAfterTick, const Vector &velAfterTick) {
+ Vector curPos = posAfterTick + (d.dx * dt) / 1000;
+ Vector curVel = velAfterTick + (d.dv * dt) / 1000;
- Derivative out;
- out.dx = curVel;
- out.dv = acceleration(force);
- //Engine::log(DEBUG, "PhysicsObject.evaluate") << "Out.dx: " << out.dx;
- return out;
+ return Derivative(curVel, acceleration(force));
}
Vector PhysicsObject::acceleration(const Force &force) {
- return (force/mass);
+ return (force / mass);
}
void PhysicsObject::applyForce (Force force, TimeMS dt) {
@@ -325,14 +328,6 @@
//Engine::log(DEBUG, "PhysicsObject.changeAim") << "Player aim: " << this->aim;
}
-ObjectType PhysicsObject::getType (void) const {
- return this->type;
-}
-
-void PhysicsObject::setFacing (FacingDirection facing) {
- this->facing = facing;
-}
-
void PhysicsObject::updatePhysics (Vector position, Vector velocity, bool inAir, FacingDirection facing, float aim) {
setPosition (position);
this->velocity = velocity;
@@ -341,50 +336,14 @@
this->aim = aim;
}
-Vector PhysicsObject::getPosition (void) const {
- return position;
-}
-
-Vector PhysicsObject::getPreviousPosition (void) const {
- return previousPosition;
-}
-
PixelCoordinate PhysicsObject::getCoordinate (void) const {
return world.terrain.getPixelCoordinate(position);
}
-Vector PhysicsObject::getVelocity (void) const {
- return velocity;
-}
-
-FacingDirection PhysicsObject::getFacing (void) const {
- return facing;
-}
-
-float PhysicsObject::getAim (void) const {
- return aim;
-}
-
Vector PhysicsObject::getDirection (void) const {
return facing == FACING_RIGHT ? Vector(cos(aim), -sin(aim)) : Vector(-cos(aim), -sin(aim));
}
-const std::vector<Vector>& PhysicsObject::getShape () const {
- return shape;
-}
-
-void PhysicsObject::setShape (std::vector<Vector> shape) {
- this->shape = shape;
-}
-
-PhysicsObject *PhysicsObject::getPivot (void) {
- return this->pivot;
-}
-
-void PhysicsObject::setPivot (PhysicsObject *pivot) {
- this->pivot = pivot;
-}
-
void PhysicsObject::tick (TimeMS tick_length) {
this->updatePosition(tick_length);
}
@@ -412,10 +371,6 @@
shouldDelete = true;
}
-bool PhysicsObject::isAlive (void) {
- return alive;
-}
-
bool PhysicsObject::removeIfDestroyed (void) {
if (!alive) {
if (shouldDelete)
@@ -433,7 +388,7 @@
}
bool PhysicsObject::collides (const PhysicsObject &obj) {
- const std::vector<Vector> oShape = obj.getShape();
+ const std::vector<Vector> oShape = obj.shape;
Vector p1, p2, p3;
int8_t sign, nsign;
for (std::vector<Vector>::const_iterator i = oShape.begin(); i != oShape.end(); i++) { // For every point in other shape
@@ -470,6 +425,18 @@
this->position = pos;
}
-void PhysicsObject::setVelocity (Vector velocity) {
- this->velocity = velocity;
+void PhysicsObject::reset (void) {
+ // zero velocity
+ this->velocity = Vector(0, 0);
+
+ // disable
+ disable();
}
+
+void PhysicsObject::resume (Vector position) {
+ // update position
+ setPosition(position);
+
+ // enable again
+ enable();
+}