Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

Finally managed to work out some physics. According to my tests, collisions with simple spheres should work with dynamic/kinematic/static objects. There are currently only a limited number of XML parameters, but we're surely going to extend that. Furthermore there is some more thinking to be done concerning changes of btRigidBody properties when it's already added to the world.

  • Property svn:eol-style set to native
File size: 7.9 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 "util/Math.h"
33#include "util/Exception.h"
34#include "core/CoreIncludes.h"
35#include "core/ConfigValueIncludes.h"
36#include "core/XMLPort.h"
37
38namespace orxonox
39{
40    CreateFactory(SpaceShip);
41
42    SpaceShip::SpaceShip(BaseObject* creator) : Pawn(creator)
43    {
44        RegisterObject(SpaceShip);
45
46        this->zeroDegree_ = 0;
47
48        this->maxSpeed_ = 0;
49        this->maxSecondarySpeed_ = 0;
50        this->maxRotation_ = 0;
51        this->translationAcceleration_ = 0;
52        this->rotationAcceleration_ = 0;
53        this->translationDamping_ = 0;
54
55        this->yawRotation_ = 0;
56        this->pitchRotation_ = 0;
57        this->rollRotation_ = 0;
58
59        this->bInvertYAxis_ = false;
60
61        this->setDestroyWhenPlayerLeft(true);
62
63        // create a phyisical body
64        OrxAssert(this->physicalBody_ == 0, " Check why there is already a physical body in the space ship.");
65        this->createPhysicalBody();
66
67        this->setConfigValues();
68        this->registerVariables();
69    }
70
71    SpaceShip::~SpaceShip()
72    {
73    }
74
75    void SpaceShip::XMLPort(Element& xmlelement, XMLPort::Mode mode)
76    {
77        SUPER(SpaceShip, XMLPort, xmlelement, mode);
78
79        XMLPortParam(SpaceShip, "maxspeed",          setMaxSpeed,          getMaxSpeed,          xmlelement, mode);
80        XMLPortParam(SpaceShip, "maxsecondaryspeed", setMaxSecondarySpeed, getMaxSecondarySpeed, xmlelement, mode);
81        XMLPortParam(SpaceShip, "maxrotation",       setMaxRotation,       getMaxRotation,       xmlelement, mode);
82        XMLPortParam(SpaceShip, "transacc",          setTransAcc,          getTransAcc,          xmlelement, mode);
83        XMLPortParam(SpaceShip, "rotacc",            setRotAcc,            getRotAcc,            xmlelement, mode);
84        XMLPortParam(SpaceShip, "transdamp",         setTransDamp,         getTransDamp,         xmlelement, mode);
85    }
86
87    void SpaceShip::registerVariables()
88    {
89        REGISTERDATA(this->maxSpeed_,                network::direction::toclient);
90        REGISTERDATA(this->maxSecondarySpeed_,       network::direction::toclient);
91        REGISTERDATA(this->maxRotation_,             network::direction::toclient);
92        REGISTERDATA(this->translationAcceleration_, network::direction::toclient);
93        REGISTERDATA(this->rotationAcceleration_,    network::direction::toclient);
94        REGISTERDATA(this->translationDamping_,      network::direction::toclient);
95    }
96
97    void SpaceShip::setConfigValues()
98    {
99        SetConfigValue(bInvertYAxis_, false).description("Set this to true for joystick-like mouse behaviour (mouse up = ship down).");
100    }
101
102    void SpaceShip::tick(float dt)
103    {
104        if (this->isLocallyControlled())
105        {
106            // #####################################
107            // ############# STEERING ##############
108            // #####################################
109
110            Vector3 velocity = this->getVelocity();
111            if (velocity.x > this->maxSecondarySpeed_)
112                velocity.x = this->maxSecondarySpeed_;
113            if (velocity.x < -this->maxSecondarySpeed_)
114                velocity.x = -this->maxSecondarySpeed_;
115            if (velocity.y > this->maxSecondarySpeed_)
116                velocity.y = this->maxSecondarySpeed_;
117            if (velocity.y < -this->maxSecondarySpeed_)
118                velocity.y = -this->maxSecondarySpeed_;
119            if (velocity.z > this->maxSecondarySpeed_)
120                velocity.z = this->maxSecondarySpeed_;
121            if (velocity.z < -this->maxSpeed_)
122                velocity.z = -this->maxSpeed_;
123
124            // normalize velocity and acceleration
125            for (size_t dimension = 0; dimension < 3; ++dimension)
126            {
127                if (this->acceleration_[dimension] == 0)
128                {
129                    if (velocity[dimension] > 0)
130                    {
131                        velocity[dimension] -= (this->translationDamping_ * dt);
132                        if (velocity[dimension] < 0)
133                            velocity[dimension] = 0;
134                    }
135                    else if (velocity[dimension] < 0)
136                    {
137                        velocity[dimension] += (this->translationDamping_ * dt);
138                        if (velocity[dimension] > 0)
139                            velocity[dimension] = 0;
140                    }
141                }
142            }
143
144            this->setVelocity(velocity);
145        }
146
147
148        SUPER(SpaceShip, tick, dt);
149
150
151        if (this->isLocallyControlled())
152        {
153            this->yaw(this->yawRotation_ * dt);
154            if (this->bInvertYAxis_)
155                this->pitch(Degree(-this->pitchRotation_ * dt));
156            else
157                this->pitch(Degree( this->pitchRotation_ * dt));
158            this->roll(this->rollRotation_ * dt);
159
160            this->acceleration_.x = 0;
161            this->acceleration_.y = 0;
162            this->acceleration_.z = 0;
163
164            this->yawRotation_   = this->zeroDegree_;
165            this->pitchRotation_ = this->zeroDegree_;
166            this->rollRotation_  = this->zeroDegree_;
167        }
168    }
169
170    void SpaceShip::moveFrontBack(const Vector2& value)
171    {
172        assert(this->physicalBody_);
173        this->physicalBody_->applyCentralForce(btVector3(0.0f, 0.0f, -1.0f/this->physicalBody_->getInvMass() * value.x));
174//        this->acceleration_.z = -this->translationAcceleration_ * value.x;
175    }
176
177    void SpaceShip::moveRightLeft(const Vector2& value)
178    {
179        this->physicalBody_->applyCentralForce(btVector3(1.0f/this->physicalBody_->getInvMass() * value.x, 0.0f, 0.0f));
180//        this->acceleration_.x = this->translationAcceleration_ * value.x;
181    }
182
183    void SpaceShip::moveUpDown(const Vector2& value)
184    {
185        this->physicalBody_->applyCentralForce(btVector3(0.0f, 1.0f/this->physicalBody_->getInvMass() * value.x, 0.0f));
186//        this->acceleration_.y = this->translationAcceleration_ * value.x;
187    }
188
189    void SpaceShip::rotateYaw(const Vector2& value)
190    {
191        Degree temp = value.x * value.x * sgn(value.x) * this->rotationAcceleration_;
192        if (temp > this->maxRotation_)
193            temp = this->maxRotation_;
194        if (temp < -this->maxRotation_)
195            temp = -this->maxRotation_;
196        this->yawRotation_ = Degree(temp);
197    }
198
199    void SpaceShip::rotatePitch(const Vector2& value)
200    {
201        Degree temp = value.x * value.x * sgn(value.x) * this->rotationAcceleration_;
202        if (temp > this->maxRotation_)
203            temp = this->maxRotation_;
204        if (temp < -this->maxRotation_)
205            temp = -this->maxRotation_;
206        this->pitchRotation_ = Degree(temp);
207    }
208
209    void SpaceShip::rotateRoll(const Vector2& value)
210    {
211        Degree temp = value.x * value.x * sgn(value.x) * this->rotationAcceleration_;
212        if (temp > this->maxRotation_)
213            temp = this->maxRotation_;
214        if (temp < -this->maxRotation_)
215            temp = -this->maxRotation_;
216        this->rollRotation_ = Degree(temp);
217    }
218
219    void SpaceShip::fire()
220    {
221    }
222}
Note: See TracBrowser for help on using the repository browser.