Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/physics/src/orxonox/objects/worldentities/pawns/SpaceShip.cc @ 2328

Last change on this file since 2328 was 2314, checked in by rgrieder, 16 years ago

Temporary solution to steer our SpaceShip: Unlike before (exponential damping), but you can still fly around.

  • Property svn:eol-style set to native
File size: 8.5 KB
Line 
1/*
2 *   ORXONOX - the hottest 3D action shooter ever to exist
3 *                    > www.orxonox.net <
4 *
5 *
6 *   License notice:
7 *
8 *   This program is free software; you can redistribute it and/or
9 *   modify it under the terms of the GNU General Public License
10 *   as published by the Free Software Foundation; either version 2
11 *   of the License, or (at your option) any later version.
12 *
13 *   This program is distributed in the hope that it will be useful,
14 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
15 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 *   GNU General Public License for more details.
17 *
18 *   You should have received a copy of the GNU General Public License
19 *   along with this program; if not, write to the Free Software
20 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
21 *
22 *   Author:
23 *      Fabian 'x3n' Landau
24 *   Co-authors:
25 *      ...
26 *
27 */
28
29#include "OrxonoxStableHeaders.h"
30#include "SpaceShip.h"
31
32#include "BulletDynamics/Dynamics/btRigidBody.h"
33
34#include "util/Math.h"
35#include "util/Exception.h"
36#include "core/CoreIncludes.h"
37#include "core/ConfigValueIncludes.h"
38#include "core/XMLPort.h"
39
40namespace orxonox
41{
42    CreateFactory(SpaceShip);
43
44    SpaceShip::SpaceShip(BaseObject* creator) : Pawn(creator)
45    {
46        RegisterObject(SpaceShip);
47
48        this->zeroDegree_ = 0;
49
50        this->maxSpeed_ = 0;
51        this->maxSecondarySpeed_ = 0;
52        this->maxRotation_ = 0;
53        this->translationAcceleration_ = 0;
54        this->rotationAcceleration_ = 0;
55        this->translationDamping_ = 0;
56
57        this->yawRotation_ = 0;
58        this->pitchRotation_ = 0;
59        this->rollRotation_ = 0;
60
61        this->bInvertYAxis_ = false;
62
63        this->setDestroyWhenPlayerLeft(true);
64
65        this->setConfigValues();
66        this->registerVariables();
67    }
68
69    SpaceShip::~SpaceShip()
70    {
71    }
72
73    void SpaceShip::XMLPort(Element& xmlelement, XMLPort::Mode mode)
74    {
75        SUPER(SpaceShip, XMLPort, xmlelement, mode);
76
77        XMLPortParam(SpaceShip, "maxspeed",          setMaxSpeed,          getMaxSpeed,          xmlelement, mode);
78        XMLPortParam(SpaceShip, "maxsecondaryspeed", setMaxSecondarySpeed, getMaxSecondarySpeed, xmlelement, mode);
79        XMLPortParam(SpaceShip, "maxrotation",       setMaxRotation,       getMaxRotation,       xmlelement, mode);
80        XMLPortParam(SpaceShip, "transacc",          setTransAcc,          getTransAcc,          xmlelement, mode);
81        XMLPortParam(SpaceShip, "rotacc",            setRotAcc,            getRotAcc,            xmlelement, mode);
82        XMLPortParam(SpaceShip, "transdamp",         setTransDamp,         getTransDamp,         xmlelement, mode);
83
84        if (this->physicalBody_)
85        {
86            this->physicalBody_->setDamping(0.8, 0.9);
87            this->physicalBody_->setActivationState(DISABLE_DEACTIVATION);
88        }
89    }
90
91    void SpaceShip::registerVariables()
92    {
93        REGISTERDATA(this->maxSpeed_,                network::direction::toclient);
94        REGISTERDATA(this->maxSecondarySpeed_,       network::direction::toclient);
95        REGISTERDATA(this->maxRotation_,             network::direction::toclient);
96        REGISTERDATA(this->translationAcceleration_, network::direction::toclient);
97        REGISTERDATA(this->rotationAcceleration_,    network::direction::toclient);
98        REGISTERDATA(this->translationDamping_,      network::direction::toclient);
99    }
100
101    void SpaceShip::setConfigValues()
102    {
103        SetConfigValue(bInvertYAxis_, false).description("Set this to true for joystick-like mouse behaviour (mouse up = ship down).");
104    }
105
106    void SpaceShip::tick(float dt)
107    {
108        if (this->isLocallyControlled())
109        {
110            // #####################################
111            // ############# STEERING ##############
112            // #####################################
113
114            //Vector3 velocity = this->getVelocity();
115            //if (velocity.x > this->maxSecondarySpeed_)
116            //    velocity.x = this->maxSecondarySpeed_;
117            //if (velocity.x < -this->maxSecondarySpeed_)
118            //    velocity.x = -this->maxSecondarySpeed_;
119            //if (velocity.y > this->maxSecondarySpeed_)
120            //    velocity.y = this->maxSecondarySpeed_;
121            //if (velocity.y < -this->maxSecondarySpeed_)
122            //    velocity.y = -this->maxSecondarySpeed_;
123            //if (velocity.z > this->maxSecondarySpeed_)
124            //    velocity.z = this->maxSecondarySpeed_;
125            //if (velocity.z < -this->maxSpeed_)
126            //    velocity.z = -this->maxSpeed_;
127
128            // normalize velocity and acceleration
129            //for (size_t dimension = 0; dimension < 3; ++dimension)
130            //{
131            //    if (this->acceleration_[dimension] == 0)
132            //    {
133            //        if (velocity[dimension] > 0)
134            //        {
135            //            velocity[dimension] -= (this->translationDamping_ * dt);
136            //            if (velocity[dimension] < 0)
137            //                velocity[dimension] = 0;
138            //        }
139            //        else if (velocity[dimension] < 0)
140            //        {
141            //            velocity[dimension] += (this->translationDamping_ * dt);
142            //            if (velocity[dimension] > 0)
143            //                velocity[dimension] = 0;
144            //        }
145            //    }
146            //}
147
148            //this->setVelocity(velocity);
149        }
150
151
152        SUPER(SpaceShip, tick, dt);
153
154
155        //if (this->isLocallyControlled())
156        //{
157        //    this->yaw(this->yawRotation_ * dt);
158        //    if (this->bInvertYAxis_)
159        //        this->pitch(Degree(-this->pitchRotation_ * dt));
160        //    else
161        //        this->pitch(Degree( this->pitchRotation_ * dt));
162        //    this->roll(this->rollRotation_ * dt);
163
164        //    this->acceleration_.x = 0;
165        //    this->acceleration_.y = 0;
166        //    this->acceleration_.z = 0;
167
168        //    this->yawRotation_   = this->zeroDegree_;
169        //    this->pitchRotation_ = this->zeroDegree_;
170        //    this->rollRotation_  = this->zeroDegree_;
171        //}
172    }
173
174    void SpaceShip::moveFrontBack(const Vector2& value)
175    {
176        assert(this->physicalBody_);
177        this->physicalBody_->applyCentralForce(physicalBody_->getWorldTransform().getBasis() * btVector3(0.0f, 0.0f, -getMass() * value.x * 100));
178//        this->acceleration_.z = -this->translationAcceleration_ * value.x;
179    }
180
181    void SpaceShip::moveRightLeft(const Vector2& value)
182    {
183        this->physicalBody_->applyCentralForce(physicalBody_->getWorldTransform().getBasis() * btVector3(getMass() * value.x * 100, 0.0f, 0.0f));
184//        this->acceleration_.x = this->translationAcceleration_ * value.x;
185    }
186
187    void SpaceShip::moveUpDown(const Vector2& value)
188    {
189        this->physicalBody_->applyCentralForce(physicalBody_->getWorldTransform().getBasis() * btVector3(0.0f, getMass() * value.x * 100, 0.0f));
190//        this->acceleration_.y = this->translationAcceleration_ * value.x;
191    }
192
193    void SpaceShip::rotateYaw(const Vector2& value)
194    {
195        this->physicalBody_->applyTorque(physicalBody_->getWorldTransform().getBasis() * btVector3(0.0f, getMass() * value.x * 10000, 0.0f));
196        //Degree temp = value.x * value.x * sgn(value.x) * this->rotationAcceleration_;
197        //if (temp > this->maxRotation_)
198        //    temp = this->maxRotation_;
199        //if (temp < -this->maxRotation_)
200        //    temp = -this->maxRotation_;
201        //this->yawRotation_ = Degree(temp);
202    }
203
204    void SpaceShip::rotatePitch(const Vector2& value)
205    {
206        this->physicalBody_->applyTorque(physicalBody_->getWorldTransform().getBasis() * btVector3(getMass() * value.x * 10000, 0.0f, 0.0f));
207        //Degree temp = value.x * value.x * sgn(value.x) * this->rotationAcceleration_;
208        //if (temp > this->maxRotation_)
209        //    temp = this->maxRotation_;
210        //if (temp < -this->maxRotation_)
211        //    temp = -this->maxRotation_;
212        //this->pitchRotation_ = Degree(temp);
213    }
214
215    void SpaceShip::rotateRoll(const Vector2& value)
216    {
217        this->physicalBody_->applyTorque(physicalBody_->getWorldTransform().getBasis() * btVector3(0.0f, 0.0f, -getMass() * value.x * 10000));
218        //Degree temp = value.x * value.x * sgn(value.x) * this->rotationAcceleration_;
219        //if (temp > this->maxRotation_)
220        //    temp = this->maxRotation_;
221        //if (temp < -this->maxRotation_)
222        //    temp = -this->maxRotation_;
223        //this->rollRotation_ = Degree(temp);
224    }
225
226    void SpaceShip::fire()
227    {
228    }
229}
Note: See TracBrowser for help on using the repository browser.