src/proto2/Physics.cc
author ekku
Thu, 20 Nov 2008 21:22:03 +0000
changeset 82 8f60abd6a083
parent 81 4a91cf6f5cc7
child 83 cbba9729e92b
permissions -rw-r--r--
Foo
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
    
b165c9a26b2e Physics implementation.
ekku
parents:
diff changeset
    34
void PhysicsObject::updatePosition () {
b165c9a26b2e Physics implementation.
ekku
parents:
diff changeset
    35
47
87883096a882 It's like so cool.
saiam
parents: 45
diff changeset
    36
    // Calculate gravity's influence on the velocity vector
60
26571fd9a8d1 physics is starting to work
terom
parents: 58
diff changeset
    37
    this->velocity += world.gravity * (PHYSICS_TICK_MS / 1000.0);
47
87883096a882 It's like so cool.
saiam
parents: 45
diff changeset
    38
        
60
26571fd9a8d1 physics is starting to work
terom
parents: 58
diff changeset
    39
    Vector newPosition = position + velocity * (PHYSICS_TICK_MS / 1000.0);
44
b165c9a26b2e Physics implementation.
ekku
parents:
diff changeset
    40
47
87883096a882 It's like so cool.
saiam
parents: 45
diff changeset
    41
    //TODO Handle the object as a square or a polygon
58
a53f5ad69500 "working" singleplayer
terom
parents: 54
diff changeset
    42
    
80
ekku
parents: 79
diff changeset
    43
    bool collided = false;
77
98dc9008d15f changed collision detection, remove old if content with new
nireco
parents: 76
diff changeset
    44
98dc9008d15f changed collision detection, remove old if content with new
nireco
parents: 76
diff changeset
    45
    //goes 1 unit forward every step and check if has hit anything
80
ekku
parents: 79
diff changeset
    46
    Vector unitVector = (newPosition-position) / (newPosition-position).length();
82
ekku
parents: 81
diff changeset
    47
    
ekku
parents: 81
diff changeset
    48
	Vector tmpVector = position;
77
98dc9008d15f changed collision detection, remove old if content with new
nireco
parents: 76
diff changeset
    49
    Vector reached = position;
82
ekku
parents: 81
diff changeset
    50
	int steps = (int) (newPosition-position).length();
ekku
parents: 81
diff changeset
    51
    for(int i = 0; i < steps; i++) {
77
98dc9008d15f changed collision detection, remove old if content with new
nireco
parents: 76
diff changeset
    52
        tmpVector += unitVector;
98dc9008d15f changed collision detection, remove old if content with new
nireco
parents: 76
diff changeset
    53
        if(world.getType(tmpVector) != EMPTY) {
80
ekku
parents: 79
diff changeset
    54
			//Engine::log(DEBUG, "physics.update_position") << "hit something";
77
98dc9008d15f changed collision detection, remove old if content with new
nireco
parents: 76
diff changeset
    55
            // Then we have hit something
98dc9008d15f changed collision detection, remove old if content with new
nireco
parents: 76
diff changeset
    56
            reached = position + unitVector*(i-1);
80
ekku
parents: 79
diff changeset
    57
            collided = true;
77
98dc9008d15f changed collision detection, remove old if content with new
nireco
parents: 76
diff changeset
    58
            break;
80
ekku
parents: 79
diff changeset
    59
        } else {
ekku
parents: 79
diff changeset
    60
			//Engine::log(DEBUG, "physics.update_position") << "didnt hit";
ekku
parents: 79
diff changeset
    61
		}
77
98dc9008d15f changed collision detection, remove old if content with new
nireco
parents: 76
diff changeset
    62
    }
98dc9008d15f changed collision detection, remove old if content with new
nireco
parents: 76
diff changeset
    63
98dc9008d15f changed collision detection, remove old if content with new
nireco
parents: 76
diff changeset
    64
    // In case of some float error
98dc9008d15f changed collision detection, remove old if content with new
nireco
parents: 76
diff changeset
    65
    if(!collided) {
98dc9008d15f changed collision detection, remove old if content with new
nireco
parents: 76
diff changeset
    66
        if(world.getType(newPosition)) {
98dc9008d15f changed collision detection, remove old if content with new
nireco
parents: 76
diff changeset
    67
            // There was error, and there is ground
98dc9008d15f changed collision detection, remove old if content with new
nireco
parents: 76
diff changeset
    68
            newPosition = tmpVector;
98dc9008d15f changed collision detection, remove old if content with new
nireco
parents: 76
diff changeset
    69
        } else {
98dc9008d15f changed collision detection, remove old if content with new
nireco
parents: 76
diff changeset
    70
            // 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
    71
        }
98dc9008d15f changed collision detection, remove old if content with new
nireco
parents: 76
diff changeset
    72
    } else {
98dc9008d15f changed collision detection, remove old if content with new
nireco
parents: 76
diff changeset
    73
        newPosition = reached;
98dc9008d15f changed collision detection, remove old if content with new
nireco
parents: 76
diff changeset
    74
        this->velocity = Vector(0, 0);
98dc9008d15f changed collision detection, remove old if content with new
nireco
parents: 76
diff changeset
    75
        //TODO: it shouldn't just stop on collision
98dc9008d15f changed collision detection, remove old if content with new
nireco
parents: 76
diff changeset
    76
    }
98dc9008d15f changed collision detection, remove old if content with new
nireco
parents: 76
diff changeset
    77
    this->position = newPosition;
44
b165c9a26b2e Physics implementation.
ekku
parents:
diff changeset
    78
}
b165c9a26b2e Physics implementation.
ekku
parents:
diff changeset
    79
