forked from PrototypeX29A/norbit
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcontrol.cpp
More file actions
86 lines (67 loc) · 1.96 KB
/
control.cpp
File metadata and controls
86 lines (67 loc) · 1.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/* spaceship & other controllers */
#include <assert.h>
#include "control.h"
#include "stdio.h"
SpaceShipController::SpaceShipController (game_object * go_): engine_L (false),
engine_R (false)
{
go = go_;
assert(go);
}
void
SpaceShipController::toggleEngineL ()
{
engine_L = !engine_L;
printf ("engine_L = %d\n", engine_L);
}
void
SpaceShipController::toggleEngineR ()
{
engine_R = !engine_R;
printf ("engine_R = %d\n", engine_R);
}
void SpaceShipController::setEngineL(bool b) {engine_L = b;}
void SpaceShipController::setEngineR (bool b) {engine_R = b; }
bool SpaceShipController::getEngineL ()const{return engine_L;};
bool SpaceShipController::getEngineR ()const {return engine_R;};
void SpaceShipController::apply(){
assert(go);
if (engine_R) {
go->apply_force( vector_2(-0.1f,-1.0f), vector_2(-100.0f,-0.10f)); // pushes upward
}
if(engine_L) {
go->apply_force( vector_2(-0.1f,-1.0f), vector_2(100.0f,-0.10f)); // pushes upward
}
}
GravityController::GravityController (game_object * go_, std::list < game_object * >*go_list_): go_list (go_list_)
{
go = go_;
assert (go);
assert (go_list);
}
void
GravityController::apply ()
{
std::list < game_object * >&ref_go_list = *go_list;
for (std::list < game_object * >::iterator it = ref_go_list.begin ();
it != ref_go_list.end (); it++)
{
//if ( (*it)->mass() < 10 * go->mass()) continue;
float DistSq =
pow (go->posx () - (*it)->posx (),
2) + pow (go->posy () - (*it)->posy (),
2) + pow (go->posz () - (*it)->posz (), 2);
if (DistSq < 2.0)
continue;
if (DistSq > 5000.0)
continue;
float G = (go->mass () * (*it)->mass ()) / DistSq;
if (G > 10.0f)
G = 10.0f;
vector_2 DirF =
-1.0f * vector_2 (go->posx () - (*it)->posx (),
go->posy () - (*it)->posy ()) / sqrt (DistSq);
go->apply_force_G (G * DirF, vector_2 (go->posx (), go->posy ())); // pushes upward
//printf("applying grav Dsq:%0.4f G: %0.4f\n", DistSq, G);
}
}