src/proto2/Physics.cc
author nireco
Mon, 24 Nov 2008 21:36:00 +0000
changeset 100 29c3d91cad82
parent 99 c9b96dcfe4ee
child 101 1c52bd7fbc43
permissions -rw-r--r--
dot production
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)
91
0a6d675099dc Mato salmiakin muotoiseksi
ekku
parents: 90
diff changeset
    31
	: world(world), mass(mass), position(position), velocity(velocity) {
60
26571fd9a8d1 physics is starting to work
terom
parents: 58
diff changeset
    32
94
08bebac3d0c2 Maalla liikkumista
ekku
parents: 93
diff changeset
    33
	this->inAir = true;
60
26571fd9a8d1 physics is starting to work
terom
parents: 58
diff changeset
    34
    world.addObject(this);
26571fd9a8d1 physics is starting to work
terom
parents: 58
diff changeset
    35
}
44
b165c9a26b2e Physics implementation.
ekku
parents:
diff changeset
    36
83
cbba9729e92b Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents: 82
diff changeset
    37
/**
94
08bebac3d0c2 Maalla liikkumista
ekku
parents: 93
diff changeset
    38
 * TODO This method doesnt quite work as it should but 
08bebac3d0c2 Maalla liikkumista
ekku
parents: 93
diff changeset
    39
 * it makes the worm move pretty smoothly.
08bebac3d0c2 Maalla liikkumista
ekku
parents: 93
diff changeset
    40
 *
08bebac3d0c2 Maalla liikkumista
ekku
parents: 93
diff changeset
    41
 * Make the worm walk on the ground.
08bebac3d0c2 Maalla liikkumista
ekku
parents: 93
diff changeset
    42
 * @return Final position.
08bebac3d0c2 Maalla liikkumista
ekku
parents: 93
diff changeset
    43
 */
96
4a801210096c fix movement physics+network code to some degree, jumping is now buggy?
terom
parents: 95
diff changeset
    44
Vector PhysicsObject::walk (bool right) {
94
08bebac3d0c2 Maalla liikkumista
ekku
parents: 93
diff changeset
    45
	Vector cursor = right ? this->position + Vector(1,0) : this->position + Vector(-1,0);
08bebac3d0c2 Maalla liikkumista
ekku
parents: 93
diff changeset
    46
	Vector reached = this->position;
08bebac3d0c2 Maalla liikkumista
ekku
parents: 93
diff changeset
    47
08bebac3d0c2 Maalla liikkumista
ekku
parents: 93
diff changeset
    48
	//for(int steps = 0; steps < 3; steps++) {
08bebac3d0c2 Maalla liikkumista
ekku
parents: 93
diff changeset
    49
	
08bebac3d0c2 Maalla liikkumista
ekku
parents: 93
diff changeset
    50
		// Go up but not if the wall is over two pixels
08bebac3d0c2 Maalla liikkumista
ekku
parents: 93
diff changeset
    51
		if(world.getType(cursor) != EMPTY) {
08bebac3d0c2 Maalla liikkumista
ekku
parents: 93
diff changeset
    52
    		for(int height = 0, max = 3; height < max+42; height++) {
08bebac3d0c2 Maalla liikkumista
ekku
parents: 93
diff changeset
    53
				
08bebac3d0c2 Maalla liikkumista
ekku
parents: 93
diff changeset
    54
				if(height >= max)
08bebac3d0c2 Maalla liikkumista
ekku
parents: 93
diff changeset
    55
					return reached;
08bebac3d0c2 Maalla liikkumista
ekku
parents: 93
diff changeset
    56
08bebac3d0c2 Maalla liikkumista
ekku
parents: 93
diff changeset
    57
				cursor.y--;
08bebac3d0c2 Maalla liikkumista
ekku
parents: 93
diff changeset
    58
				if(world.getType(cursor) == EMPTY) {
08bebac3d0c2 Maalla liikkumista
ekku
parents: 93
diff changeset
    59
					// Check that the other parts of the worm don't collide with anything
08bebac3d0c2 Maalla liikkumista
ekku
parents: 93
diff changeset
    60
					if(possibleLocation(cursor)) {
08bebac3d0c2 Maalla liikkumista
ekku
parents: 93
diff changeset
    61
						reached = cursor;
08bebac3d0c2 Maalla liikkumista
ekku
parents: 93
diff changeset
    62
						continue;
08bebac3d0c2 Maalla liikkumista
ekku
parents: 93
diff changeset
    63
					} else {
08bebac3d0c2 Maalla liikkumista
ekku
parents: 93
diff changeset
    64
						// Can't get any further
08bebac3d0c2 Maalla liikkumista
ekku
parents: 93
diff changeset
    65
						return reached;
08bebac3d0c2 Maalla liikkumista
ekku
parents: 93
diff changeset
    66
					}
08bebac3d0c2 Maalla liikkumista
ekku
parents: 93
diff changeset
    67
				}
08bebac3d0c2 Maalla liikkumista
ekku
parents: 93
diff changeset
    68
			}
08bebac3d0c2 Maalla liikkumista
ekku
parents: 93
diff changeset
    69
		} else {
08bebac3d0c2 Maalla liikkumista
ekku
parents: 93
diff changeset
    70
			if(possibleLocation(cursor)) {
08bebac3d0c2 Maalla liikkumista
ekku
parents: 93
diff changeset
    71
				reached = cursor;
08bebac3d0c2 Maalla liikkumista
ekku
parents: 93
diff changeset
    72
			}
08bebac3d0c2 Maalla liikkumista
ekku
parents: 93
diff changeset
    73
			// Start checking if the lower squares are empty
08bebac3d0c2 Maalla liikkumista
ekku
parents: 93
diff changeset
    74
            for(int depth = 0, max = 3; depth < max+42; depth++) {
08bebac3d0c2 Maalla liikkumista
ekku
parents: 93
diff changeset
    75
				
08bebac3d0c2 Maalla liikkumista
ekku
parents: 93
diff changeset
    76
				if(depth >= max) {
08bebac3d0c2 Maalla liikkumista
ekku
parents: 93
diff changeset
    77
					// We can start a free fall now
08bebac3d0c2 Maalla liikkumista
ekku
parents: 93
diff changeset
    78
					this->inAir = true;
08bebac3d0c2 Maalla liikkumista
ekku
parents: 93
diff changeset
    79
                    // Put some speed there to make loke smoother
08bebac3d0c2 Maalla liikkumista
ekku
parents: 93
diff changeset
    80
					//this->velocity.y = -5;
08bebac3d0c2 Maalla liikkumista
ekku
parents: 93
diff changeset
    81
					return reached;
08bebac3d0c2 Maalla liikkumista
ekku
parents: 93
diff changeset
    82
				}
08bebac3d0c2 Maalla liikkumista
ekku
parents: 93
diff changeset
    83
08bebac3d0c2 Maalla liikkumista
ekku
parents: 93
diff changeset
    84
				cursor.y++;
08bebac3d0c2 Maalla liikkumista
ekku
parents: 93
diff changeset
    85
				if(world.getType(cursor) == EMPTY) {
08bebac3d0c2 Maalla liikkumista
ekku
parents: 93
diff changeset
    86
					// Check that the other parts of the worm don't collide with anything
08bebac3d0c2 Maalla liikkumista
ekku
parents: 93
diff changeset
    87
					if(possibleLocation(cursor)) {
08bebac3d0c2 Maalla liikkumista
ekku
parents: 93
diff changeset
    88
						reached = cursor;
08bebac3d0c2 Maalla liikkumista
ekku
parents: 93
diff changeset
    89
						continue;
08bebac3d0c2 Maalla liikkumista
ekku
parents: 93
diff changeset
    90
					} else {
08bebac3d0c2 Maalla liikkumista
ekku
parents: 93
diff changeset
    91
						// Can't get any further
08bebac3d0c2 Maalla liikkumista
ekku
parents: 93
diff changeset
    92
						return reached;
08bebac3d0c2 Maalla liikkumista
ekku
parents: 93
diff changeset
    93
					}
08bebac3d0c2 Maalla liikkumista
ekku
parents: 93
diff changeset
    94
				}
08bebac3d0c2 Maalla liikkumista
ekku
parents: 93
diff changeset
    95
			}
08bebac3d0c2 Maalla liikkumista
ekku
parents: 93
diff changeset
    96
		}      
08bebac3d0c2 Maalla liikkumista
ekku
parents: 93
diff changeset
    97
08bebac3d0c2 Maalla liikkumista
ekku
parents: 93
diff changeset
    98
	//	cursor.x += right ? 1 : -1;
08bebac3d0c2 Maalla liikkumista
ekku
parents: 93
diff changeset
    99
	//}
08bebac3d0c2 Maalla liikkumista
ekku
parents: 93
diff changeset
   100
	return reached;
08bebac3d0c2 Maalla liikkumista
ekku
parents: 93
diff changeset
   101
}
08bebac3d0c2 Maalla liikkumista
ekku
parents: 93
diff changeset
   102
