Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/physics_merge/src/orxonox/objects/worldentities/pawns/SpaceShip.cc @ 2453

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

SpaceShip is now harmonising with Bullet as well. The attributes of the steering can be configured in the XML file. I recommend a very high angularDamping or otherwise it will get very tricky to steer the ship (however more realistic).

  • Property svn:eol-style set to native
File size: 5.3 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            ThrowException(PhysicsViolation, "Cannot tell a SpaceShip not to be dynamic!");
98            return false;
99        }
100        else
101            return true;
102    }
103
104    void SpaceShip::tick(float dt)
105    {
106        SUPER(SpaceShip, tick, dt);
107
108        if (this->isLocallyControlled())
109        {
110            this->localLinearAcceleration_.setX(this->localLinearAcceleration_.x() * getMass() * this->auxilaryThrust_);
111            this->localLinearAcceleration_.setY(this->localLinearAcceleration_.y() * getMass() * this->auxilaryThrust_);
112            if (this->localLinearAcceleration_.z() > 0)
113                this->localLinearAcceleration_.setZ(this->localLinearAcceleration_.z() * getMass() * this->auxilaryThrust_);
114            else
115                this->localLinearAcceleration_.setZ(this->localLinearAcceleration_.z() * getMass() * this->primaryThrust_);
116            this->physicalBody_->applyCentralForce(physicalBody_->getWorldTransform().getBasis() * this->localLinearAcceleration_);
117            this->localLinearAcceleration_.setValue(0, 0, 0);
118
119            this->localAngularAcceleration_ *= this->getLocalInertia() * this->rotationThrust_;
120            this->physicalBody_->applyTorque(physicalBody_->getWorldTransform().getBasis() * this->localAngularAcceleration_);
121            this->localAngularAcceleration_.setValue(0, 0, 0);
122        }
123    }
124
125    void SpaceShip::moveFrontBack(const Vector2& value)
126    {
127        this->localLinearAcceleration_.setZ(this->localLinearAcceleration_.z() - value.x);
128    }
129
130    void SpaceShip::moveRightLeft(const Vector2& value)
131    {
132        this->localLinearAcceleration_.setX(this->localLinearAcceleration_.x() + value.x);
133    }
134
135    void SpaceShip::moveUpDown(const Vector2& value)
136    {
137        this->localLinearAcceleration_.setY(this->localLinearAcceleration_.y() + value.x);
138    }
139
140    void SpaceShip::rotateYaw(const Vector2& value)
141    {
142        this->localAngularAcceleration_.setY(this->localLinearAcceleration_.y() + value.x);
143    }
144
145    void SpaceShip::rotatePitch(const Vector2& value)
146    {
147        this->localAngularAcceleration_.setX(this->localLinearAcceleration_.x() + value.x);
148    }
149
150    void SpaceShip::rotateRoll(const Vector2& value)
151    {
152        this->localAngularAcceleration_.setZ(this->localLinearAcceleration_.z() - value.x);
153    }
154
155    void SpaceShip::fire()
156    {
157    }
158}
Note: See TracBrowser for help on using the repository browser.