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