08bebac3d0c2 Maalla liikkumista
ekku
parents: 93
diff changeset
   103
void PhysicsObject::jump () {
96
4a801210096c fix movement physics+network code to some degree, jumping is now buggy?
terom
parents: 95
diff changeset
   104
 	velocity.y = -100;
4a801210096c fix movement physics+network code to some degree, jumping is now buggy?
terom
parents: 95
diff changeset
   105
	inAir = true;
94
08bebac3d0c2 Maalla liikkumista
ekku
parents: 93
diff changeset
   106
}
08bebac3d0c2 Maalla liikkumista
ekku
parents: 93
diff changeset
   107
08bebac3d0c2 Maalla liikkumista
ekku
parents: 93
diff changeset
   108
bool PhysicsObject::possibleLocation (Vector loc) {
08bebac3d0c2 Maalla liikkumista
ekku
parents: 93
diff changeset
   109
	for(unsigned int i = 0; i < this->shape.size(); i++) {
08bebac3d0c2 Maalla liikkumista
ekku
parents: 93
diff changeset
   110
		if(world.getType(loc+shape[i]) != EMPTY)
08bebac3d0c2 Maalla liikkumista
ekku
parents: 93
diff changeset
   111
			return false;
08bebac3d0c2 Maalla liikkumista
ekku
parents: 93
diff changeset
   112
	}
08bebac3d0c2 Maalla liikkumista
ekku
parents: 93
diff changeset
   113
	return true;
08bebac3d0c2 Maalla liikkumista
ekku
parents: 93
diff changeset
   114
}
08bebac3d0c2 Maalla liikkumista
ekku
parents: 93
diff changeset
   115
08bebac3d0c2 Maalla liikkumista
ekku
parents: 93
diff changeset
   116
/**
83
cbba9729e92b Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents: 82
diff changeset
   117
 * Updates object speed and position. This function organises force
cbba9729e92b Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents: 82
diff changeset
   118
 * integration and collision detection.
cbba9729e92b Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents: 82
diff changeset
   119
 */   
cbba9729e92b Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents: 82
diff changeset
   120
