Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 2303 was 2303, checked in by rgrieder, 15 years ago
  • Added support for attaching physical WorldEntities to each other. Currently you can only add static objects to kinematic/dynamic/static other objects. Future plans involve attaching kinematic objects to static/kinematic ones. All other combinations don't make sense at all.
  • Added CollisionShape, SphereCollisionShape and CompoundCollisionShape

Usage for collision shapes (example):

<LinearEntity collisionType="kinematic">

<attached>

<Model position="0,0,0" scale=10 mesh="ast1.mesh" />
<StaticEntity position="0,10,0" collisionType="static"> # Everything else than "static" fails!

<collisionShapes>

<SphereCollisionShape radius=40 position="10,10,-10"/>
<CompoundCollisionShape position="4,4,4"> # You can also make compound shapes directly

<SphereCollisionShape radius=10/>

</CompoundCollisionShape>

</collisionShapes>

</StaticEntity>

</attached>

</LinearEntity>

  • Property svn:eol-style set to native
File size: 7.7 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
85    void SpaceShip::registerVariables()
86    {
87        REGISTERDATA(this->maxSpeed_,                network::direction::toclient);
88        REGISTERDATA(this->maxSecondarySpeed_,       network::direction::toclient);
89        REGISTERDATA(this->maxRotation_,             network::direction::toclient);
90        REGISTERDATA(this->translationAcceleration_, network::direction::toclient);
91        REGISTERDATA(this->rotationAcceleration_,    network::direction::toclient);
92        REGISTERDATA(this->translationDamping_,      network::direction::toclient);
93    }
94
95    void SpaceShip::setConfigValues()
96    {
97        SetConfigValue(bInvertYAxis_, false).description("Set this to true for joystick-like mouse behaviour (mouse up = ship down).");
98    }
99
100    void SpaceShip::tick(float dt)
101    {
102        if (this->isLocallyControlled())
103        {
104            // #####################################
105            // ############# STEERING ##############
106            // #####################################
107
108            Vector3 velocity = this->getVelocity();
109            if (velocity.x > this->maxSecondarySpeed_)
110                velocity.x = this->maxSecondarySpeed_;
111            if (velocity.x < -this->maxSecondarySpeed_)
112                velocity.x = -this->maxSecondarySpeed_;
113            if (velocity.y > this->maxSecondarySpeed_)
114                velocity.y = this->maxSecondarySpeed_;
115            if (velocity.y < -this->maxSecondarySpeed_)
116                velocity.y = -this->maxSecondarySpeed_;
117            if (velocity.z > this->maxSecondarySpeed_)
118                velocity.z = this->maxSecondarySpeed_;
119            if (velocity.z < -this->maxSpeed_)
120                velocity.z = -this->maxSpeed_;
121
122            // normalize velocity and acceleration
123            for (size_t dimension = 0; dimension < 3; ++dimension)
124            {
125                if (this->acceleration_[dimension] == 0)
126                {
127                    if (velocity[dimension] > 0)
128                    {
129                        velocity[dimension] -= (this->translationDamping_ * dt);
130                        if (velocity[dimension] < 0)
131                            velocity[dimension] = 0;
132                    }
133                    else if (velocity[dimension] < 0)
134                    {
135                        velocity[dimension] += (this->translationDamping_ * dt);
136                        if (velocity[dimension] > 0)
137                            velocity[dimension] = 0;
138                    }
139                }
140            }
141
142            this->setVelocity(velocity);
143        }
144
145
146        SUPER(SpaceShip, tick, dt);
147
148
149        if (this->isLocallyControlled())
150        {
151            this->yaw(this->yawRotation_ * dt);
152            if (this->bInvertYAxis_)
153                this->pitch(Degree(-this->pitchRotation_ * dt));
154            else
155                this->pitch(Degree( this->pitchRotation_ * dt));
156            this->roll(this->rollRotation_ * dt);
157
158            this->acceleration_.x = 0;
159            this->acceleration_.y = 0;
160            this->acceleration_.z = 0;
161
162            this->yawRotation_   = this->zeroDegree_;
163            this->pitchRotation_ = this->zeroDegree_;
164            this->rollRotation_  = this->zeroDegree_;
165        }
166    }
167
168    void SpaceShip::moveFrontBack(const Vector2& value)
169    {
170        assert(this->physicalBody_);
171        this->physicalBody_->applyCentralForce(btVector3(0.0f, 0.0f, -1.0f/this->physicalBody_->getInvMass() * value.x));
172//        this->acceleration_.z = -this->translationAcceleration_ * value.x;
173    }
174
175    void SpaceShip::moveRightLeft(const Vector2& value)
176    {
177        this->physicalBody_->applyCentralForce(btVector3(1.0f/this->physicalBody_->getInvMass() * value.x, 0.0f, 0.0f));
178//        this->acceleration_.x = this->translationAcceleration_ * value.x;
179    }
180
181    void SpaceShip::moveUpDown(const Vector2& value)
182    {
183        this->physicalBody_->applyCentralForce(btVector3(0.0f, 1.0f/this->physicalBody_->getInvMass() * value.x, 0.0f));
184//        this->acceleration_.y = this->translationAcceleration_ * value.x;
185    }
186
187    void SpaceShip::rotateYaw(const Vector2& value)
188    {
189        Degree temp = value.x * value.x * sgn(value.x) * this->rotationAcceleration_;
190        if (temp > this->maxRotation_)
191            temp = this->maxRotation_;
192        if (temp < -this->maxRotation_)
193            temp = -this->maxRotation_;
194        this->yawRotation_ = Degree(temp);
195    }
196
197    void SpaceShip::rotatePitch(const Vector2& value)
198    {
199        Degree temp = value.x * value.x * sgn(value.x) * this->rotationAcceleration_;
200        if (temp > this->maxRotation_)
201            temp = this->maxRotation_;
202        if (temp < -this->maxRotation_)
203            temp = -this->maxRotation_;
204        this->pitchRotation_ = Degree(temp);
205    }
206
207    void SpaceShip::rotateRoll(const Vector2& value)
208    {
209        Degree temp = value.x * value.x * sgn(value.x) * this->rotationAcceleration_;
210        if (temp > this->maxRotation_)
211            temp = this->maxRotation_;
212        if (temp < -this->maxRotation_)
213            temp = -this->maxRotation_;
214        this->rollRotation_ = Degree(temp);
215    }
216
217    void SpaceShip::fire()
218    {
219    }
220}
Note: See TracBrowser for help on using the repository browser.