73
3274c4804ea5 Collision and stuff
ekku
parents: 71
diff changeset
    80
bool PhysicsWorld::collided (Vector oldPos, Vector newPos) {
76
577f248b03b4 removed tabs from Physics.cc
nireco
parents: 75
diff changeset
    81
    int deltaX = oldPos.x - newPos.x; 
577f248b03b4 removed tabs from Physics.cc
nireco
parents: 75
diff changeset
    82
    int deltaY = oldPos.y - newPos.y; 
577f248b03b4 removed tabs from Physics.cc
nireco
parents: 75
diff changeset
    83
    double distance = sqrt(deltaX * deltaX + deltaY * deltaY);
79
295ecb26d8ff jotain mik? hajottaa kaiken
ekku
parents: 78
diff changeset
    84
    double xInc = deltaX / distance;
295ecb26d8ff jotain mik? hajottaa kaiken
ekku
parents: 78
diff changeset
    85
    double yInc = deltaY / distance;
73
3274c4804ea5 Collision and stuff
ekku
parents: 71
diff changeset
    86
    double currentX = oldPos.x;
3274c4804ea5 Collision and stuff
ekku
parents: 71
diff changeset
    87
    double currentY = oldPos.y;
3274c4804ea5 Collision and stuff
ekku
parents: 71
diff changeset
    88
76
577f248b03b4 removed tabs from Physics.cc
nireco
parents: 75
diff changeset
    89
    // This implementation is bit slow since it checks some squares twice.
73
3274c4804ea5 Collision and stuff
ekku
parents: 71
diff changeset
    90
    for(unsigned int i = 1; i < distance; i++) {
76
577f248b03b4 removed tabs from Physics.cc
nireco
parents: 75
diff changeset
    91
        currentX += xInc;
577f248b03b4 removed tabs from Physics.cc
nireco
parents: 75
diff changeset
    92
        currentY += yInc;
577f248b03b4 removed tabs from Physics.cc
nireco
parents: 75
diff changeset
    93
        if(terrain[(int)currentX][(int)currentY] != EMPTY)
79
295ecb26d8ff jotain mik? hajottaa kaiken
ekku
parents: 78
diff changeset
    94
            return true;
76
577f248b03b4 removed tabs from Physics.cc
nireco
parents: 75
diff changeset
    95
    }
79
295ecb26d8ff jotain mik? hajottaa kaiken
ekku
parents: 78
diff changeset
    96
    return false;
73
3274c4804ea5 Collision and stuff
ekku
parents: 71
diff changeset
    97
}
3274c4804ea5 Collision and stuff
ekku
parents: 71
diff changeset
    98
79
295ecb26d8ff jotain mik? hajottaa kaiken
ekku
parents: 78
diff changeset
    99
void PhysicsObject::integrate(Vector force, TimeMS dt) {
70
a5b7499219a4 Some drafts
saiam
parents: 68
diff changeset
   100
    // TODO
a5b7499219a4 Some drafts
saiam
parents: 68
diff changeset
   101
}
a5b7499219a4 Some drafts
saiam
parents: 68
diff changeset
   102
79
295ecb26d8ff jotain mik? hajottaa kaiken
ekku
parents: 78
diff changeset
   103