void PhysicsObject::updatePosition () {
94
08bebac3d0c2 Maalla liikkumista
ekku
parents: 93
diff changeset
   121
08bebac3d0c2 Maalla liikkumista
ekku
parents: 93
diff changeset
   122
    if(!this->inAir) {
98
606c419e42a7 Ei se viel? k??nny kun on sienestetty.
saiam
parents: 97
diff changeset
   123
        /*if(!forceq.empty()) {
606c419e42a7 Ei se viel? k??nny kun on sienestetty.
saiam
parents: 97
diff changeset
   124
          if(forceq.front().y == 0) {
606c419e42a7 Ei se viel? k??nny kun on sienestetty.
saiam
parents: 97
diff changeset
   125
          this->position = moveVertically(forceq.front().x > 0);
606c419e42a7 Ei se viel? k??nny kun on sienestetty.
saiam
parents: 97
diff changeset
   126
          forceq.pop();
606c419e42a7 Ei se viel? k??nny kun on sienestetty.
saiam
parents: 97
diff changeset
   127
          }
606c419e42a7 Ei se viel? k??nny kun on sienestetty.
saiam
parents: 97
diff changeset
   128
          } */
606c419e42a7 Ei se viel? k??nny kun on sienestetty.
saiam
parents: 97
diff changeset
   129
        return;
94
08bebac3d0c2 Maalla liikkumista
ekku
parents: 93
diff changeset
   130
	}
08bebac3d0c2 Maalla liikkumista
ekku
parents: 93
diff changeset
   131
	// TODO HERE
08bebac3d0c2 Maalla liikkumista
ekku
parents: 93
diff changeset
   132
83
cbba9729e92b Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents: 82
diff changeset
   133
    // Add gravity to the force queue
85
351cb6b69c04 Making things simpler. More to come soon.
saiam
parents: 84
diff changeset
   134
    forceq.push(world.gravity);
83
cbba9729e92b Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents: 82
diff changeset
   135
    
cbba9729e92b Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents: 82
diff changeset
   136
    // Go trough every force in the queue
cbba9729e92b Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents: 82
diff changeset
   137
    // 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
   138
    Force total;
83
cbba9729e92b Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents: 82
diff changeset
   139
    posAfterTick = position;
cbba9729e92b Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents: 82
diff changeset
   140
    velAfterTick = velocity;
cbba9729e92b Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents: 82
diff changeset
   141
    while (!forceq.empty()) {
85
351cb6b69c04 Making things simpler. More to come soon.
saiam
parents: 84
diff changeset
   142
        total += forceq.front();
83
cbba9729e92b Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents: 82
diff changeset
   143
        forceq.pop();
cbba9729e92b Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents: 82
diff changeset
   144
        //        Engine::log(DEBUG, "PhysicsObject.updatePosition") << "Current position: " << posAfterTick;
cbba9729e92b Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents: 82
diff changeset
   145
    }
85
351cb6b69c04 Making things simpler. More to come soon.
saiam
parents: 84
diff changeset
   146
    integrate(total, PHYSICS_TICK_MS);
83
cbba9729e92b Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents: 82
diff changeset
   147
84
3cb862028a24 No nyt se toimii v?h?n paremmin. Koodia pit?? kyll? viel? siisti? aika huolella.
saiam
parents: 83
diff changeset
   148
    Vector newPosition = posAfterTick /*+ (velAfterTick * PHYSICS_TICK_MS)/1000*/;
83
cbba9729e92b Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents: 82
diff changeset
   149
    this->velocity = velAfterTick;
88
7431cd0cf900 Kuu n?kyy!
ekku
parents: 86
diff changeset
   150
    //Engine::log(DEBUG, "PhysicsObject.updatePosition") << "Nopeus: "<<this->velocity;
83
cbba9729e92b Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents: 82
diff changeset
   151
    /*
60
26571fd9a8d1 physics is starting to work
terom
parents: 58
diff changeset
   152
    this->velocity += world.gravity * (PHYSICS_TICK_MS / 1000.0);
83
cbba9729e92b Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents: 82
diff changeset
   153
60
26571fd9a8d1 physics is starting to work
terom
parents: 58
diff changeset
   154
    Vector newPosition = position + velocity * (PHYSICS_TICK_MS / 1000.0);
83
cbba9729e92b Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents: 82
diff changeset
   155
    */
44
b165c9a26b2e Physics implementation.
ekku
parents:
diff changeset
   156
47
87883096a882 It's like so cool.
saiam
parents: 45
diff changeset
   157
    //TODO Handle the object as a square or a polygon
83
cbba9729e92b Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents: 82
diff changeset
   158
80
ekku
parents: 79
diff changeset
   159
    bool collided = false;
77
98dc9008d15f changed collision detection, remove old if content with new
nireco
parents: 76
diff changeset
   160
98dc9008d15f changed collision detection, remove old if content with new
nireco
parents: 76
diff changeset
   161
    //goes 1 unit forward every step and check if has hit anything
80
ekku
parents: 79
diff changeset
   162
    Vector unitVector = (newPosition-position) / (newPosition-position).length();
82
ekku
parents: 81
diff changeset
   163
    
ekku
parents: 81
diff changeset
   164
	Vector tmpVector = position;
77
98dc9008d15f changed collision detection, remove old if content with new
nireco
parents: 76
diff changeset
   165
    Vector reached = position;
92
fe9da348afed Kimpoomisia
ekku
parents: 91
diff changeset
   166
	int steps = (int) (newPosition-position).length() + 2;
fe9da348afed Kimpoomisia
ekku
parents: 91
diff changeset
   167
		
fe9da348afed Kimpoomisia
ekku
parents: 91
diff changeset
   168
	//Engine::log(DEBUG, "physics.update_position") << unitVector-newPosition;
fe9da348afed Kimpoomisia
ekku
parents: 91
diff changeset
   169
	//Vector foo = position+unitVector*steps-newPosition;
fe9da348afed Kimpoomisia
ekku
parents: 91
diff changeset
   170
    //Engine::log(DEBUG, "PhysicsObject.updatePosition") << "Virhe: "<< foo;
95
10704e1df844 Normal stuff
ekku
parents: 94
diff changeset
   171
    
82
ekku
parents: 81
diff changeset
   172
    for(int i = 0; i < steps; i++) {
77
98dc9008d15f changed collision detection, remove old if content with new
nireco
parents: 76
diff changeset
   173
        tmpVector += unitVector;
94
08bebac3d0c2 Maalla liikkumista
ekku
parents: 93
diff changeset
   174
08bebac3d0c2 Maalla liikkumista
ekku
parents: 93
diff changeset
   175
		float minVelocity = 10;
08bebac3d0c2 Maalla liikkumista
ekku
parents: 93
diff changeset
   176
		// Check if any of the four corners of the worm collide
97
2e7c8ab485de Voi tuska mun kanssani
ekku
parents: 96
diff changeset
   177
		for(int sh = 0; sh < 4; sh++) {
2e7c8ab485de Voi tuska mun kanssani
ekku
parents: 96
diff changeset
   178
            if(world.getType(tmpVector+shape[2]) != EMPTY) {
2e7c8ab485de Voi tuska mun kanssani
ekku
parents: 96
diff changeset
   179
				reached = position + unitVector*(i-1);
2e7c8ab485de Voi tuska mun kanssani
ekku
parents: 96
diff changeset
   180
				collided = true;
2e7c8ab485de Voi tuska mun kanssani
ekku
parents: 96
diff changeset
   181
				this->bounce(world.getNormal(tmpVector+shape[2], tmpVector-unitVector+shape[2]));
2e7c8ab485de Voi tuska mun kanssani
ekku
parents: 96
diff changeset
   182
				this->velocity *= 0.6;
2e7c8ab485de Voi tuska mun kanssani
ekku
parents: 96
diff changeset
   183
				if(abs(this->velocity.x) < minVelocity && (abs(this->velocity.y) < minVelocity)) {
2e7c8ab485de Voi tuska mun kanssani
ekku
parents: 96
diff changeset
   184
					this->inAir = false;
2e7c8ab485de Voi tuska mun kanssani
ekku
parents: 96
diff changeset
   185
					this->velocity = Vector(0,0);
2e7c8ab485de Voi tuska mun kanssani
ekku
parents: 96
diff changeset
   186
				}
2e7c8ab485de Voi tuska mun kanssani
ekku
parents: 96
diff changeset
   187
				break;
2e7c8ab485de Voi tuska mun kanssani
ekku
parents: 96
diff changeset
   188
			}
2e7c8ab485de Voi tuska mun kanssani
ekku
parents: 96
diff changeset
   189
		}
2e7c8ab485de Voi tuska mun kanssani
ekku
parents: 96
diff changeset
   190
		if(collided)
2e7c8ab485de Voi tuska mun kanssani
ekku
parents: 96
diff changeset
   191
			break;
2e7c8ab485de Voi tuska mun kanssani
ekku
parents: 96
diff changeset
   192
		/*
2e7c8ab485de Voi tuska mun kanssani
ekku
parents: 96
diff changeset
   193
		if(velocity.y > 0) {
2e7c8ab485de Voi tuska mun kanssani
ekku
parents: 96
diff changeset
   194
        	if(world.getType(tmpVector+shape[2]) != EMPTY) {
92
fe9da348afed Kimpoomisia
ekku
parents: 91
diff changeset
   195
            	reached = position + unitVector*(i-1);
fe9da348afed Kimpoomisia
ekku
parents: 91
diff changeset
   196
            	collided = true;
fe9da348afed Kimpoomisia
ekku
parents: 91
diff changeset
   197
				this->velocity.y *= -0.3;
94
08bebac3d0c2 Maalla liikkumista
ekku
parents: 93
diff changeset
   198
				this->velocity.x *= 0.7;
08bebac3d0c2 Maalla liikkumista
ekku
parents: 93
diff changeset
   199
				if(abs(this->velocity.x) < minVelocity && (abs(this->velocity.y) < minVelocity)) {
08bebac3d0c2 Maalla liikkumista
ekku
parents: 93
diff changeset
   200
            		this->inAir = false;
08bebac3d0c2 Maalla liikkumista
ekku
parents: 93
diff changeset
   201
					this->velocity = Vector(0,0);
08bebac3d0c2 Maalla liikkumista
ekku
parents: 93
diff changeset
   202
				}
92
fe9da348afed Kimpoomisia
ekku
parents: 91
diff changeset
   203
            	break;
fe9da348afed Kimpoomisia
ekku
parents: 91
diff changeset
   204
			}
fe9da348afed Kimpoomisia
ekku
parents: 91
diff changeset
   205
		} else {
97
2e7c8ab485de Voi tuska mun kanssani
ekku
parents: 96
diff changeset
   206
        	if(world.getType(tmpVector+shape[0]) != EMPTY) {
92
fe9da348afed Kimpoomisia
ekku
parents: 91
diff changeset
   207
            	reached = position + unitVector*(i-1);
fe9da348afed Kimpoomisia
ekku
parents: 91
diff changeset
   208
            	collided = true;
fe9da348afed Kimpoomisia
ekku
parents: 91
diff changeset
   209
				this->velocity.y *= -0.3;
94
08bebac3d0c2 Maalla liikkumista
ekku
parents: 93
diff changeset
   210
				this->velocity.x *= 0.7;
08bebac3d0c2 Maalla liikkumista
ekku
parents: 93
diff changeset
   211
				if(abs(this->velocity.x) < minVelocity && (abs(this->velocity.y) < minVelocity)) {
08bebac3d0c2 Maalla liikkumista
ekku
parents: 93
diff changeset
   212
            		this->inAir = false;
08bebac3d0c2 Maalla liikkumista
ekku
parents: 93
diff changeset
   213
					this->velocity = Vector(0,0);
08bebac3d0c2 Maalla liikkumista
ekku
parents: 93
diff changeset
   214
				}
92
fe9da348afed Kimpoomisia
ekku
parents: 91
diff changeset
   215
            	break;
fe9da348afed Kimpoomisia
ekku
parents: 91
diff changeset
   216
			}
fe9da348afed Kimpoomisia
ekku
parents: 91
diff changeset
   217
        }
fe9da348afed Kimpoomisia
ekku
parents: 91
diff changeset
   218
fe9da348afed Kimpoomisia
ekku
parents: 91
diff changeset
   219
		if(velocity.x > 0) {
97
2e7c8ab485de Voi tuska mun kanssani
ekku
parents: 96
diff changeset
   220
        	if(world.getType(tmpVector+shape[1]) != EMPTY) {
92
fe9da348afed Kimpoomisia
ekku
parents: 91
diff changeset
   221
            	reached = position + unitVector*(i-1);
fe9da348afed Kimpoomisia
ekku
parents: 91
diff changeset
   222
            	collided = true;
94
08bebac3d0c2 Maalla liikkumista
ekku
parents: 93
diff changeset
   223
				this->velocity.x *= -0.6;
08bebac3d0c2 Maalla liikkumista
ekku
parents: 93
diff changeset
   224
				this->velocity.y *= 0.7;
08bebac3d0c2 Maalla liikkumista
ekku
parents: 93
diff changeset
   225
				if(abs(this->velocity.x) < minVelocity && (abs(this->velocity.y) < minVelocity)) {
08bebac3d0c2 Maalla liikkumista
ekku
parents: 93
diff changeset
   226
            		this->inAir = false;
08bebac3d0c2 Maalla liikkumista
ekku
parents: 93
diff changeset
   227
					this->velocity = Vector(0,0);
08bebac3d0c2 Maalla liikkumista
ekku
parents: 93
diff changeset
   228
				}
92
fe9da348afed Kimpoomisia
ekku
parents: 91
diff changeset
   229
            	break;
fe9da348afed Kimpoomisia
ekku
parents: 91
diff changeset
   230
			}
fe9da348afed Kimpoomisia
ekku
parents: 91
diff changeset
   231
		} else {
97
2e7c8ab485de Voi tuska mun kanssani
ekku
parents: 96
diff changeset
   232
        	if(world.getType(tmpVector+shape[3]) != EMPTY) {
92
fe9da348afed Kimpoomisia
ekku
parents: 91
diff changeset
   233
            	reached = position + unitVector*(i-1);
fe9da348afed Kimpoomisia
ekku
parents: 91
diff changeset
   234
            	collided = true;
94
08bebac3d0c2 Maalla liikkumista
ekku
parents: 93
diff changeset
   235
				this->velocity.x *= -0.6;
08bebac3d0c2 Maalla liikkumista
ekku
parents: 93
diff changeset
   236
				this->velocity.y *= 0.7;
08bebac3d0c2 Maalla liikkumista
ekku
parents: 93
diff changeset
   237
				if(abs(this->velocity.x) < minVelocity && (abs(this->velocity.y) < minVelocity)) {
08bebac3d0c2 Maalla liikkumista
ekku
parents: 93
diff changeset
   238
            		this->inAir = false;
08bebac3d0c2 Maalla liikkumista
ekku
parents: 93
diff changeset
   239
					this->velocity = Vector(0,0);
08bebac3d0c2 Maalla liikkumista
ekku
parents: 93
diff changeset
   240
				}
92
fe9da348afed Kimpoomisia
ekku
parents: 91
diff changeset
   241
            	break;
fe9da348afed Kimpoomisia
ekku
parents: 91
diff changeset
   242
			}
97
2e7c8ab485de Voi tuska mun kanssani
ekku
parents: 96
diff changeset
   243
        }*/
93
05a69f3f2ed4 Kimpoomisia lis??
ekku
parents: 92
diff changeset
   244
		
05a69f3f2ed4 Kimpoomisia lis??
ekku
parents: 92
diff changeset
   245
		// This col. det. doesn't let worms inside the ground, but on the other hand the worms get often stuck
97
2e7c8ab485de Voi tuska mun kanssani
ekku
parents: 96
diff changeset
   246
		/*if(world.getType(tmpVector+shape[0]) != EMPTY || (world.getType(tmpVector+shape[2]) != EMPTY)) {
93
05a69f3f2ed4 Kimpoomisia lis??
ekku
parents: 92
diff changeset
   247
            reached = position + unitVector*(i-1);
05a69f3f2ed4 Kimpoomisia lis??
ekku
parents: 92
diff changeset
   248
            collided = true;
05a69f3f2ed4 Kimpoomisia lis??
ekku
parents: 92
diff changeset
   249
			this->velocity.y *= -0.3;
95
10704e1df844 Normal stuff
ekku
parents: 94
diff changeset
   250
			if(abs(this->velocity.x) < minVelocity && (abs(this->velocity.y) < minVelocity)) {
94
08bebac3d0c2 Maalla liikkumista
ekku
parents: 93
diff changeset
   251
            	this->inAir = false;
08bebac3d0c2 Maalla liikkumista
ekku
parents: 93
diff changeset
   252
				this->velocity = Vector(0,0);
08bebac3d0c2 Maalla liikkumista
ekku
parents: 93
diff changeset
   253
			}
93
05a69f3f2ed4 Kimpoomisia lis??
ekku
parents: 92
diff changeset
   254
            break;
95
10704e1df844 Normal stuff
ekku
parents: 94
diff changeset
   255
        }
10704e1df844 Normal stuff
ekku
parents: 94
diff changeset
   256
		if(world.getType(tmpVector+shape[1]) != EMPTY || (world.getType(tmpVector+shape[3]) != EMPTY)) {
93
05a69f3f2ed4 Kimpoomisia lis??
ekku
parents: 92
diff changeset
   257
            reached = position + unitVector*(i-1);
05a69f3f2ed4 Kimpoomisia lis??
ekku
parents: 92
diff changeset
   258
            collided = true;
94
08bebac3d0c2 Maalla liikkumista
ekku
parents: 93
diff changeset
   259
			this->velocity.x *= -0.6;
95
10704e1df844 Normal stuff
ekku
parents: 94
diff changeset
   260
			if(abs(this->velocity.x) < minVelocity && (abs(this->velocity.y) < minVelocity)) {
94
08bebac3d0c2 Maalla liikkumista
ekku
parents: 93
diff changeset
   261
            	this->inAir = false;
08bebac3d0c2 Maalla liikkumista
ekku
parents: 93
diff changeset
   262
				this->velocity = Vector(0,0);
08bebac3d0c2 Maalla liikkumista
ekku
parents: 93
diff changeset
   263
			}
93
05a69f3f2ed4 Kimpoomisia lis??
ekku
parents: 92
diff changeset
   264
            break;
97
2e7c8ab485de Voi tuska mun kanssani
ekku
parents: 96
diff changeset
   265
		}*/
94
08bebac3d0c2 Maalla liikkumista
ekku
parents: 93
diff changeset
   266
		
80
ekku
parents: 79
diff changeset
   267
			//Engine::log(DEBUG, "physics.update_position") << "didnt hit";
77
98dc9008d15f changed collision detection, remove old if content with new
nireco
parents: 76
diff changeset
   268
    }
98dc9008d15f changed collision detection, remove old if content with new
nireco
parents: 76
diff changeset
   269
92
fe9da348afed Kimpoomisia
ekku
parents: 91
diff changeset
   270
    // In case of some float error check the final coordinate
77
98dc9008d15f changed collision detection, remove old if content with new
nireco
parents: 76
diff changeset
   271
    if(!collided) {
92
fe9da348afed Kimpoomisia
ekku
parents: 91
diff changeset
   272
        if(world.getType(newPosition+shape[0]) != EMPTY || (world.getType(newPosition+shape[1]) != EMPTY) 
fe9da348afed Kimpoomisia
ekku
parents: 91
diff changeset
   273
			|| (world.getType(newPosition+shape[2]) != EMPTY) || (world.getType(newPosition+shape[3]) != EMPTY)) {
fe9da348afed Kimpoomisia
ekku
parents: 91
diff changeset
   274
            
fe9da348afed Kimpoomisia
ekku
parents: 91
diff changeset
   275
			Engine::log(DEBUG, "physics.update_position") << "didnt hit";
fe9da348afed Kimpoomisia
ekku
parents: 91
diff changeset
   276
			// There was error, and there is ground
fe9da348afed Kimpoomisia
ekku
parents: 91
diff changeset
   277
            //newPosition = tmpVector;
77
98dc9008d15f changed collision detection, remove old if content with new
nireco
parents: 76
diff changeset
   278
        } else {
98dc9008d15f changed collision detection, remove old if content with new
nireco
parents: 76
diff changeset
   279
            // 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
   280
        }
98dc9008d15f changed collision detection, remove old if content with new
nireco
parents: 76
diff changeset
   281
    } else {
98dc9008d15f changed collision detection, remove old if content with new
nireco
parents: 76
diff changeset
   282
        newPosition = reached;
92
fe9da348afed Kimpoomisia
ekku
parents: 91
diff changeset
   283
        //this->velocity = Vector(0, 0);
77
98dc9008d15f changed collision detection, remove old if content with new
nireco
parents: 76
diff changeset
   284
        //TODO: it shouldn't just stop on collision
98dc9008d15f changed collision detection, remove old if content with new
nireco
parents: 76
diff changeset
   285
    }
98dc9008d15f changed collision detection, remove old if content with new
nireco
parents: 76
diff changeset
   286
    this->position = newPosition;
83
cbba9729e92b Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents: 82
diff changeset
   287
44
b165c9a26b2e Physics implementation.
ekku
parents:
diff changeset
   288
}
97
2e7c8ab485de Voi tuska mun kanssani
ekku
parents: 96
diff changeset
   289
