src/proto2/Physics.cc
author saiam
Thu, 20 Nov 2008 21:25:09 +0000
changeset 83 cbba9729e92b
parent 82 8f60abd6a083
child 84 3cb862028a24
permissions -rw-r--r--
Integrointia fyssaan, jotain pikkubugausta havaittavissa.
58
a53f5ad69500 "working" singleplayer
terom
parents: 54
diff changeset
     1
a53f5ad69500 "working" singleplayer
terom
parents: 54
diff changeset
     2
#include "Physics.hh"
a53f5ad69500 "working" singleplayer
terom
parents: 54
diff changeset
     3
#include "Engine.hh"
a53f5ad69500 "working" singleplayer
terom
parents: 54
diff changeset
     4
45
32c876923cac I added a couple of lines. This still clearly is not going to work in this state.
saiam
parents: 44
diff changeset
     5
#include <algorithm>
50
9e1a6506f5a1 some rough-handed code modifications towards a newer, better, working Physics
terom
parents: 49
diff changeset
     6
#include <functional>
77
98dc9008d15f changed collision detection, remove old if content with new
nireco
parents: 76
diff changeset
     7
#include <cmath>
45
32c876923cac I added a couple of lines. This still clearly is not going to work in this state.
saiam
parents: 44
diff changeset
     8
60
26571fd9a8d1 physics is starting to work
terom
parents: 58
diff changeset
     9
PhysicsWorld::PhysicsWorld (Vector gravity, Vector dimensions)
79
295ecb26d8ff jotain mik? hajottaa kaiken
ekku
parents: 78
diff changeset
    10
    : tick_timer(PHYSICS_TICK_MS), gravity(gravity), dimensions(dimensions), terrain(dimensions.x, std::vector<TerrainType>(dimensions.y, EMPTY)) {
50
9e1a6506f5a1 some rough-handed code modifications towards a newer, better, working Physics
terom
parents: 49
diff changeset
    11
54
b8b043ba0abd fix some more compiler errors...
terom
parents: 52
diff changeset
    12
    slots.connect(tick_timer.sig_timer(), this, &PhysicsWorld::tick);
50
9e1a6506f5a1 some rough-handed code modifications towards a newer, better, working Physics
terom
parents: 49
diff changeset
    13
    tick_timer.enable();
9e1a6506f5a1 some rough-handed code modifications towards a newer, better, working Physics
terom
parents: 49
diff changeset
    14
}
44
b165c9a26b2e Physics implementation.
ekku
parents:
diff changeset
    15
b165c9a26b2e Physics implementation.
ekku
parents:
diff changeset
    16
void PhysicsWorld::addObject (PhysicsObject *object) {
47
87883096a882 It's like so cool.
saiam
parents: 45
diff changeset
    17
    objects.push_back(object);
44
b165c9a26b2e Physics implementation.
ekku
parents:
diff changeset
    18
}
b165c9a26b2e Physics implementation.
ekku
parents:
diff changeset
    19
b165c9a26b2e Physics implementation.
ekku
parents:
diff changeset
    20
void PhysicsWorld::tick () {
60
26571fd9a8d1 physics is starting to work
terom
parents: 58
diff changeset
    21
//    Engine::log(DEBUG, "physics.apply_force") << "*tick*";
58
a53f5ad69500 "working" singleplayer
terom
parents: 54
diff changeset
    22
76
577f248b03b4 removed tabs from Physics.cc
nireco
parents: 75
diff changeset
    23
    for (std::vector<PhysicsObject*>::iterator i = objects.begin(); i != objects.end(); i++) {
577f248b03b4 removed tabs from Physics.cc
nireco
parents: 75
diff changeset
    24
        (*i)->tick(); 
577f248b03b4 removed tabs from Physics.cc
nireco
parents: 75
diff changeset
    25
    }
48
fa1da22db8a0 It looks like physics could now work, but I doubt it...
saiam
parents: 47
diff changeset
    26
}
fa1da22db8a0 It looks like physics could now work, but I doubt it...
saiam
parents: 47
diff changeset
    27
60
26571fd9a8d1 physics is starting to work
terom
parents: 58
diff changeset
    28
