Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/netp3/src/orxonox/objects/worldentities/PongBall.cc @ 3043

Last change on this file since 3043 was 3043, checked in by scheusso, 15 years ago

first approach of having client-side pong physics
some optimisations in TrafficControl

  • Property svn:eol-style set to native
File size: 7.1 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 "PongBall.h"
31
32#include "core/CoreIncludes.h"
33#include "core/GameMode.h"
34#include "objects/gametypes/Gametype.h"
35
36namespace orxonox
37{
38    CreateFactory(PongBall);
39
40    const float PongBall::MAX_REL_Z_VELOCITY = 1.5;
41
42    PongBall::PongBall(BaseObject* creator) : MovableEntity(creator)
43    {
44        RegisterObject(PongBall);
45
46        this->speed_ = 0;
47        this->bat_ = 0;
48        this->batID_ = new unsigned int[2];
49        this->batID_[0] = OBJECTID_UNKNOWN;
50        this->batID_[1] = OBJECTID_UNKNOWN;
51        this->relMercyOffset_ = 0.05;
52       
53        this->registerVariables();
54    }
55   
56    void PongBall::registerVariables()
57    {
58        registerVariable( this->batID_[0] );
59        registerVariable( this->batID_[1], variableDirection::toclient, new NetworkCallback<PongBall>( this, &PongBall::applyBats) );
60    }
61
62    void PongBall::tick(float dt)
63    {
64        SUPER(PongBall, tick, dt);
65
66        if (GameMode::isMaster())
67        {
68            Vector3 position = this->getPosition();
69            Vector3 velocity = this->getVelocity();
70
71            if (position.z > this->fieldHeight_ / 2 || position.z < -this->fieldHeight_ / 2)
72            {
73                velocity.z = -velocity.z;
74
75                if (position.z > this->fieldHeight_ / 2)
76                    position.z = this->fieldHeight_ / 2;
77                if (position.z < -this->fieldHeight_ / 2)
78                    position.z = -this->fieldHeight_ / 2;
79            }
80
81            if (position.x > this->fieldWidth_ / 2 || position.x < -this->fieldWidth_ / 2)
82            {
83                float distance = 0;
84
85                if (this->bat_)
86                {
87                    if (position.x > this->fieldWidth_ / 2 && this->bat_[1])
88                    {
89                        distance = (position.z - this->bat_[1]->getPosition().z) / (this->fieldHeight_ * (this->batlength_ * 1.10) / 2);
90                        if (fabs(distance) <= 1)
91                        {
92                            position.x = this->fieldWidth_ / 2;
93                            velocity.x = -velocity.x;
94                            velocity.z = distance * distance * sgn(distance) * PongBall::MAX_REL_Z_VELOCITY * this->speed_;
95                        }
96                        else if (position.x > this->fieldWidth_ / 2 * (1 + this->relMercyOffset_))
97                        {
98                            if (this->getGametype() && this->bat_[0])
99                            {
100                                this->getGametype()->playerScored(this->bat_[0]->getPlayer());
101                                return;
102                            }
103                        }
104                    }
105                    if (position.x < -this->fieldWidth_ / 2 && this->bat_[0])
106                    {
107                        distance = (position.z - this->bat_[0]->getPosition().z) / (this->fieldHeight_ * (this->batlength_ * 1.10) / 2);
108                        if (fabs(distance) <= 1)
109                        {
110                            position.x = -this->fieldWidth_ / 2;
111                            velocity.x = -velocity.x;
112                            velocity.z = distance * distance * sgn(distance) * PongBall::MAX_REL_Z_VELOCITY * this->speed_;
113                        }
114                        else if (position.x < -this->fieldWidth_ / 2 * (1 + this->relMercyOffset_))
115                        {
116                            if (this->getGametype() && this->bat_[1])
117                            {
118                                this->getGametype()->playerScored(this->bat_[1]->getPlayer());
119                                return;
120                            }
121                        }
122                    }
123                }
124            }
125
126            if (velocity != this->getVelocity())
127                this->setVelocity(velocity);
128            if (position != this->getPosition())
129                this->setPosition(position);
130        }
131        else
132        {
133          Vector3 position = this->getPosition();
134          Vector3 velocity = this->getVelocity();
135
136          if (position.z > this->fieldHeight_ / 2 || position.z < -this->fieldHeight_ / 2)
137          {
138            velocity.z = -velocity.z;
139
140            if (position.z > this->fieldHeight_ / 2)
141              position.z = this->fieldHeight_ / 2;
142            if (position.z < -this->fieldHeight_ / 2)
143              position.z = -this->fieldHeight_ / 2;
144          }
145
146          if (position.x > this->fieldWidth_ / 2 || position.x < -this->fieldWidth_ / 2)
147          {
148            float distance = 0;
149
150            if (this->bat_)
151            {
152              if (position.x > this->fieldWidth_ / 2 && this->bat_[1])
153              {
154                distance = (position.z - this->bat_[1]->getPosition().z) / (this->fieldHeight_ * (this->batlength_ * 1.10) / 2);
155                if (fabs(distance) <= 1)
156                {
157                  position.x = this->fieldWidth_ / 2;
158                  velocity.x = -velocity.x;
159                  velocity.z = distance * distance * sgn(distance) * PongBall::MAX_REL_Z_VELOCITY * this->speed_;
160                }
161              }
162              if (position.x < -this->fieldWidth_ / 2 && this->bat_[0])
163              {
164                distance = (position.z - this->bat_[0]->getPosition().z) / (this->fieldHeight_ * (this->batlength_ * 1.10) / 2);
165                if (fabs(distance) <= 1)
166                {
167                  position.x = -this->fieldWidth_ / 2;
168                  velocity.x = -velocity.x;
169                  velocity.z = distance * distance * sgn(distance) * PongBall::MAX_REL_Z_VELOCITY * this->speed_;
170                }
171              }
172            }
173          }
174
175          if (velocity != this->getVelocity())
176            this->setVelocity(velocity);
177          if (position != this->getPosition())
178            this->setPosition(position);
179        }
180    }
181
182    void PongBall::setSpeed(float speed)
183    {
184        if (speed != this->speed_)
185        {
186            this->speed_ = speed;
187
188            Vector3 velocity = this->getVelocity();
189            if (velocity.x != 0)
190                velocity.x = sgn(velocity.x) * this->speed_;
191            else
192                velocity.x = this->speed_ * sgn(rnd(-1,1));
193
194            this->setVelocity(velocity);
195        }
196    }
197}
Note: See TracBrowser for help on using the repository browser.