-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathActor.h
89 lines (66 loc) · 2.07 KB
/
Actor.h
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
87
88
89
#include "Vector.h"
#include <algorithm>
#include <vector>
// include shared ptr.
#if defined( __GNUC__ )
// tr1/memory is required to include tr1/shared_ptr.h... Dunno why.
#include <tr1/memory>
#elif defined( __MSVC__ )
#error "Insert whatever you have to to use shared_ptr here!"
#endif
#pragma once
// Any object that can be drawn, moved, and graphed can use Actor as its base.
// It does not determine how its derivative is drawn, moved, or graphed.
class Actor
{
void init()
{
deleteMe = false;
std::fill( v.begin(), v.end(), 0.0f );
std::fill( a.begin(), a.end(), 0.0f );
}
public:
typedef float value_type;
typedef Vector<float,2> vector_type;
protected:
Actor()
{
}
template< typename T >
void simple_integration( T& s, T& v, T& a, float dt, value_type maxSpeed=0 )
{
v += a * dt;
if( maxSpeed && magnitude(v) > maxSpeed )
v = magnitude( v, maxSpeed );
s += v*dt + a*dt*dt*0.5;
}
public:
typedef std::tr1::shared_ptr< Actor > ActorPointer;
typedef std::vector< ActorPointer > ActorList;
static ActorList actors;
// Single-letter vars used in order to be close to the mathematical
// equations.
vector_type s; // State, or position.
vector_type v; // Velocity.
vector_type a; // Acceleration.
vector_type previousS;
value_type maxSpeed;
value_type scale; // At what magnification to draw the Actor.
value_type m; // Mass.
value_type im; // Inverse mass.
bool deleteMe;
Actor( const vector_type& pos );
// This class does not care whether its derivatives draw with vectors,
// bitmaps, or what-have-you.
virtual void draw() = 0;
// This class does not care about how the object is moved, but does set
// up a simple, generic state integration.
virtual void move( float dt )
{
previousS = s;
simple_integration( s, v, a, dt );
}
virtual ~Actor()
{
}
};