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