2e7c8ab485de Voi tuska mun kanssani
ekku
parents: 96
diff changeset
   290
/**
2e7c8ab485de Voi tuska mun kanssani
ekku
parents: 96
diff changeset
   291
 * Gets the index of the given coordinate direction
2e7c8ab485de Voi tuska mun kanssani
ekku
parents: 96
diff changeset
   292
 * referring to the DIRECTIONS table in Physics.hh
2e7c8ab485de Voi tuska mun kanssani
ekku
parents: 96
diff changeset
   293
 */
95
10704e1df844 Normal stuff
ekku
parents: 94
diff changeset
   294
int getDirectionIndex (Vector dir) {
10704e1df844 Normal stuff
ekku
parents: 94
diff changeset
   295
	int index = 0;
98
606c419e42a7 Ei se viel? k??nny kun on sienestetty.
saiam
parents: 97
diff changeset
   296
	if(dir.x < -0.1 || (dir.y > 0.1 && dir.x < 0.1)) {
95
10704e1df844 Normal stuff
ekku
parents: 94
diff changeset
   297
		index += 4;
98
606c419e42a7 Ei se viel? k??nny kun on sienestetty.
saiam
parents: 97
diff changeset
   298
        }
97
2e7c8ab485de Voi tuska mun kanssani
ekku
parents: 96
diff changeset
   299
	if((dir.x > 0.1 && dir.y > 0.1) || (dir.y > 0.1 && dir.x < 0.1)) {
2e7c8ab485de Voi tuska mun kanssani
ekku
parents: 96
diff changeset
   300
		index += 2;
98
606c419e42a7 Ei se viel? k??nny kun on sienestetty.
saiam
parents: 97
diff changeset
   301
        }
606c419e42a7 Ei se viel? k??nny kun on sienestetty.
saiam
parents: 97
diff changeset
   302
	if(!(dir.x > -0.1 && dir.y < 0.1) && !(dir.y < 0.1 && dir.y > -0.1)) {
97
2e7c8ab485de Voi tuska mun kanssani
ekku
parents: 96
diff changeset
   303
		index += 1;
98
606c419e42a7 Ei se viel? k??nny kun on sienestetty.
saiam
parents: 97
diff changeset
   304
        }
97
2e7c8ab485de Voi tuska mun kanssani
ekku
parents: 96
diff changeset
   305
	return index;
95
10704e1df844 Normal stuff
ekku
parents: 94
diff changeset
   306
}
10704e1df844 Normal stuff
ekku
parents: 94
diff changeset
   307