void PhysicsObject::applyForce (Vector force, TimeMS dt) {
58
a53f5ad69500 "working" singleplayer
terom
parents: 54
diff changeset
   104
    Vector oldVelocity = velocity;
a53f5ad69500 "working" singleplayer
terom
parents: 54
diff changeset
   105
60
26571fd9a8d1 physics is starting to work
terom
parents: 58
diff changeset
   106
    this->velocity += force * dt / 1000 / mass;  // The last factor denotes the time.
47
87883096a882 It's like so cool.
saiam
parents: 45
diff changeset
   107
    // It should be scaled somehow.
58
a53f5ad69500 "working" singleplayer
terom
parents: 54
diff changeset
   108
    
60
26571fd9a8d1 physics is starting to work
terom
parents: 58
diff changeset
   109
//    Engine::log(DEBUG, "physics.apply_force") << "force=" << force << ", velocity " << oldVelocity << " -> " << velocity;
44
b165c9a26b2e Physics implementation.
ekku
parents:
diff changeset
   110
}
b165c9a26b2e Physics implementation.
ekku
parents:
diff changeset
   111
60
26571fd9a8d1 physics is starting to work
terom
parents: 58
diff changeset
   112
void PhysicsObject::updatePhysics (Vector position, Vector velocity) {
47
87883096a882 It's like so cool.
saiam
parents: 45
diff changeset
   113
    this->position = position;
87883096a882 It's like so cool.
saiam
parents: 45
diff changeset
   114
    this->velocity = velocity;
44
b165c9a26b2e Physics implementation.
ekku
parents:
diff changeset
   115
}
b165c9a26b2e Physics implementation.
ekku
parents:
diff changeset
   116
    
b165c9a26b2e Physics implementation.
ekku
parents:
diff changeset
   117
Vector PhysicsObject::getPosition () {
47
87883096a882 It's like so cool.
saiam
parents: 45
diff changeset
   118
    return this->position;
44
b165c9a26b2e Physics implementation.
ekku
parents:
diff changeset
   119
}
b165c9a26b2e Physics implementation.
ekku
parents:
diff changeset
   120
b165c9a26b2e Physics implementation.
ekku
parents:
diff changeset
   121
void PhysicsObject::tick () {
47
87883096a882 It's like so cool.
saiam
parents: 45
diff changeset
   122
    this->updatePosition();
44
b165c9a26b2e Physics implementation.
ekku
parents:
diff changeset
   123
}
b165c9a26b2e Physics implementation.
ekku
parents:
diff changeset
   124
75
f2c79f2d9384 added simple random map generation
nireco
parents: 73
diff changeset
   125
/**
f2c79f2d9384 added simple random map generation
nireco
parents: 73
diff changeset
   126
 * simple random map generation
f2c79f2d9384 added simple random map generation
nireco
parents: 73
diff changeset
   127
 * first fills whole level with dirt
f2c79f2d9384 added simple random map generation
nireco
parents: 73
diff changeset
   128
 * then randomizes circles of empty or rock
f2c79f2d9384 added simple random map generation
nireco
parents: 73
diff changeset
   129
 * @param seed - seed number for random number generator
f2c79f2d9384 added simple random map generation
nireco
parents: 73
diff changeset
   130
 */
79
295ecb26d8ff jotain mik? hajottaa kaiken
ekku
parents: 78
diff changeset
   131
