Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

Added callback for collisions. Every WorldEntity that collides against another will be called with the following function:
virtual bool collidesAgainst(WorldEntity* otherObject, btManifoldPoint& contactPoint);
btManifoldPoint is from Bullet and tells you more about the contact point, like position.

Per default, the callback is disabled. Enable it with this→enableCollisionCallback(), for instance in your derived class constructor.
Note that if you activate the callback for e.g. SpaceShips, but not for MovableEntities, then collidesAgainst will only be called in the SpaceShip. This could be changed however, just ask me.

  • Property svn:eol-style set to native
File size: 5.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    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        // Get notification about collisions
64        this->enableCollisionCallback();
65
66        this->setConfigValues();
67        this->registerVariables();
68    }
69
70    SpaceShip::~SpaceShip()
71    {
72    }
73
74    void SpaceShip::XMLPort(Element& xmlelement, XMLPort::Mode mode)
75    {
76        SUPER(SpaceShip, XMLPort, xmlelement, mode);
77
78        XMLPortParamVariable(SpaceShip, "primaryThrust",  primaryThrust_,  xmlelement, mode);
79        XMLPortParamVariable(SpaceShip, "auxilaryThrust", auxilaryThrust_, xmlelement, mode);
80        XMLPortParamVariable(SpaceShip, "rotationThrust", rotationThrust_, xmlelement, mode);
81    }
82
83    void SpaceShip::registerVariables()
84    {
85        registerVariable(this->primaryThrust_,  variableDirection::toclient);
86        registerVariable(this->auxilaryThrust_, variableDirection::toclient);
87        registerVariable(this->rotationThrust_, variableDirection::toclient);
88    }
89
90    void SpaceShip::setConfigValues()
91    {
92        SetConfigValue(bInvertYAxis_, false).description("Set this to true for joystick-like mouse behaviour (mouse up = ship down).");
93    }
94
95    bool SpaceShip::isCollisionTypeLegal(WorldEntity::CollisionType type) const
96    {
97        if (type != WorldEntity::Dynamic)
98        {
99            CCOUT(1) << "Error: Cannot tell a SpaceShip not to be dynamic! Ignoring." << std::endl;
100            assert(false); // Only in debug mode
101            return false;
102        }
103        else
104            return true;
105    }
106
107    void SpaceShip::tick(float dt)
108    {
109        SUPER(SpaceShip, tick, dt);
110
111        if (this->isLocallyControlled())
112        {
113            this->localLinearAcceleration_.setX(this->localLinearAcceleration_.x() * getMass() * this->auxilaryThrust_);
114            this->localLinearAcceleration_.setY(this->localLinearAcceleration_.y() * getMass() * this->auxilaryThrust_);
115            if (this->localLinearAcceleration_.z() > 0)
116                this->localLinearAcceleration_.setZ(this->localLinearAcceleration_.z() * getMass() * this->auxilaryThrust_);
117            else
118                this->localLinearAcceleration_.setZ(this->localLinearAcceleration_.z() * getMass() * this->primaryThrust_);
119            this->physicalBody_->applyCentralForce(physicalBody_->getWorldTransform().getBasis() * this->localLinearAcceleration_);
120            this->localLinearAcceleration_.setValue(0, 0, 0);
121
122            this->localAngularAcceleration_ *= this->getLocalInertia() * this->rotationThrust_;
123            this->physicalBody_->applyTorque(physicalBody_->getWorldTransform().getBasis() * this->localAngularAcceleration_);
124            this->localAngularAcceleration_.setValue(0, 0, 0);
125        }
126    }
127
128    void SpaceShip::moveFrontBack(const Vector2& value)
129    {
130        this->localLinearAcceleration_.setZ(this->localLinearAcceleration_.z() - value.x);
131    }
132
133    void SpaceShip::moveRightLeft(const Vector2& value)
134    {
135        this->localLinearAcceleration_.setX(this->localLinearAcceleration_.x() + value.x);
136    }
137
138    void SpaceShip::moveUpDown(const Vector2& value)
139    {
140        this->localLinearAcceleration_.setY(this->localLinearAcceleration_.y() + value.x);
141    }
142
143    void SpaceShip::rotateYaw(const Vector2& value)
144    {
145        this->localAngularAcceleration_.setY(this->localAngularAcceleration_.y() + value.x);
146    }
147
148    void SpaceShip::rotatePitch(const Vector2& value)
149    {
150        this->localAngularAcceleration_.setX(this->localAngularAcceleration_.x() + value.x);
151    }
152
153    void SpaceShip::rotateRoll(const Vector2& value)
154    {
155        this->localAngularAcceleration_.setZ(this->localAngularAcceleration_.z() - value.x);
156    }
157
158    void SpaceShip::fire()
159    {
160    }
161}
Note: See TracBrowser for help on using the repository browser.