97
2e7c8ab485de Voi tuska mun kanssani
ekku
parents: 96
diff changeset
   308
/**
2e7c8ab485de Voi tuska mun kanssani
ekku
parents: 96
diff changeset
   309
 * Computes hitten wall's normal. Calculated from 3*3 grid
2e7c8ab485de Voi tuska mun kanssani
ekku
parents: 96
diff changeset
   310
 */
95
10704e1df844 Normal stuff
ekku
parents: 94
diff changeset
   311
Vector PhysicsWorld::getNormal (Vector hitPoint, Vector prevPoint) {
10704e1df844 Normal stuff
ekku
parents: 94
diff changeset
   312
	// Search free points with bfs and put them to vector
10704e1df844 Normal stuff
ekku
parents: 94
diff changeset
   313
	std::vector<Vector> frees;
97
2e7c8ab485de Voi tuska mun kanssani
ekku
parents: 96
diff changeset
   314
	Vector hit = Vector((int)hitPoint.x, (int)hitPoint.y);
2e7c8ab485de Voi tuska mun kanssani
ekku
parents: 96
diff changeset
   315
	Vector prev = Vector((int)prevPoint.x, (int)prevPoint.y);
2e7c8ab485de Voi tuska mun kanssani
ekku
parents: 96
diff changeset
   316
	int dirIdx = getDirectionIndex(hit-prev);
2e7c8ab485de Voi tuska mun kanssani
ekku
parents: 96
diff changeset
   317
	for(int i = 1; i <= 2; i++) {
2e7c8ab485de Voi tuska mun kanssani
ekku
parents: 96
diff changeset
   318
		if(getType(hit+DIRECTIONS[dirIdx+i]) == EMPTY)
2e7c8ab485de Voi tuska mun kanssani
ekku
parents: 96
diff changeset
   319
			frees.push_back(DIRECTIONS[dirIdx+i]);
2e7c8ab485de Voi tuska mun kanssani
ekku
parents: 96
diff changeset
   320
		else
2e7c8ab485de Voi tuska mun kanssani
ekku
parents: 96
diff changeset
   321
			break;
2e7c8ab485de Voi tuska mun kanssani
ekku
parents: 96
diff changeset
   322
	}
2e7c8ab485de Voi tuska mun kanssani
ekku
parents: 96
diff changeset
   323
	for(int i = 1; i <= 2; i++) {
2e7c8ab485de Voi tuska mun kanssani
ekku
parents: 96
diff changeset
   324
		if(getType(hit+DIRECTIONS[dirIdx-i]) == EMPTY)
2e7c8ab485de Voi tuska mun kanssani
ekku
parents: 96
diff changeset
   325
			frees.push_back(DIRECTIONS[dirIdx-i]);
2e7c8ab485de Voi tuska mun kanssani
ekku
parents: 96
diff changeset
   326
		else
2e7c8ab485de Voi tuska mun kanssani
ekku
parents: 96
diff changeset
   327
			break;
2e7c8ab485de Voi tuska mun kanssani
ekku
parents: 96
diff changeset
   328
	}
98
606c419e42a7 Ei se viel? k??nny kun on sienestetty.
saiam
parents: 97
diff changeset
   329
    frees.push_back(DIRECTIONS[dirIdx]);
97
2e7c8ab485de Voi tuska mun kanssani
ekku
parents: 96
diff changeset
   330
2e7c8ab485de Voi tuska mun kanssani
ekku
parents: 96
diff changeset
   331
	Vector normal(0,0);
98
606c419e42a7 Ei se viel? k??nny kun on sienestetty.
saiam
parents: 97
diff changeset
   332
	for(int i = 0; i < frees.size(); i++) {
97
2e7c8ab485de Voi tuska mun kanssani
ekku
parents: 96
diff changeset
   333
		normal += frees[i];
2e7c8ab485de Voi tuska mun kanssani
ekku
parents: 96
diff changeset
   334
	}
2e7c8ab485de Voi tuska mun kanssani
ekku
parents: 96
diff changeset
   335
	return normal;
2e7c8ab485de Voi tuska mun kanssani
ekku
parents: 96
diff changeset
   336
}
2e7c8ab485de Voi tuska mun kanssani
ekku
parents: 96
diff changeset
   337
