Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/presentation/src/orxonox/objects/worldentities/pawns/SpaceShip.cc @ 2463

Last change on this file since 2463 was 2463, checked in by rgrieder, 15 years ago

Two bugfixes and an optimisation.

  • Property svn:eol-style set to native
File size: 5.4 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    const float orientationGain = 100;
43    CreateFactory(SpaceShip);
44
45    SpaceShip::SpaceShip(BaseObject* creator) : Pawn(creator)
46    {
47        RegisterObject(SpaceShip);
48
49        this->primaryThrust_  = 100;
50        this->auxilaryThrust_ =  30;
51        this->rotationThrust_ =  10;
52
53        this->localLinearAcceleration_.setValue(0, 0, 0);
54        this->localAngularAcceleration_.setValue(0, 0, 0);
55
56        this->bInvertYAxis_ = false;
57
58        this->setDestroyWhenPlayerLeft(true);
59
60        // SpaceShip is always a physical object per default
61        // Be aware of this call: The collision type legality check will not reach derived classes!
62        this->setCollisionType(WorldEntity::Dynamic);
63
64        this->setConfigValues();
65        this->registerVariables();
66    }
67
68    SpaceShip::~SpaceShip()
69    {
70    }
71
72    void SpaceShip::XMLPort(Element& xmlelement, XMLPort::Mode mode)
73    {
74        SUPER(SpaceShip, XMLPort, xmlelement, mode);
75
76        XMLPortParamVariable(SpaceShip, "primaryThrust",  primaryThrust_,  xmlelement, mode);
77        XMLPortParamVariable(SpaceShip, "auxilaryThrust", auxilaryThrust_, xmlelement, mode);
78        XMLPortParamVariable(SpaceShip, "rotationThrust", rotationThrust_, xmlelement, mode);
79    }
80
81    void SpaceShip::registerVariables()
82    {
83        registerVariable(this->primaryThrust_,  variableDirection::toclient);
84        registerVariable(this->auxilaryThrust_, variableDirection::toclient);
85        registerVariable(this->rotationThrust_, variableDirection::toclient);
86    }
87
88    void SpaceShip::setConfigValues()
89    {
90        SetConfigValue(bInvertYAxis_, false).description("Set this to true for joystick-like mouse behaviour (mouse up = ship down).");
91    }
92
93    bool SpaceShip::isCollisionTypeLegal(WorldEntity::CollisionType type) const
94    {
95        if (type != WorldEntity::Dynamic)
96        {
97            CCOUT(1) << "Error: Cannot tell a SpaceShip not to be dynamic! Ignoring." << std::endl;
98            assert(false); // Only in debug mode
99            return false;
100        }
101        else
102            return true;
103    }
104
105    void SpaceShip::tick(float dt)
106    {
107        SUPER(SpaceShip, tick, dt);
108
109        if (this->isLocallyControlled())
110        {
111            this->localLinearAcceleration_.setX(this->localLinearAcceleration_.x() * getMass() * this->auxilaryThrust_);
112            this->localLinearAcceleration_.setY(this->localLinearAcceleration_.y() * getMass() * this->auxilaryThrust_);
113            if (this->localLinearAcceleration_.z() > 0)
114                this->localLinearAcceleration_.setZ(this->localLinearAcceleration_.z() * getMass() * this->auxilaryThrust_);
115            else
116                this->localLinearAcceleration_.setZ(this->localLinearAcceleration_.z() * getMass() * this->primaryThrust_);
117            this->physicalBody_->applyCentralForce(physicalBody_->getWorldTransform().getBasis() * this->localLinearAcceleration_);
118            this->localLinearAcceleration_.setValue(0, 0, 0);
119
120            this->localAngularAcceleration_ *= this->getLocalInertia() * this->rotationThrust_;
121            this->physicalBody_->applyTorque(physicalBody_->getWorldTransform().getBasis() * this->localAngularAcceleration_);
122            this->localAngularAcceleration_.setValue(0, 0, 0);
123        }
124    }
125
126    void SpaceShip::moveFrontBack(const Vector2& value)
127    {
128        this->localLinearAcceleration_.setZ(this->localLinearAcceleration_.z() - value.x);
129    }
130
131    void SpaceShip::moveRightLeft(const Vector2& value)
132    {
133        this->localLinearAcceleration_.setX(this->localLinearAcceleration_.x() + value.x);
134    }
135
136    void SpaceShip::moveUpDown(const Vector2& value)
137    {
138        this->localLinearAcceleration_.setY(this->localLinearAcceleration_.y() + value.x);
139    }
140
141    void SpaceShip::rotateYaw(const Vector2& value)
142    {
143        this->localAngularAcceleration_.setY(this->localAngularAcceleration_.y() + value.x);
144    }
145
146    void SpaceShip::rotatePitch(const Vector2& value)
147    {
148        this->localAngularAcceleration_.setX(this->localAngularAcceleration_.x() + value.x);
149    }
150
151    void SpaceShip::rotateRoll(const Vector2& value)
152    {
153        this->localAngularAcceleration_.setZ(this->localAngularAcceleration_.z() - value.x);
154    }
155
156    void SpaceShip::fire()
157    {
158    }
159}
Note: See TracBrowser for help on using the repository browser.