Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/orxonox/objects/worldentities/PongBall.cc @ 2885

Last change on this file since 2885 was 2885, checked in by landauf, 15 years ago

Extended PongAI:

  • Random prediction errors depend on the vertical ball-speed
  • Fixed a bug: Position correction was also affected by the reaction delay. Now it works instantly.
  • Added oscillation avoidance system (the already implemented hysteresis avoidance system failed at low framerates)

Additionally fixed auto-respawn in Pong Gametype.

  • Property svn:eol-style set to native
File size: 4.8 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 "objects/worldentities/PongBat.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->relMercyOffset_ = 0.05;
49    }
50
51    void PongBall::tick(float dt)
52    {
53        SUPER(PongBall, tick, dt);
54
55        if (Core::isMaster())
56        {
57            Vector3 position = this->getPosition();
58            Vector3 velocity = this->getVelocity();
59
60            if (position.z > this->fieldHeight_ / 2 || position.z < -this->fieldHeight_ / 2)
61            {
62                velocity.z = -velocity.z;
63
64                if (position.z > this->fieldHeight_ / 2)
65                    position.z = this->fieldHeight_ / 2;
66                if (position.z < -this->fieldHeight_ / 2)
67                    position.z = -this->fieldHeight_ / 2;
68            }
69
70            if (position.x > this->fieldWidth_ / 2 || position.x < -this->fieldWidth_ / 2)
71            {
72                float distance = 0;
73
74                if (this->bat_)
75                {
76                    if (position.x > this->fieldWidth_ / 2 && this->bat_[1])
77                    {
78                        distance = (position.z - this->bat_[1]->getPosition().z) / (this->fieldHeight_ * (this->batlength_ * 1.10) / 2);
79                        if (fabs(distance) <= 1)
80                        {
81                            position.x = this->fieldWidth_ / 2;
82                            velocity.x = -velocity.x;
83                            velocity.z = distance * distance * sgn(distance) * PongBall::MAX_REL_Z_VELOCITY * this->speed_;
84                        }
85                        else if (position.x > this->fieldWidth_ / 2 * (1 + this->relMercyOffset_))
86                        {
87                            if (this->getGametype() && this->bat_[0])
88                            {
89                                this->getGametype()->playerScored(this->bat_[0]->getPlayer());
90                                return;
91                            }
92                        }
93                    }
94                    if (position.x < -this->fieldWidth_ / 2 && this->bat_[0])
95                    {
96                        distance = (position.z - this->bat_[0]->getPosition().z) / (this->fieldHeight_ * (this->batlength_ * 1.10) / 2);
97                        if (fabs(distance) <= 1)
98                        {
99                            position.x = -this->fieldWidth_ / 2;
100                            velocity.x = -velocity.x;
101                            velocity.z = distance * distance * sgn(distance) * PongBall::MAX_REL_Z_VELOCITY * this->speed_;
102                        }
103                        else if (position.x < -this->fieldWidth_ / 2 * (1 + this->relMercyOffset_))
104                        {
105                            if (this->getGametype() && this->bat_[1])
106                            {
107                                this->getGametype()->playerScored(this->bat_[1]->getPlayer());
108                                return;
109                            }
110                        }
111                    }
112                }
113            }
114
115            if (velocity != this->getVelocity())
116                this->setVelocity(velocity);
117            if (position != this->getPosition())
118                this->setPosition(position);
119        }
120    }
121
122    void PongBall::setSpeed(float speed)
123    {
124        if (speed != this->speed_)
125        {
126            this->speed_ = speed;
127
128            Vector3 velocity = this->getVelocity();
129            if (velocity.x != 0)
130                velocity.x = sgn(velocity.x) * this->speed_;
131            else
132                velocity.x = this->speed_ * sgn(rnd(-1,1));
133
134            this->setVelocity(velocity);
135        }
136    }
137}
Note: See TracBrowser for help on using the repository browser.