void PhysicsWorld::generateTerrain(int seed) {
75
f2c79f2d9384 added simple random map generation
nireco
parents: 73
diff changeset
   132
    // 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
   133
    srand(seed);
f2c79f2d9384 added simple random map generation
nireco
parents: 73
diff changeset
   134
    
f2c79f2d9384 added simple random map generation
nireco
parents: 73
diff changeset
   135
    // some constants to control random generation
f2c79f2d9384 added simple random map generation
nireco
parents: 73
diff changeset
   136
    const int min_range = 10;
f2c79f2d9384 added simple random map generation
nireco
parents: 73
diff changeset
   137
    const int max_range = 40;
79
295ecb26d8ff jotain mik? hajottaa kaiken
ekku
parents: 78
diff changeset
   138
    const int num = 0;
75
f2c79f2d9384 added simple random map generation
nireco
parents: 73
diff changeset
   139
    const int rock_rarity = 4; // 1 / rock_rarity will be rock circle
f2c79f2d9384 added simple random map generation
nireco
parents: 73
diff changeset
   140
f2c79f2d9384 added simple random map generation
nireco
parents: 73
diff changeset
   141
    // loops for amount of circles
f2c79f2d9384 added simple random map generation
nireco
parents: 73
diff changeset
   142
    for(int i = 0; i < num; i++) {
f2c79f2d9384 added simple random map generation
nireco
parents: 73
diff changeset
   143
        // information of new circle
79
295ecb26d8ff jotain mik? hajottaa kaiken
ekku
parents: 78
diff changeset
   144
        int midx = rand()%(int)dimensions.x;
295ecb26d8ff jotain mik? hajottaa kaiken
ekku
parents: 78
diff changeset
   145
        int midy = rand()%(int)dimensions.y;
295ecb26d8ff jotain mik? hajottaa kaiken
ekku
parents: 78
diff changeset
   146
295ecb26d8ff jotain mik? hajottaa kaiken
ekku
parents: 78
diff changeset
   147
        // put first circle in the middle of the cave
295ecb26d8ff jotain mik? hajottaa kaiken
ekku
parents: 78
diff changeset
   148
		// so that we have some area we can certainly spawn into 
295ecb26d8ff jotain mik? hajottaa kaiken
ekku
parents: 78
diff changeset
   149
		if(i == 0) {
295ecb26d8ff jotain mik? hajottaa kaiken
ekku
parents: 78
diff changeset
   150
			midx = dimensions.x / 2;
295ecb26d8ff jotain mik? hajottaa kaiken
ekku
parents: 78
diff changeset
   151
			midy = dimensions.y / 2;
295ecb26d8ff jotain mik? hajottaa kaiken
ekku
parents: 78
diff changeset
   152
		}
295ecb26d8ff jotain mik? hajottaa kaiken
ekku
parents: 78
diff changeset
   153
75
f2c79f2d9384 added simple random map generation
nireco
parents: 73
diff changeset
   154
        int range = rand()%(max_range-min_range)+min_range;
f2c79f2d9384 added simple random map generation
nireco
parents: 73
diff changeset
   155
        TerrainType type = EMPTY;
f2c79f2d9384 added simple random map generation
nireco
parents: 73
diff changeset
   156
        if(rand()%rock_rarity == 0) {
f2c79f2d9384 added simple random map generation
nireco
parents: 73
diff changeset
   157
            type = ROCK;
f2c79f2d9384 added simple random map generation
nireco
parents: 73
diff changeset
   158
        }
f2c79f2d9384 added simple random map generation
nireco
parents: 73
diff changeset
   159
        // loops for every pixel of circle
79
295ecb26d8ff jotain mik? hajottaa kaiken
ekku
parents: 78
diff changeset
   160
        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
   161
            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
   162
                if(x*x+y*y < range*range) {
f2c79f2d9384 added simple random map generation
nireco
parents: 73
diff changeset
   163
                    // and sets it to type
f2c79f2d9384 added simple random map generation
nireco
parents: 73
diff changeset
   164
                    terrain[x][y] = type;
f2c79f2d9384 added simple random map generation
nireco
parents: 73
diff changeset
   165
                }
f2c79f2d9384 added simple random map generation
nireco
parents: 73
diff changeset
   166
            }
f2c79f2d9384 added simple random map generation
nireco
parents: 73
diff changeset
   167
        }
f2c79f2d9384 added simple random map generation
nireco
parents: 73
diff changeset
   168
    }
f2c79f2d9384 added simple random map generation
nireco
parents: 73
diff changeset
   169
}
77
98dc9008d15f changed collision detection, remove old if content with new
nireco
parents: 76
diff changeset
   170
98dc9008d15f changed collision detection, remove old if content with new
nireco
parents: 76
diff changeset
   171
/**
98dc9008d15f changed collision detection, remove old if content with new
nireco
parents: 76
diff changeset
   172
 * 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
   173
 * @param pos - coordinate of tile
98dc9008d15f changed collision detection, remove old if content with new
nireco
parents: 76
diff changeset
   174
 */
79
295ecb26d8ff jotain mik? hajottaa kaiken
ekku
parents: 78
diff changeset
   175
TerrainType PhysicsWorld::getType(Vector pos) const {
77
98dc9008d15f changed collision detection, remove old if content with new
nireco
parents: 76
diff changeset
   176
    int x = (int)(pos.x);
98dc9008d15f changed collision detection, remove old if content with new
nireco
parents: 76
diff changeset
   177
    int y = (int)(pos.y);
98dc9008d15f changed collision detection, remove old if content with new
nireco
parents: 76
diff changeset
   178
    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
   179
        return ROCK;
98dc9008d15f changed collision detection, remove old if content with new
nireco
parents: 76
diff changeset
   180
    }
98dc9008d15f changed collision detection, remove old if content with new
nireco
parents: 76
diff changeset
   181
    return terrain[x][y];
98dc9008d15f changed collision detection, remove old if content with new
nireco
parents: 76
diff changeset
   182
}