PhysicsObject::PhysicsObject (PhysicsWorld &world, float mass, Vector position, Vector velocity)
26571fd9a8d1 physics is starting to work
terom
parents: 58
diff changeset
    29
    : world(world), mass(mass), position(position), velocity(velocity) {
26571fd9a8d1 physics is starting to work
terom
parents: 58
diff changeset
    30
26571fd9a8d1 physics is starting to work
terom
parents: 58
diff changeset
    31
    world.addObject(this);
26571fd9a8d1 physics is starting to work
terom
parents: 58
diff changeset
    32
}
44
b165c9a26b2e Physics implementation.
ekku
parents:
diff changeset
    33
83
cbba9729e92b Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents: 82
diff changeset
    34
/**
cbba9729e92b Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents: 82
diff changeset
    35
 * Updates object speed and position. This function organises force
cbba9729e92b Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents: 82
diff changeset
    36
 * integration and collision detection.
cbba9729e92b Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents: 82
diff changeset
    37
 */   
cbba9729e92b Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents: 82
diff changeset
    38
void PhysicsObject::updatePosition () {
cbba9729e92b Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents: 82
diff changeset
    39
    // Add gravity to the force queue
cbba9729e92b Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents: 82
diff changeset
    40
    forceq.push(Force(world.gravity, PHYSICS_TICK_MS));
cbba9729e92b Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents: 82
diff changeset
    41
    
cbba9729e92b Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents: 82
diff changeset
    42
    // Go trough every force in the queue
cbba9729e92b Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents: 82
diff changeset
    43
    // TODO: It might be possible to optimize by adding forces together
cbba9729e92b Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents: 82
diff changeset
    44
    std::queue<Force> newfq;
cbba9729e92b Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents: 82
diff changeset
    45
    Force tmpf;
cbba9729e92b Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents: 82
diff changeset
    46
    posAfterTick = position;
cbba9729e92b Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents: 82
diff changeset
    47
    velAfterTick = velocity;
cbba9729e92b Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents: 82
diff changeset
    48
    while (!forceq.empty()) {
cbba9729e92b Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents: 82
diff changeset
    49
        tmpf = forceq.front();
cbba9729e92b Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents: 82
diff changeset
    50
        if (tmpf.dt <= PHYSICS_TICK_MS) { // Force affects only one tick
cbba9729e92b Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents: 82
diff changeset
    51
            integrate(tmpf.force, tmpf.dt);
cbba9729e92b Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents: 82
diff changeset
    52
        } else { // Add remaining time to next tick
cbba9729e92b Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents: 82
diff changeset
    53
            newfq.push(Force(tmpf.force, tmpf.dt - PHYSICS_TICK_MS));
cbba9729e92b Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents: 82
diff changeset
    54
            integrate(tmpf.force, PHYSICS_TICK_MS);
cbba9729e92b Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents: 82
diff changeset
    55
        }
cbba9729e92b Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents: 82
diff changeset
    56
        forceq.pop();
cbba9729e92b Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents: 82
diff changeset
    57
        //        Engine::log(DEBUG, "PhysicsObject.updatePosition") << "Current position: " << posAfterTick;
cbba9729e92b Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents: 82
diff changeset
    58
    }
cbba9729e92b Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents: 82
diff changeset
    59
    forceq = newfq;
cbba9729e92b Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents: 82
diff changeset
    60
cbba9729e92b Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents: 82
diff changeset
    61
    Vector newPosition = posAfterTick + (velAfterTick * PHYSICS_TICK_MS)/1000;
cbba9729e92b Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents: 82
diff changeset
    62
    this->velocity = velAfterTick;
cbba9729e92b Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents: 82
diff changeset
    63
    Engine::log(DEBUG, "PhysicsObject.updatePosition") << "Nopeus: "<<this->velocity;
cbba9729e92b Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents: 82
diff changeset
    64
    /*
60
26571fd9a8d1 physics is starting to work
terom
parents: 58
diff changeset
    65
    this->velocity += world.gravity * (PHYSICS_TICK_MS / 1000.0);
83
cbba9729e92b Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents: 82
diff changeset
    66
60
26571fd9a8d1 physics is starting to work
terom
parents: 58
diff changeset
    67
    Vector newPosition = position + velocity * (PHYSICS_TICK_MS / 1000.0);
83
cbba9729e92b Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents: 82
diff changeset
    68
    */
44
b165c9a26b2e Physics implementation.
ekku
parents:
diff changeset
    69
47
87883096a882 It's like so cool.
saiam
parents: 45
diff changeset
    70
    //TODO Handle the object as a square or a polygon
83
cbba9729e92b Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents: 82
diff changeset
    71
80
ekku
parents: 79
diff changeset
    72
    bool collided = false;
77
98dc9008d15f changed collision detection, remove old if content with new
nireco
parents: 76
diff changeset
    73
98dc9008d15f changed collision detection, remove old if content with new
nireco
parents: 76
diff changeset
    74
    //goes 1 unit forward every step and check if has hit anything
80
ekku
parents: 79
diff changeset
    75
    Vector unitVector = (newPosition-position) / (newPosition-position).length();
82
ekku
parents: 81
diff changeset
    76
    
ekku
parents: 81
diff changeset
    77
	Vector tmpVector = position;
77
98dc9008d15f changed collision detection, remove old if content with new
nireco
parents: 76
diff changeset
    78
    Vector reached = position;
82
ekku
parents: 81
diff changeset
    79
	int steps = (int) (newPosition-position).length();
ekku
parents: 81
diff changeset
    80
    for(int i = 0; i < steps; i++) {
77
98dc9008d15f changed collision detection, remove old if content with new
nireco
parents: 76
diff changeset
    81
        tmpVector += unitVector;
98dc9008d15f changed collision detection, remove old if content with new
nireco
parents: 76
diff changeset
    82
        if(world.getType(tmpVector) != EMPTY) {
80
ekku
parents: 79
diff changeset
    83
			//Engine::log(DEBUG, "physics.update_position") << "hit something";
77
98dc9008d15f changed collision detection, remove old if content with new
nireco
parents: 76
diff changeset
    84
            // Then we have hit something
98dc9008d15f changed collision detection, remove old if content with new
nireco
parents: 76
diff changeset
    85
            reached = position + unitVector*(i-1);
80
ekku
parents: 79
diff changeset
    86
            collided = true;
77
98dc9008d15f changed collision detection, remove old if content with new
nireco
parents: 76
diff changeset
    87
            break;
80
ekku
parents: 79
diff changeset
    88
        } else {
ekku
parents: 79
diff changeset
    89
			//Engine::log(DEBUG, "physics.update_position") << "didnt hit";
ekku
parents: 79
diff changeset
    90
		}
77
98dc9008d15f changed collision detection, remove old if content with new
nireco
parents: 76
diff changeset
    91
    }
98dc9008d15f changed collision detection, remove old if content with new
nireco
parents: 76
diff changeset
    92
98dc9008d15f changed collision detection, remove old if content with new
nireco
parents: 76
diff changeset
    93
    // In case of some float error
98dc9008d15f changed collision detection, remove old if content with new
nireco
parents: 76
diff changeset
    94
    if(!collided) {
98dc9008d15f changed collision detection, remove old if content with new
nireco
parents: 76
diff changeset
    95
        if(world.getType(newPosition)) {
98dc9008d15f changed collision detection, remove old if content with new
nireco
parents: 76
diff changeset
    96
            // There was error, and there is ground
98dc9008d15f changed collision detection, remove old if content with new
nireco
parents: 76
diff changeset
    97
            newPosition = tmpVector;
98dc9008d15f changed collision detection, remove old if content with new
nireco
parents: 76
diff changeset
    98
        } else {
98dc9008d15f changed collision detection, remove old if content with new
nireco
parents: 76
diff changeset
    99
            // This means everything was ok, so no need to do anything
98dc9008d15f changed collision detection, remove old if content with new
nireco
parents: 76
diff changeset
   100
        }
98dc9008d15f changed collision detection, remove old if content with new
nireco
parents: 76
diff changeset
   101
    } else {
98dc9008d15f changed collision detection, remove old if content with new
nireco
parents: 76
diff changeset
   102
        newPosition = reached;
98dc9008d15f changed collision detection, remove old if content with new
nireco
parents: 76
diff changeset
   103
        this->velocity = Vector(0, 0);
98dc9008d15f changed collision detection, remove old if content with new
nireco
parents: 76
diff changeset
   104
        //TODO: it shouldn't just stop on collision
98dc9008d15f changed collision detection, remove old if content with new
nireco
parents: 76
diff changeset
   105
    }
98dc9008d15f changed collision detection, remove old if content with new
nireco
parents: 76
diff changeset
   106
    this->position = newPosition;
83
cbba9729e92b Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents: 82
diff changeset
   107
44
b165c9a26b2e Physics implementation.
ekku
parents:
diff changeset
   108
}
b165c9a26b2e Physics implementation.
ekku
parents:
diff changeset
   109
