1 | /* |
---|
2 | orxonox - the future of 3D-vertical-scrollers |
---|
3 | |
---|
4 | Copyright (C) 2006 orx |
---|
5 | |
---|
6 | This program is free software; you can redistribute it and/or modify |
---|
7 | it under the terms of the GNU General Public License as published by |
---|
8 | the Free Software Foundation; either version 2, or (at your option) |
---|
9 | any later version. |
---|
10 | |
---|
11 | ### File Specific: |
---|
12 | |
---|
13 | the actual playing shoult take place within this box |
---|
14 | |
---|
15 | main-programmer: Christoph Renner |
---|
16 | */ |
---|
17 | |
---|
18 | #include "action_box.h" |
---|
19 | #include "track.h" |
---|
20 | #include "state.h" |
---|
21 | |
---|
22 | /** |
---|
23 | * this function is called each frame. update your state here |
---|
24 | * @param time time since last tick |
---|
25 | */ |
---|
26 | void ActionBox::tick( float time ) |
---|
27 | { |
---|
28 | updatePlanes(); |
---|
29 | } |
---|
30 | |
---|
31 | /** |
---|
32 | * draws the box for debugging |
---|
33 | */ |
---|
34 | void ActionBox::draw( ) const |
---|
35 | { |
---|
36 | glMatrixMode(GL_MODELVIEW); |
---|
37 | glPushMatrix(); |
---|
38 | |
---|
39 | glPushAttrib(GL_ENABLE_BIT); |
---|
40 | |
---|
41 | glDisable(GL_LIGHTING); |
---|
42 | glDisable(GL_TEXTURE_2D); |
---|
43 | glDisable(GL_BLEND); |
---|
44 | glLineWidth(2.0); |
---|
45 | glTranslatef ( track->getTrackNode()->getAbsCoor ().x, |
---|
46 | track->getTrackNode()->getAbsCoor ().y, |
---|
47 | track->getTrackNode()->getAbsCoor ().z); |
---|
48 | Vector tmpRot = track->getTrackNode()->getAbsDir().getSpacialAxis(); |
---|
49 | glRotatef (track->getTrackNode()->getAbsDir().getSpacialAxisAngle(), tmpRot.x, tmpRot.y, tmpRot.z ); |
---|
50 | |
---|
51 | |
---|
52 | glColor3f(1.0, 1.0, 0.0); |
---|
53 | glBegin(GL_LINE_STRIP); |
---|
54 | glVertex3f(0, height_2, width_2); |
---|
55 | glVertex3f(0, -height_2, width_2); |
---|
56 | glVertex3f(0, -height_2, -width_2); |
---|
57 | glVertex3f(0, height_2, -width_2); |
---|
58 | glVertex3f(0, height_2, width_2); |
---|
59 | glEnd(); |
---|
60 | |
---|
61 | glColor3f(1.0, 0.0, 0.0 ); |
---|
62 | glBegin(GL_LINE_STRIP); |
---|
63 | glVertex3f(depth, height_2 * stretch, width_2 * stretch); |
---|
64 | glVertex3f(depth, -height_2 * stretch, width_2 * stretch); |
---|
65 | glVertex3f(depth, -height_2 * stretch, -width_2 * stretch); |
---|
66 | glVertex3f(depth, height_2 * stretch, -width_2 * stretch); |
---|
67 | glVertex3f(depth, height_2 * stretch, width_2 * stretch); |
---|
68 | glEnd(); |
---|
69 | |
---|
70 | glBegin(GL_LINE_STRIP); |
---|
71 | glVertex3f(depth, height_2 * stretch, width_2 * stretch); |
---|
72 | glVertex3f(0, height_2, width_2); |
---|
73 | glVertex3f(0, -height_2, width_2); |
---|
74 | glVertex3f(depth, -height_2 * stretch, width_2 * stretch); |
---|
75 | glEnd(); |
---|
76 | |
---|
77 | glBegin(GL_LINE_STRIP); |
---|
78 | glVertex3f(depth, height_2 * stretch, -width_2 * stretch); |
---|
79 | glVertex3f(0, height_2, -width_2); |
---|
80 | glVertex3f(0, -height_2, -width_2); |
---|
81 | glVertex3f(depth, -height_2 * stretch, -width_2 * stretch); |
---|
82 | glEnd(); |
---|
83 | |
---|
84 | glPopMatrix(); |
---|
85 | } |
---|
86 | |
---|
87 | /** |
---|
88 | * create a new action box. you must not create more than one action box |
---|
89 | * @param _track track to assign actionbox to |
---|
90 | * @param width_2 width/2 of near plane |
---|
91 | * @param height_2 height/2 of near plane |
---|
92 | * @param depth distance between near and far plane |
---|
93 | * @param stretch far plane will be stretched by this factor |
---|
94 | */ |
---|
95 | ActionBox::ActionBox( Track* _track, float width_2, float height_2, float depth, float stretch ) |
---|
96 | { |
---|
97 | assert( _track ); |
---|
98 | assert( State::getActionBox() == NULL ); |
---|
99 | |
---|
100 | State::setActionBox( this ); |
---|
101 | |
---|
102 | this->width_2 = width_2; |
---|
103 | this->height_2 = height_2; |
---|
104 | this->depth = depth; |
---|
105 | this->stretch = stretch; |
---|
106 | this->track = _track; |
---|
107 | |
---|
108 | setParent( _track->getTrackNode() ); |
---|
109 | |
---|
110 | toList( OM_COMMON ); |
---|
111 | } |
---|
112 | |
---|
113 | /** |
---|
114 | * destructor |
---|
115 | */ |
---|
116 | ActionBox::~ActionBox( ) |
---|
117 | { |
---|
118 | assert( State::getActionBox() ); |
---|
119 | State::setActionBox( NULL ); |
---|
120 | } |
---|
121 | |
---|
122 | /** |
---|
123 | * checks wether a point is inside this box |
---|
124 | * @param pos point to check (absCoor) |
---|
125 | * @return true if inside |
---|
126 | */ |
---|
127 | bool ActionBox::isPointInBox( const Vector & pos ) |
---|
128 | { |
---|
129 | bool result = true; //TODO remove |
---|
130 | for ( int i = 0; i<6; i++ ) |
---|
131 | { |
---|
132 | if ( planes[i].distancePoint( pos ) < 0 ) |
---|
133 | { |
---|
134 | result = false; // TODO return false; |
---|
135 | printf("PLANE %i FAILED %f\n", i, planes[i].distancePoint( pos ) ); |
---|
136 | } |
---|
137 | } |
---|
138 | return result; //TODO return true; |
---|
139 | } |
---|
140 | |
---|
141 | /** |
---|
142 | * update planes with new coordiantes. nomals must point to the inside |
---|
143 | */ |
---|
144 | void ActionBox::updatePlanes( ) |
---|
145 | { |
---|
146 | Vector p[7]; |
---|
147 | |
---|
148 | p[0] = Vector( 0.0, height_2, -width_2 ); |
---|
149 | p[1] = Vector( 0.0, height_2, width_2 ); |
---|
150 | p[2] = Vector( 0.0, -height_2, -width_2 ); |
---|
151 | p[3] = Vector( depth, height_2, -width_2 ); |
---|
152 | p[4] = Vector( depth, height_2, width_2 ); |
---|
153 | p[5] = Vector( depth, -height_2, width_2 ); |
---|
154 | p[6] = Vector( depth, -height_2, -width_2 ); |
---|
155 | |
---|
156 | for ( int i = 0; i<7; i++ ) |
---|
157 | { |
---|
158 | p[i] = track->getTrackNode()->getAbsDir().apply( p[i] ); |
---|
159 | p[i] += track->getTrackNode()->getAbsCoor(); |
---|
160 | } |
---|
161 | |
---|
162 | planes[0] = Plane( p[1], p[0], p[2] ); |
---|
163 | planes[1] = Plane( p[3], p[4], p[5] ); |
---|
164 | planes[2] = Plane( p[5], p[4], p[1] ); |
---|
165 | planes[3] = Plane( p[2], p[0], p[3] ); |
---|
166 | planes[4] = Plane( p[3], p[0], p[1] ); |
---|
167 | planes[5] = Plane( p[2], p[6], p[5] ); |
---|
168 | } |
---|
169 | |
---|
170 | |
---|
171 | |
---|