2e7c8ab485de Voi tuska mun kanssani
ekku
parents: 96
diff changeset
   338
/**
2e7c8ab485de Voi tuska mun kanssani
ekku
parents: 96
diff changeset
   339
 * Bounces from straight wall in any direction.
2e7c8ab485de Voi tuska mun kanssani
ekku
parents: 96
diff changeset
   340
 * Direction given as normal of that wall
2e7c8ab485de Voi tuska mun kanssani
ekku
parents: 96
diff changeset
   341
 */
2e7c8ab485de Voi tuska mun kanssani
ekku
parents: 96
diff changeset
   342
void PhysicsObject::bounce (Vector normal) {
2e7c8ab485de Voi tuska mun kanssani
ekku
parents: 96
diff changeset
   343
    Vector tangent(normal.y, -normal.x);
100
29c3d91cad82 dot production
nireco
parents: 99
diff changeset
   344
    Vector tprojection = (velocity * tangent)*tangent / (tangent.length()*tangent.length());
29c3d91cad82 dot production
nireco
parents: 99
diff changeset
   345
    Vector nprojection = (velocity * normal)*normal / (normal.length()*normal.length());
99
c9b96dcfe4ee Mui. Sienet.
saiam
parents: 98
diff changeset
   346
    velocity = tprojection - nprojection;
97
2e7c8ab485de Voi tuska mun kanssani
ekku
parents: 96
diff changeset
   347
}
44
b165c9a26b2e Physics implementation.
ekku
parents:
diff changeset
   348
94
08bebac3d0c2 Maalla liikkumista
ekku
parents: 93
diff changeset
   349
/*bool PhysicsWorld::collided (Vector oldPos, Vector newPos) {
08bebac3d0c2 Maalla liikkumista
ekku
parents: 93
diff changeset
   350
	return false;
08bebac3d0c2 Maalla liikkumista
ekku
parents: 93
diff changeset
   351
}*/
73
3274c4804ea5 Collision and stuff
ekku
parents: 71
diff changeset
   352
83
cbba9729e92b Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents: 82
diff changeset
   353
/**
cbba9729e92b Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents: 82
diff changeset
   354
 * Integrates given force over time and stores new position to
cbba9729e92b Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents: 82
diff changeset
   355
 * posAfterTick and new velocity to velAfterTick.
cbba9729e92b Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents: 82
diff changeset
   356
 * @param force Force vector.
cbba9729e92b Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents: 82
diff changeset
   357
 * @param dt The time the force is applied (<=PHYSICS_TICK_MS)
cbba9729e92b Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents: 82
diff changeset
   358
 */
85
351cb6b69c04 Making things simpler. More to come soon.
saiam
parents: 84
diff changeset
   359
