Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Apr 18, 2019, 4:04:33 PM (5 years ago)
Author:
pomselj
Message:

oxw file cleaned up

File:
1 edited

Legend:

Unmodified
Added
Removed
  • code/branches/OrxoBlox_FS19/src/modules/weapons/projectiles/BallProjectile.cc

    r12291 r12310  
    4040#include "core/command/Executor.h"
    4141#include "util/Convert.h"
     42#include <bullet/BulletCollision/NarrowPhaseCollision/btManifoldPoint.h>
     43#include <bullet/LinearMath/btVector3.h>
    4244
    4345namespace orxonox
     
    5860    void BallProjectile::registerVariables()
    5961    {
     62        registerVariable( this->fieldWidth_ );
     63        registerVariable( this->fieldHeight_ );
    6064        registerVariable(this->materialBase_);
     65        registerVariable( this->speed_ );
    6166    }
    6267
     
    8792    }
    8893
     94
     95
    8996    void BallProjectile::Bounce(WorldEntity* otherObject, btManifoldPoint& contactPoint, const btCollisionShape* cs) {
    9097
    9198        Vector3 velocity = this->getVelocity();
    92         Vector3 positionOtherObject = otherObject->getPosition();
    93         Vector3 contactPosition = this->getPosition();
     99        Vector3 myPosition = otherObject->getPosition();
     100        btVector3 positionOtherObject = contactPoint.getPositionWorldOnA();
    94101        orxout() << "About to Bounce >D" << endl;
    95102        //if (positionOtherObject.y < 0) {
     
    98105        //else {
    99106       
    100             int distance_X = positionOtherObject.x - contactPosition.x;
    101             int distance_Y = positionOtherObject.y - contactPosition.y;
     107            int distance_X = positionOtherObject.getX() - myPosition.x;
     108            int distance_Z = positionOtherObject.getZ() - myPosition.z;
    102109
    103110            if (distance_X < 0)
    104                 distance_Y = -distance_Y;
     111                distance_X = -distance_X;
    105112   
    106113
    107             if (distance_Y < 0)
    108                 distance_X = -distance_X;
    109 
    110             if (distance_X < distance_Y)
    111                 velocity.y = -velocity.y;
    112             if (distance_Y < distance_X)
     114            if (distance_Z < 0)
     115                distance_Z = -distance_Z;
     116
     117            orxout() << distance_X << endl;
     118            orxout() << distance_Z << endl;
     119
     120            if (distance_X < distance_Z) {
     121                velocity.z = -velocity.z;
     122                orxout() << "z" << endl;
     123            }
     124            if (distance_Z < distance_X) {
    113125                velocity.x = -velocity.x;
     126                orxout() << "x" << endl;
     127            }
    114128            else {
    115129                velocity.x = -velocity.x;
    116                 velocity.y = -velocity.y;
     130                velocity.z = -velocity.z;
     131                orxout() << "both" << endl;
    117132            }
     133            this->setVelocity(velocity);
    118134        //}
    119135    }
    120136
    121 /**
    122     @brief
    123         The function called when a projectile hits another thing.
    124         Calls the hit-function, starts the shield recharge countdown, displays visual hit effects defined in Pawn.
    125         Needs to be called in the collidesAgainst() function by every Class directly inheriting from BasicProjectile.
    126     @param otherObject
    127         A pointer to the object the Projectile has collided against.
    128     @param contactPoint
    129         A btManifoldPoint indicating the point of contact/impact.
    130     @param cs
    131         The btCollisionShape of the other object
    132     @return
    133         Returns true if the collision resulted in a successful hit.
    134     @see Pawn.h
    135     */
     137
     138
     139
    136140   
    137141    bool BallProjectile::processCollision(WorldEntity* otherObject, btManifoldPoint& contactPoint, const btCollisionShape* cs)
     
    145149        return result;
    146150    }
     151
     152
     153
     154
     155
     156
     157    void BallProjectile::tick(float dt)
     158    {
     159        SUPER(BallProjectile, tick, dt);
     160
     161        // Get the current position, velocity and acceleration of the ball.
     162        Vector3 position = this->getPosition();
     163        Vector3 velocity = this->getVelocity();
     164        Vector3 acceleration = this->getAcceleration();
     165
     166        // If the ball has gone over the top or bottom boundary of the playing field (i.e. the ball has hit the top or bottom delimiters).
     167        if (position.z > this->fieldHeight_ / 2 || position.z < -this->fieldHeight_ / 2)
     168        {
     169            // Its velocity in z-direction is inverted (i.e. it bounces off).
     170            velocity.z = -velocity.z;
     171            // And its position is set as to not overstep the boundary it has just crossed.
     172            if (position.z > this->fieldHeight_ / 2)
     173                position.z = this->fieldHeight_ / 2;
     174            if (position.z < -this->fieldHeight_ / 2)
     175                position.z = -this->fieldHeight_ / 2;
     176
     177            this->fireEvent();
     178        }
     179       
     180        //Ball hits the right or left wall and should bounce back.
     181        // If the ball has crossed the left or right boundary of the playing field.
     182        if (position.x > this->fieldWidth_ / 2 || position.x < -this->fieldWidth_ / 2)
     183        {
     184            //Ball hits the right Wall
     185            if (position.x > this->fieldWidth_ / 2)
     186                {
     187                    // Set the ball to be exactly at the boundary.
     188                    position.x = this->fieldWidth_ / 2;
     189                    // Invert its velocity in x-direction (i.e. it bounces off).
     190                    velocity.x = -velocity.x;
     191                    this->fireEvent();
     192                    }
     193
     194            //Ball hits the left wall
     195            else if (position.x < -this->fieldWidth_ / 2)
     196                {
     197                        // Set the ball to be exactly at the boundary.
     198                        position.x = -this->fieldWidth_ / 2;
     199                        // Invert its velocity in x-direction (i.e. it bounces off).
     200                        velocity.x = -velocity.x;
     201                        this->fireEvent();
     202                    }
     203        }
     204
     205        // Set the position, velocity and acceleration of the ball, if they have changed.
     206        if (acceleration != this->getAcceleration())
     207            this->setAcceleration(acceleration);
     208        if (velocity != this->getVelocity())
     209            this->setVelocity(velocity);
     210        if (position != this->getPosition())
     211            this->setPosition(position);
     212    }
     213
     214
     215
     216
     217    void BallProjectile::setSpeed(float speed)
     218    {
     219        if (speed != this->speed_) // If the speed changes
     220        {
     221            this->speed_ = speed;
     222
     223            // Set the speed in the direction of the balls current velocity.
     224            Vector3 velocity = this->getVelocity();
     225            if (velocity.x != 0)
     226                velocity.x = sgn(velocity.x) * this->speed_;
     227            else // If the balls current velocity is zero, the speed is set in a random direction.
     228                velocity.x = this->speed_ * sgn(rnd(-1,1));
     229
     230            this->setVelocity(velocity);
     231        }
     232    }
     233
     234
    147235}
Note: See TracChangeset for help on using the changeset viewer.