73
3274c4804ea5 Collision and stuff
ekku
parents: 71
diff changeset
   110
bool PhysicsWorld::collided (Vector oldPos, Vector newPos) {
76
577f248b03b4 removed tabs from Physics.cc
nireco
parents: 75
diff changeset
   111
    int deltaX = oldPos.x - newPos.x; 
577f248b03b4 removed tabs from Physics.cc
nireco
parents: 75
diff changeset
   112
    int deltaY = oldPos.y - newPos.y; 
577f248b03b4 removed tabs from Physics.cc
nireco
parents: 75
diff changeset
   113
    double distance = sqrt(deltaX * deltaX + deltaY * deltaY);
79
295ecb26d8ff jotain mik? hajottaa kaiken
ekku
parents: 78
diff changeset
   114
    double xInc = deltaX / distance;
295ecb26d8ff jotain mik? hajottaa kaiken
ekku
parents: 78
diff changeset
   115
    double yInc = deltaY / distance;
73
3274c4804ea5 Collision and stuff
ekku
parents: 71
diff changeset
   116
    double currentX = oldPos.x;
3274c4804ea5 Collision and stuff
ekku
parents: 71
diff changeset
   117
    double currentY = oldPos.y;
3274c4804ea5 Collision and stuff
ekku
parents: 71
diff changeset
   118
76
577f248b03b4 removed tabs from Physics.cc
nireco
parents: 75
diff changeset
   119
    // This implementation is bit slow since it checks some squares twice.
73
3274c4804ea5 Collision and stuff
ekku
parents: 71
diff changeset
   120
    for(unsigned int i = 1; i < distance; i++) {
76
577f248b03b4 removed tabs from Physics.cc
nireco
parents: 75
diff changeset
   121
        currentX += xInc;
577f248b03b4 removed tabs from Physics.cc
nireco
parents: 75
diff changeset
   122
        currentY += yInc;
577f248b03b4 removed tabs from Physics.cc
nireco
parents: 75
diff changeset
   123
        if(terrain[(int)currentX][(int)currentY] != EMPTY)
79
295ecb26d8ff jotain mik? hajottaa kaiken
ekku
parents: 78
diff changeset
   124
            return true;
76
577f248b03b4 removed tabs from Physics.cc
nireco
parents: 75
diff changeset
   125
    }
79
295ecb26d8ff jotain mik? hajottaa kaiken
ekku
parents: 78
diff changeset
   126
    return false;
73
3274c4804ea5 Collision and stuff
ekku
parents: 71
diff changeset
   127
}
3274c4804ea5 Collision and stuff
ekku
parents: 71
diff changeset
   128
