Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/modularships/src/orxonox/worldentities/MovableEntity.cc @ 9997

Last change on this file since 9997 was 9995, checked in by noep, 11 years ago

Modified collision-detecting-process, such that collisions can be traced back to single child-CollisionShapes of Compound-CollisionShapes

  • Property svn:eol-style set to native
File size: 4.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 *      Reto Grieder
25 *   Co-authors:
26 *      ...
27 *
28 */
29
30#include "MovableEntity.h"
31
32#include "core/CoreIncludes.h"
33#include "core/GameMode.h"
34#include "core/XMLPort.h"
35#include "core/command/Executor.h"
36#include "worldentities/pawns/Pawn.h"
37
38namespace orxonox
39{
40    static const float MAX_RESYNCHRONIZE_TIME = 3.0f;
41    static const float CONTINUOUS_SYNCHRONIZATION_TIME = 10.0f;
42
43    RegisterClass(MovableEntity);
44
45    MovableEntity::MovableEntity(Context* context) : MobileEntity(context)
46    {
47        RegisterObject(MovableEntity);
48
49        this->overwrite_position_    = Vector3::ZERO;
50        this->overwrite_orientation_ = Quaternion::IDENTITY;
51
52        this->continuousResynchroTimer_ = 0;
53
54        this->setPriority(Priority::Low);
55
56        this->registerVariables();
57    }
58
59    MovableEntity::~MovableEntity()
60    {
61        if (this->isInitialized())
62            if (this->continuousResynchroTimer_)
63                this->continuousResynchroTimer_->destroy();
64    }
65
66    void MovableEntity::XMLPort(Element& xmlelement, XMLPort::Mode mode)
67    {
68        SUPER(MovableEntity, XMLPort, xmlelement, mode);
69
70        XMLPortParam(MovableEntity, "enablecollisiondamage", setEnableCollisionDamage, getEnableCollisionDamage, xmlelement, mode).defaultValues(false);
71        XMLPortParam(MovableEntity, "collisiondamage", setCollisionDamage, getCollisionDamage, xmlelement, mode).defaultValues(1);
72    }
73
74    bool MovableEntity::collidesAgainst(WorldEntity* otherObject, btManifoldPoint& contactPoint)
75    {
76        if (GameMode::isMaster() && enableCollisionDamage_)
77        {
78            Pawn* victim = orxonox_cast<Pawn*>(otherObject);
79            if (victim)
80            {
81                float damage = this->collisionDamage_ * (victim->getVelocity() - this->getVelocity()).length();
82                victim->hit(0, contactPoint, damage);
83            }
84        }
85
86        return false;
87    }
88
89    bool MovableEntity::customCollidesAgainst(WorldEntity* otherObject, const btCollisionShape* ownCollisionShape, btManifoldPoint& contactPoint)
90    {
91        if (GameMode::isMaster() && enableCollisionDamage_)
92        {
93            Pawn* victim = orxonox_cast<Pawn*>(otherObject);
94            if (victim)
95            {
96                float damage = this->collisionDamage_ * (victim->getVelocity() - this->getVelocity()).length();
97                victim->customHit(0, contactPoint, ownCollisionShape, damage);
98            }
99        }
100
101        return false;
102    }
103
104
105    void MovableEntity::registerVariables()
106    {
107        registerVariable(this->linearVelocity_,        VariableDirection::ToClient, new NetworkCallback<MovableEntity>(this, &MovableEntity::processLinearVelocity));
108        registerVariable(this->angularVelocity_,       VariableDirection::ToClient, new NetworkCallback<MovableEntity>(this, &MovableEntity::processAngularVelocity));
109
110        registerVariable(this->overwrite_position_,    VariableDirection::ToClient, new NetworkCallback<MovableEntity>(this, &MovableEntity::overwritePosition));
111        registerVariable(this->overwrite_orientation_, VariableDirection::ToClient, new NetworkCallback<MovableEntity>(this, &MovableEntity::overwriteOrientation));
112    }
113
114    void MovableEntity::clientConnected(unsigned int clientID)
115    {
116        this->resynchronizeTimer_.setTimer(rnd() * MAX_RESYNCHRONIZE_TIME, false, createExecutor(createFunctor(&MovableEntity::resynchronize, this)));
117    }
118
119    void MovableEntity::clientDisconnected(unsigned int clientID)
120    {
121    }
122
123    void MovableEntity::resynchronize()
124    {
125        if (GameMode::isMaster() && !this->continuousResynchroTimer_)
126        {
127            // Resynchronise every few seconds because we only work with velocities (no positions)
128            continuousResynchroTimer_ = new Timer(CONTINUOUS_SYNCHRONIZATION_TIME + rnd(-1, 1),
129                true, createExecutor(createFunctor(&MovableEntity::resynchronize, this)), false);
130        }
131
132        this->overwrite_position_ = this->getPosition();
133        this->overwrite_orientation_ = this->getOrientation();
134    }
135}
Note: See TracBrowser for help on using the repository browser.