1
General / Inverting velocity to bounce an object (along x-axis)
« on: April 14, 2017, 02:57:25 pm »
Hey guys,
I'm programming a small game and apart from a few issues it's coming along nicley.
One problem I have is one of the enemies will move from left to right across the screen, he will also be affected by negative gravity to increase his velocity to travel upwards aswell.
For this i'm using:
The full code for inside it's update(dt); is as follows:
At the moment it will just keep traveling past the screen x edge untill it reaches the max y (800) and will respawn at the bottom of the screen - still traveling at a positive x velocity.
As far as i'm aware this is just inverting the velocity when it reaches the side of the screen? or have I missed somthing.
**I should note its Y velocity is being effected by a manager class which is setting its velocity by taking the players current velocity and matching it through the game.update();
Thanks
I'm programming a small game and apart from a few issues it's coming along nicley.
One problem I have is one of the enemies will move from left to right across the screen, he will also be affected by negative gravity to increase his velocity to travel upwards aswell.
For this i'm using:
Code: [Select]
move(velocity * dt);
The full code for inside it's update(dt); is as follows:
Code: [Select]
void Enemy3::update(float dt)
{
// moving from left to right of the screen and having negative gravity effecting the y movement
if (position.x >= 800)
{
velocity.x= -velocity.x;
}
if (position.x <= 0)
{
velocity.x = -velocity.x;
}
move(velocity * dt);
updateAABB(); // updating AABB collision
}
At the moment it will just keep traveling past the screen x edge untill it reaches the max y (800) and will respawn at the bottom of the screen - still traveling at a positive x velocity.
As far as i'm aware this is just inverting the velocity when it reaches the side of the screen? or have I missed somthing.
**I should note its Y velocity is being effected by a manager class which is setting its velocity by taking the players current velocity and matching it through the game.update();
Thanks