83
cbba9729e92b Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents: 82
diff changeset
   129
/**
cbba9729e92b Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents: 82
diff changeset
   130
 * Integrates given force over time and stores new position to
cbba9729e92b Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents: 82
diff changeset
   131
 * posAfterTick and new velocity to velAfterTick.
cbba9729e92b Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents: 82
diff changeset
   132
 * @param force Force vector.
cbba9729e92b Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents: 82
diff changeset
   133
 * @param dt The time the force is applied (<=PHYSICS_TICK_MS)
cbba9729e92b Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents: 82
diff changeset
   134
 */
79
295ecb26d8ff jotain mik? hajottaa kaiken
ekku
parents: 78
diff changeset
   135
void PhysicsObject::integrate(Vector force, TimeMS dt) {
83
cbba9729e92b Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents: 82
diff changeset
   136
    Derivative tmpd;
cbba9729e92b Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents: 82
diff changeset
   137
    Derivative k1 = evaluate(force, 0, tmpd);
cbba9729e92b Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents: 82
diff changeset
   138
    Derivative k2 = evaluate(force, 0.5f*dt, k1);
cbba9729e92b Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents: 82
diff changeset
   139
    Derivative k3 = evaluate(force, 0.5f*dt, k2);
cbba9729e92b Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents: 82
diff changeset
   140
    Derivative k4 = evaluate(force, dt, k3);
cbba9729e92b Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents: 82
diff changeset
   141
    
cbba9729e92b Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents: 82
diff changeset
   142
cbba9729e92b Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents: 82
diff changeset
   143
    const Vector dxdt = (k1.dx + (k2.dx + k3.dx) * 2.0f + k4.dx) * 1.0f/6.0f;
cbba9729e92b Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents: 82
diff changeset
   144
    const Vector dvdt = (k1.dv + (k2.dv + k3.dv) * 2.0f + k4.dv) * 1.0f/6.0f;
cbba9729e92b Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents: 82
diff changeset
   145
    
cbba9729e92b Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents: 82
diff changeset
   146
    //    Engine::log(DEBUG, "PhysicsObject.integrate") << "Changes: "<< dxdt << " " << dvdt << " Time: " <<dt;
cbba9729e92b Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents: 82
diff changeset
   147
    posAfterTick = posAfterTick + (dxdt * dt)/1000;
cbba9729e92b Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents: 82
diff changeset
   148
    velAfterTick = velAfterTick + (dvdt * dt)/1000;
cbba9729e92b Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents: 82
diff changeset
   149
    //Engine::log(DEBUG, "PhysicsObject.integrate") << "velAfterTick: " << velAfterTick;
70
a5b7499219a4 Some drafts
saiam
parents: 68
diff changeset
   150
}
a5b7499219a4 Some drafts
saiam
parents: 68
diff changeset
   151