void PhysicsObject::integrate(Force force, TimeMS dt) {
83
cbba9729e92b Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents: 82
diff changeset
   360
    Derivative tmpd;
cbba9729e92b Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents: 82
diff changeset
   361
    Derivative k1 = evaluate(force, 0, tmpd);
cbba9729e92b Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents: 82
diff changeset
   362
    Derivative k2 = evaluate(force, 0.5f*dt, k1);
cbba9729e92b Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents: 82
diff changeset
   363
    Derivative k3 = evaluate(force, 0.5f*dt, k2);
cbba9729e92b Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents: 82
diff changeset
   364
    Derivative k4 = evaluate(force, dt, k3);
cbba9729e92b Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents: 82
diff changeset
   365
    
cbba9729e92b Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents: 82
diff changeset
   366
cbba9729e92b Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents: 82
diff changeset
   367
    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
   368
    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
   369
    
cbba9729e92b Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents: 82
diff changeset
   370
    //    Engine::log(DEBUG, "PhysicsObject.integrate") << "Changes: "<< dxdt << " " << dvdt << " Time: " <<dt;
cbba9729e92b Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents: 82
diff changeset
   371
    posAfterTick = posAfterTick + (dxdt * dt)/1000;
cbba9729e92b Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents: 82
diff changeset
   372
    velAfterTick = velAfterTick + (dvdt * dt)/1000;
cbba9729e92b Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents: 82
diff changeset
   373
    //Engine::log(DEBUG, "PhysicsObject.integrate") << "velAfterTick: " << velAfterTick;
70
a5b7499219a4 Some drafts
saiam
parents: 68
diff changeset
   374
}
a5b7499219a4 Some drafts
saiam
parents: 68
diff changeset
   375
85
351cb6b69c04 Making things simpler. More to come soon.
saiam
parents: 84
diff changeset
   376
Derivative PhysicsObject::evaluate(Force force, TimeMS dt, Derivative &d) {
83
cbba9729e92b Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents: 82
diff changeset
   377
    Vector curPos = posAfterTick + (d.dx*dt)/1000;
cbba9729e92b Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents: 82
diff changeset
   378
    Vector curVel = velAfterTick + (d.dv*dt)/1000;
58
a53f5ad69500 "working" singleplayer
terom
parents: 54
diff changeset
   379
83
cbba9729e92b Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents: 82
diff changeset
   380
    Derivative out;
cbba9729e92b Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents: 82
diff changeset
   381
    out.dx = curVel;
cbba9729e92b Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents: 82
diff changeset
   382
    out.dv = acceleration(force);
cbba9729e92b Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents: 82
diff changeset
   383
    //Engine::log(DEBUG, "PhysicsObject.evaluate") << "Out.dx: " << out.dx;
cbba9729e92b Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents: 82
diff changeset
   384
    return out;
cbba9729e92b Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents: 82
diff changeset
   385
}
cbba9729e92b Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents: 82
diff changeset
   386
85
351cb6b69c04 Making things simpler. More to come soon.
saiam
parents: 84
diff changeset
   387
Vector PhysicsObject::acceleration(const Force &force) {
83
cbba9729e92b Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents: 82
diff changeset
   388
    return (force/mass);
cbba9729e92b Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents: 82
diff changeset
   389
}
cbba9729e92b Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents: 82
diff changeset
   390
cbba9729e92b Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents: 82
diff changeset
   391
/**
cbba9729e92b Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents: 82
diff changeset
   392
 * Adds force to the force queue. Force queue is emptied on each
cbba9729e92b Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents: 82
diff changeset
   393
 * tick. Forces that last over one tick are also handled.
cbba9729e92b Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents: 82
diff changeset
   394
 * @param force Force vector.
cbba9729e92b Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents: 82
diff changeset
   395
 * @param dt The time the force is applied.
cbba9729e92b Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents: 82
diff changeset
   396
 */
85
351cb6b69c04 Making things simpler. More to come soon.
saiam
parents: 84
diff changeset
   397
void PhysicsObject::applyForce (Force force, TimeMS dt) {
96
4a801210096c fix movement physics+network code to some degree, jumping is now buggy?
terom
parents: 95
diff changeset
   398
    // XXX: dt is not used? Is it assumed to be the same as the integrate() dt?
4a801210096c fix movement physics+network code to some degree, jumping is now buggy?
terom
parents: 95
diff changeset
   399
    
83
cbba9729e92b Integrointia fyssaan, jotain pikkubugausta havaittavissa.
saiam
parents: 82
diff changeset
   400
    // Add applied force to the queue
85
351cb6b69c04 Making things simpler. More to come soon.
saiam
parents: 84
diff changeset
   401
    forceq.push(force);
94
08bebac3d0c2 Maalla liikkumista
ekku
parents: 93
diff changeset
   402
	this->inAir = true;
44
b165c9a26b2e Physics implementation.
ekku
parents:
diff changeset
   403
}
b165c9a26b2e Physics implementation.
ekku
parents:
diff changeset
   404
60
26571fd9a8d1 physics is starting to work
terom
parents: 58
diff changeset
   405
void PhysicsObject::updatePhysics (Vector position, Vector velocity) {
47
87883096a882 It's like so cool.
saiam
parents: 45
diff changeset
   406
    this->position = position;
87883096a882 It's like so cool.
saiam
parents: 45
diff changeset
   407
    this->velocity = velocity;
44
b165c9a26b2e Physics implementation.
ekku
parents:
diff changeset
   408
}
b165c9a26b2e Physics implementation.
ekku
parents:
diff changeset
   409
    
b165c9a26b2e Physics implementation.
ekku
parents:
diff changeset
   410
Vector PhysicsObject::getPosition () {
47
87883096a882 It's like so cool.
saiam
parents: 45
diff changeset
   411
    return this->position;
44
b165c9a26b2e Physics implementation.
ekku
parents:
diff changeset
   412
}
b165c9a26b2e Physics implementation.
ekku
parents:
diff changeset
   413
91
0a6d675099dc Mato salmiakin muotoiseksi
ekku
parents: 90
diff changeset
   414
std::vector<Vector>& PhysicsObject::getShape () {
0a6d675099dc Mato salmiakin muotoiseksi
ekku
parents: 90
diff changeset
   415
    return this->shape;
0a6d675099dc Mato salmiakin muotoiseksi
ekku
parents: 90
diff changeset
   416
}
0a6d675099dc Mato salmiakin muotoiseksi
ekku
parents: 90
diff changeset
   417
0a6d675099dc Mato salmiakin muotoiseksi
ekku
parents: 90
diff changeset
   418
void PhysicsObject::setShape (std::vector<Vector> shape) {
0a6d675099dc Mato salmiakin muotoiseksi
ekku
parents: 90
diff changeset
   419
 	this->shape = shape;
0a6d675099dc Mato salmiakin muotoiseksi
ekku
parents: 90
diff changeset
   420
}
0a6d675099dc Mato salmiakin muotoiseksi
ekku
parents: 90
diff changeset
   421
44
b165c9a26b2e Physics implementation.
ekku
parents:
diff changeset
   422
