Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Collision issue  (Read 1365 times)

0 Members and 1 Guest are viewing this topic.

blobeb

  • Newbie
  • *
  • Posts: 3
    • View Profile
    • Email
Collision issue
« on: November 02, 2013, 10:31:36 am »
My collision isnt working. It doesnt register unless im moving and even then it doesn't register 6 times out of 10.

Code: [Select]
bool player::isCollidingSide(gameBall& gameball)
{

if(right > gameball.left|| left < gameball.right ||
            top > gameball.bottom || bottom < gameball.top)
        {
            return false;
        }
        return true;
}

the update function code
Code: [Select]
void game::update(float &deltatime)
{
sf::Vector2f movement(0.f, 0.f);

if (mIsMovingUp)
movement.y -= 0.01f;

if (mIsMovingDown)
movement.y += 0.01f;

player.rect.move(movement*playerspeed);
float factor=ballspeed*deltatime;
balll.ballShape.move(xvel*ballspeed, yvel*ballspeed);


if(player.isCollidingSide(balll))
{
xvel = -xvel;
std::cout<<"Collision x";
}


}


bonus question:  What does this function do?
Code: [Select]
void Ball::accelerate(Paddle PLAYER)
{
    currentSpeed.y = (ballObject.getGlobalBounds().top
                        + ballObject.getGlobalBounds().height / 2
                            - PLAYER.getRect().top
                                - PLAYER.getRect().height / 2) / 100;
}
« Last Edit: November 02, 2013, 10:34:12 am by blobeb »

The Hatchet

  • Full Member
  • ***
  • Posts: 135
    • View Profile
    • Email
Re: Collision issue
« Reply #1 on: November 02, 2013, 12:07:52 pm »
should probably be
if(right < gameball.left|| left > gameball.right ||
            top > gameball.bottom || bottom < gameball.top)
 
Assuming all your right, left, top, bottom variables are updating correctly for the player and ball.

and
void Ball::accelerate(Paddle PLAYER)
{
    currentSpeed.y = (ballObject.getGlobalBounds().top
                        + ballObject.getGlobalBounds().height / 2
                            - PLAYER.getRect().top
                                - PLAYER.getRect().height / 2) / 100;
}
seems to take the Y distance between the center of the ball and center of the player and divides it by 100 and sets the balls speed to that.  1)This makes no sense to do whatsoever as the ball would go slower the closer it vertically gets to the player and faster the further away.  2)unless you just randomly created this to make your job harder you probably shouldn't just copy/paste chuncks of code without knowing what they do

blobeb

  • Newbie
  • *
  • Posts: 3
    • View Profile
    • Email
Re: Collision issue
« Reply #2 on: November 02, 2013, 01:25:14 pm »
Thanks and i found that peice of code on an example of a pong implementation in SFML.