83
cbba9729e92b Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents: 82
diff changeset
   152
Derivative PhysicsObject::evaluate(Vector force, TimeMS dt, Derivative &d) {
cbba9729e92b Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents: 82
diff changeset
   153
    Vector curPos = posAfterTick + (d.dx*dt)/1000;
cbba9729e92b Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents: 82
diff changeset
   154
    Vector curVel = velAfterTick + (d.dv*dt)/1000;
58
a53f5ad69500 "working" singleplayer
terom
parents: 54
diff changeset
   155
83
cbba9729e92b Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents: 82
diff changeset
   156
    Derivative out;
cbba9729e92b Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents: 82
diff changeset
   157
    out.dx = curVel;
cbba9729e92b Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents: 82
diff changeset
   158
    out.dv = acceleration(force);
cbba9729e92b Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents: 82
diff changeset
   159
    //Engine::log(DEBUG, "PhysicsObject.evaluate") << "Out.dx: " << out.dx;
cbba9729e92b Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents: 82
diff changeset
   160
    return out;
cbba9729e92b Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents: 82
diff changeset
   161
}
cbba9729e92b Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents: 82
diff changeset
   162
cbba9729e92b Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents: 82
diff changeset
   163
Vector PhysicsObject::acceleration(const Vector &force) {
cbba9729e92b Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents: 82
diff changeset
   164
    return (force/mass);
cbba9729e92b Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents: 82
diff changeset
   165
}
cbba9729e92b Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents: 82
diff changeset
   166
cbba9729e92b Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents: 82
diff changeset
   167
/**
cbba9729e92b Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents: 82
diff changeset
   168
 * Adds force to the force queue. Force queue is emptied on each
cbba9729e92b Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents: 82
diff changeset
   169
 * tick. Forces that last over one tick are also handled.
cbba9729e92b Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents: 82
diff changeset
   170
 * @param force Force vector.
cbba9729e92b Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents: 82
diff changeset
   171
 * @param dt The time the force is applied.
cbba9729e92b Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents: 82
diff changeset
   172
 */
cbba9729e92b Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents: 82
diff changeset
   173
void PhysicsObject::applyForce (Vector force, TimeMS dt) {
cbba9729e92b Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents: 82
diff changeset
   174
    // Add applied force to the queue
cbba9729e92b Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents: 82
diff changeset
   175
    forceq.push(Force(force, dt));
44
b165c9a26b2e Physics implementation.
ekku
parents:
diff changeset
   176
}
b165c9a26b2e Physics implementation.
ekku
parents:
diff changeset
   177
60
26571fd9a8d1 physics is starting to work
terom
parents: 58
diff changeset
   178
void PhysicsObject::updatePhysics (Vector position, Vector velocity) {
47
87883096a882 It's like so cool.
saiam
parents: 45
diff changeset
   179
    this->position = position;
87883096a882 It's like so cool.
saiam
parents: 45
diff changeset
   180
    this->velocity = velocity;
44
b165c9a26b2e Physics implementation.
ekku
parents:
diff changeset
   181
}
b165c9a26b2e Physics implementation.
ekku
parents:
diff changeset
   182
    