void PhysicsObject::tick () {
47
87883096a882 It's like so cool.
saiam
parents: 45
diff changeset
   423
    this->updatePosition();
44
b165c9a26b2e Physics implementation.
ekku
parents:
diff changeset
   424
}
b165c9a26b2e Physics implementation.
ekku
parents:
diff changeset
   425
75
f2c79f2d9384 added simple random map generation
nireco
parents: 73
diff changeset
   426
/**
f2c79f2d9384 added simple random map generation
nireco
parents: 73
diff changeset
   427
 * simple random map generation
f2c79f2d9384 added simple random map generation
nireco
parents: 73
diff changeset
   428
 * first fills whole level with dirt
f2c79f2d9384 added simple random map generation
nireco
parents: 73
diff changeset
   429
 * then randomizes circles of empty or rock
f2c79f2d9384 added simple random map generation
nireco
parents: 73
diff changeset
   430
 * @param seed - seed number for random number generator
f2c79f2d9384 added simple random map generation
nireco
parents: 73
diff changeset
   431
 */
79
295ecb26d8ff jotain mik? hajottaa kaiken
ekku
parents: 78
diff changeset
   432
void PhysicsWorld::generateTerrain(int seed) {
75
f2c79f2d9384 added simple random map generation
nireco
parents: 73
diff changeset
   433
    // 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
   434
    srand(seed);
f2c79f2d9384 added simple random map generation
nireco
parents: 73
diff changeset
   435
    
f2c79f2d9384 added simple random map generation
nireco
parents: 73
diff changeset
   436
    // some constants to control random generation
f2c79f2d9384 added simple random map generation
nireco
parents: 73
diff changeset
   437
    const int min_range = 10;
f2c79f2d9384 added simple random map generation
nireco
parents: 73
diff changeset
   438
    const int max_range = 40;
90
ekku
parents: 88
diff changeset
   439
    const int num = 30;
75
f2c79f2d9384 added simple random map generation
nireco
parents: 73
diff changeset
   440
    const int rock_rarity = 4; // 1 / rock_rarity will be rock circle
f2c79f2d9384 added simple random map generation
nireco
parents: 73
diff changeset
   441
f2c79f2d9384 added simple random map generation
nireco
parents: 73
diff changeset
   442
    // loops for amount of circles
f2c79f2d9384 added simple random map generation
nireco
parents: 73
diff changeset
   443
    for(int i = 0; i < num; i++) {
f2c79f2d9384 added simple random map generation
nireco
parents: 73
diff changeset
   444
        // information of new circle
79
295ecb26d8ff jotain mik? hajottaa kaiken
ekku
parents: 78
diff changeset
   445
        int midx = rand()%(int)dimensions.x;
295ecb26d8ff jotain mik? hajottaa kaiken
ekku
parents: 78
diff changeset
   446
        int midy = rand()%(int)dimensions.y;
88
7431cd0cf900 Kuu n?kyy!
ekku
parents: 86
diff changeset
   447
        int range = rand()%(max_range-min_range)+min_range;
79
295ecb26d8ff jotain mik? hajottaa kaiken
ekku
parents: 78
diff changeset
   448
295ecb26d8ff jotain mik? hajottaa kaiken
ekku
parents: 78
diff changeset
   449
        // put first circle in the middle of the cave
295ecb26d8ff jotain mik? hajottaa kaiken
ekku
parents: 78
diff changeset
   450
		// so that we have some area we can certainly spawn into 
295ecb26d8ff jotain mik? hajottaa kaiken
ekku
parents: 78
diff changeset
   451
		if(i == 0) {
90
ekku
parents: 88
diff changeset
   452
			midx = dimensions.x/2;
ekku
parents: 88
diff changeset
   453
			midy = dimensions.y/2;
ekku
parents: 88
diff changeset
   454
			range = 150;
79
295ecb26d8ff jotain mik? hajottaa kaiken
ekku
parents: 78
diff changeset
   455
		}
295ecb26d8ff jotain mik? hajottaa kaiken
ekku
parents: 78
diff changeset
   456
90
ekku
parents: 88
diff changeset
   457
        TerrainType type = EMPTY;
75
f2c79f2d9384 added simple random map generation
nireco
parents: 73
diff changeset
   458
        if(rand()%rock_rarity == 0) {
f2c79f2d9384 added simple random map generation
nireco
parents: 73
diff changeset
   459
            type = ROCK;
f2c79f2d9384 added simple random map generation
nireco
parents: 73
diff changeset
   460
        }
90
ekku
parents: 88
diff changeset
   461
75
f2c79f2d9384 added simple random map generation
nireco
parents: 73
diff changeset
   462
        // loops for every pixel of circle
79
295ecb26d8ff jotain mik? hajottaa kaiken
ekku
parents: 78
diff changeset
   463
        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
   464
            for(int y = std::max(0, midy-range); y < std::min((int)dimensions.y, midy+range); y++) {
90
ekku
parents: 88
diff changeset
   465
                if((x-midx) * (x-midx) + (y-midy) * (y-midy) < range*range) {
75
f2c79f2d9384 added simple random map generation
nireco
parents: 73
diff changeset
   466
                    // and sets it to type
f2c79f2d9384 added simple random map generation
nireco
parents: 73
diff changeset
   467
                    terrain[x][y] = type;
f2c79f2d9384 added simple random map generation
nireco
parents: 73
diff changeset
   468
                }
f2c79f2d9384 added simple random map generation
nireco
parents: 73
diff changeset
   469
            }
f2c79f2d9384 added simple random map generation
nireco
parents: 73
diff changeset
   470
        }
f2c79f2d9384 added simple random map generation
nireco
parents: 73
diff changeset
   471
    }
f2c79f2d9384 added simple random map generation
nireco
parents: 73
diff changeset
   472
}
77
98dc9008d15f changed collision detection, remove old if content with new
nireco
parents: 76
diff changeset
   473
98dc9008d15f changed collision detection, remove old if content with new
nireco
parents: 76
diff changeset
   474
/**
98dc9008d15f changed collision detection, remove old if content with new
nireco
parents: 76
diff changeset
   475
 * 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
   476
 * @param pos - coordinate of tile
98dc9008d15f changed collision detection, remove old if content with new
nireco
parents: 76
diff changeset
   477
 */
79
295ecb26d8ff jotain mik? hajottaa kaiken
ekku
parents: 78
diff changeset
   478
TerrainType PhysicsWorld::getType(Vector pos) const {
77
98dc9008d15f changed collision detection, remove old if content with new
nireco
parents: 76
diff changeset
   479
    int x = (int)(pos.x);
98dc9008d15f changed collision detection, remove old if content with new
nireco
parents: 76
diff changeset
   480
    int y = (int)(pos.y);
98dc9008d15f changed collision detection, remove old if content with new
nireco
parents: 76
diff changeset
   481
    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
   482
        return ROCK;
98dc9008d15f changed collision detection, remove old if content with new
nireco
parents: 76
diff changeset
   483
    }
98dc9008d15f changed collision detection, remove old if content with new
nireco
parents: 76
diff changeset
   484
    return terrain[x][y];
98dc9008d15f changed collision detection, remove old if content with new
nireco
parents: 76
diff changeset
   485
}