b165c9a26b2e Physics implementation.
ekku
parents:
diff changeset
   183
Vector PhysicsObject::getPosition () {
47
87883096a882 It's like so cool.
saiam
parents: 45
diff changeset
   184
    return this->position;
44
b165c9a26b2e Physics implementation.
ekku
parents:
diff changeset
   185
}
b165c9a26b2e Physics implementation.
ekku
parents:
diff changeset
   186
b165c9a26b2e Physics implementation.
ekku
parents:
diff changeset
   187
void PhysicsObject::tick () {
47
87883096a882 It's like so cool.
saiam
parents: 45
diff changeset
   188
    this->updatePosition();
44
b165c9a26b2e Physics implementation.
ekku
parents:
diff changeset
   189
}
b165c9a26b2e Physics implementation.
ekku
parents:
diff changeset
   190
75
f2c79f2d9384 added simple random map generation
nireco
parents: 73
diff changeset
   191
/**
f2c79f2d9384 added simple random map generation
nireco
parents: 73
diff changeset
   192
 * simple random map generation
f2c79f2d9384 added simple random map generation
nireco
parents: 73
diff changeset
   193
 * first fills whole level with dirt
f2c79f2d9384 added simple random map generation
nireco
parents: 73
diff changeset
   194
 * then randomizes circles of empty or rock
f2c79f2d9384 added simple random map generation
nireco
parents: 73
diff changeset
   195
 * @param seed - seed number for random number generator
f2c79f2d9384 added simple random map generation
nireco
parents: 73
diff changeset
   196
 */
79
295ecb26d8ff jotain mik? hajottaa kaiken
ekku
parents: 78
diff changeset
   197
void PhysicsWorld::generateTerrain(int seed) {
75
f2c79f2d9384 added simple random map generation
nireco
parents: 73
diff changeset
   198
    // generating should use own random number generator, but didn't find easily how that is done
f2c79f2d9384 added simple random map generation
nireco
parents: 73
diff changeset
   199
    srand(seed);
f2c79f2d9384 added simple random map generation
nireco
parents: 73
diff changeset
   200
    
f2c79f2d9384 added simple random map generation
nireco
parents: 73
diff changeset
   201
    // some constants to control random generation
f2c79f2d9384 added simple random map generation
nireco
parents: 73
diff changeset
   202
    const int min_range = 10;
f2c79f2d9384 added simple random map generation
nireco
parents: 73
diff changeset
   203
    const int max_range = 40;
79
295ecb26d8ff jotain mik? hajottaa kaiken
ekku
parents: 78
diff changeset
   204
    const int num = 0;
75
f2c79f2d9384 added simple random map generation
nireco
parents: 73
diff changeset
   205
    const int rock_rarity = 4; // 1 / rock_rarity will be rock circle
f2c79f2d9384 added simple random map generation
nireco
parents: 73
diff changeset
   206
f2c79f2d9384 added simple random map generation
nireco
parents: 73
diff changeset
   207
    // loops for amount of circles
f2c79f2d9384 added simple random map generation
nireco
parents: 73
diff changeset
   208
    for(int i = 0; i < num; i++) {
f2c79f2d9384 added simple random map generation
nireco
parents: 73
diff changeset
   209
        // information of new circle
79
295ecb26d8ff jotain mik? hajottaa kaiken
ekku
parents: 78
diff changeset
   210
        int midx = rand()%(int)dimensions.x;
295ecb26d8ff jotain mik? hajottaa kaiken
ekku
parents: 78
diff changeset
   211
        int midy = rand()%(int)dimensions.y;
295ecb26d8ff jotain mik? hajottaa kaiken
ekku
parents: 78
diff changeset
   212
295ecb26d8ff jotain mik? hajottaa kaiken
ekku
parents: 78
diff changeset
   213
        // put first circle in the middle of the cave
295ecb26d8ff jotain mik? hajottaa kaiken
ekku
parents: 78
diff changeset
   214
		// so that we have some area we can certainly spawn into 
295ecb26d8ff jotain mik? hajottaa kaiken
ekku
parents: 78
diff changeset
   215
		if(i == 0) {
295ecb26d8ff jotain mik? hajottaa kaiken
ekku
parents: 78
diff changeset
   216
			midx = dimensions.x / 2;
295ecb26d8ff jotain mik? hajottaa kaiken
ekku
parents: 78
diff changeset
   217
			midy = dimensions.y / 2;
295ecb26d8ff jotain mik? hajottaa kaiken
ekku
parents: 78
diff changeset
   218
		}
295ecb26d8ff jotain mik? hajottaa kaiken
ekku
parents: 78
diff changeset
   219
75
f2c79f2d9384 added simple random map generation
nireco
parents: 73
diff changeset
   220
        int range = rand()%(max_range-min_range)+min_range;
f2c79f2d9384 added simple random map generation
nireco
parents: 73
diff changeset
   221
        TerrainType type = EMPTY;
f2c79f2d9384 added simple random map generation
nireco
parents: 73
diff changeset
   222
        if(rand()%rock_rarity == 0) {
f2c79f2d9384 added simple random map generation
nireco
parents: 73
diff changeset
   223
            type = ROCK;
f2c79f2d9384 added simple random map generation
nireco
parents: 73
diff changeset
   224
        }
f2c79f2d9384 added simple random map generation
nireco
parents: 73
diff changeset
   225
        // loops for every pixel of circle
79
295ecb26d8ff jotain mik? hajottaa kaiken
ekku
parents: 78
diff changeset
   226
        for(int x = std::max(0, midx-range); x < std::min((int)dimensions.x, midx+range); x++) {
295ecb26d8ff jotain mik? hajottaa kaiken
ekku
parents: 78
diff changeset
   227
            for(int y = std::max(0, midy-range); y < std::min((int)dimensions.y, midy+range); y++) {
75
f2c79f2d9384 added simple random map generation
nireco
parents: 73
diff changeset
   228
                if(x*x+y*y < range*range) {
f2c79f2d9384 added simple random map generation
nireco
parents: 73
diff changeset
   229
                    // and sets it to type
f2c79f2d9384 added simple random map generation
nireco
parents: 73
diff changeset
   230
                    terrain[x][y] = type;
f2c79f2d9384 added simple random map generation
nireco
parents: 73
diff changeset
   231
                }
f2c79f2d9384 added simple random map generation
nireco
parents: 73
diff changeset
   232
            }
f2c79f2d9384 added simple random map generation
nireco
parents: 73
diff changeset
   233
        }
f2c79f2d9384 added simple random map generation
nireco
parents: 73
diff changeset
   234
    }
f2c79f2d9384 added simple random map generation
nireco
parents: 73
diff changeset
   235
}
77
98dc9008d15f changed collision detection, remove old if content with new
nireco
parents: 76
diff changeset
   236
98dc9008d15f changed collision detection, remove old if content with new
nireco
parents: 76
diff changeset
   237
/**
98dc9008d15f changed collision detection, remove old if content with new
nireco
parents: 76
diff changeset
   238
 * Returns terrainType in given tile. ROCK if tile is out of area
98dc9008d15f changed collision detection, remove old if content with new
nireco
parents: 76
diff changeset
   239
 * @param pos - coordinate of tile
98dc9008d15f changed collision detection, remove old if content with new
nireco
parents: 76
diff changeset
   240
 */
79
295ecb26d8ff jotain mik? hajottaa kaiken
ekku
parents: 78
diff changeset
   241
TerrainType PhysicsWorld::getType(Vector pos) const {
77
98dc9008d15f changed collision detection, remove old if content with new
nireco
parents: 76
diff changeset
   242
    int x = (int)(pos.x);
98dc9008d15f changed collision detection, remove old if content with new
nireco
parents: 76
diff changeset
   243
    int y = (int)(pos.y);
98dc9008d15f changed collision detection, remove old if content with new
nireco
parents: 76
diff changeset
   244
    if(x < 0 || y < 0 || x >= dimensions.x || y >= dimensions.y) {
98dc9008d15f changed collision detection, remove old if content with new
nireco
parents: 76
diff changeset
   245
        return ROCK;
98dc9008d15f changed collision detection, remove old if content with new
nireco
parents: 76
diff changeset
   246
    }
98dc9008d15f changed collision detection, remove old if content with new
nireco
parents: 76
diff changeset
   247
    return terrain[x][y];
98dc9008d15f changed collision detection, remove old if content with new
nireco
parents: 76
diff changeset
   248
}