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

Author Topic: Inverting velocity to bounce an object (along x-axis)  (Read 5061 times)

0 Members and 1 Guest are viewing this topic.

anoynlk

  • Newbie
  • *
  • Posts: 15
    • View Profile
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:

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

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: Inverting velocity to bounce an object (along x-axis)
« Reply #1 on: April 14, 2017, 11:19:41 pm »
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 think so. I'm not quite sure what your assumption is as what should happen and what your observation is as to what is happening. So I'm not sure what your actual problem is.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

anoynlk

  • Newbie
  • *
  • Posts: 15
    • View Profile
Re: Inverting velocity to bounce an object (along x-axis)
« Reply #2 on: April 15, 2017, 12:13:14 am »
The problem is that it does not invert the velocity. It just travels off screen.

UroboroStudio

  • Newbie
  • *
  • Posts: 25
    • View Profile
    • Email
Re: Inverting velocity to bounce an object (along x-axis)
« Reply #3 on: April 15, 2017, 12:24:55 pm »
It's a wild guess, but the way you are describing the problem is that the IF statements in the update() function are never true.

So I would say to check that your position vector (which I suppose is a member variable of the class Enemy3) is not updated correctly when you call move().

I mean, if you are using a sf::Sprite to represent and draw your enemies, moving the sprite also change it's internal position automatically (which can be obtained by getPosition()) but not your custom position vector.

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Inverting velocity to bounce an object (along x-axis)
« Reply #4 on: April 15, 2017, 12:39:47 pm »
Assuming that we can just ignore the y position then and without it, it should just bounce from side to side, negating the velocity might not be quite enough as it could still be out of range and be re-inverted next time.

If an assumed position was 799.9:
799.9 + (v * dt) = new position
if v = 1 and dt = 1:
799.9 + (1 * 1) = 799.9 + 1 = 800.9
If, then, on the next update, dt is lower...
for example, if dt is now 0.5:
800.9 >= 800 so v becomes inverted (v = -1)
then:
800.9 + (-1 * 0.5) = 800.9 + -0.5 = 800.9 - 0.5 = 800.4

As you can see, in the next update, it's still greater than or equal to 800 so the velocity would be inverted again and increase the position value.

The easy solution to this is to move it back into range when it goes out of range:
if (position.x > 800.f) position.x = 800.f;
Of course, the velocity still needs to be flipped as before but this movement stops it from being able to re-flip it.
Also, the same "clamp" would need to be applied to the lower limit.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

anoynlk

  • Newbie
  • *
  • Posts: 15
    • View Profile
Re: Inverting velocity to bounce an object (along x-axis)
« Reply #5 on: April 15, 2017, 09:07:26 pm »
Hey,

I tried this, changed my code to:

Code: [Select]
        if (position.x >= 800)
{
position.x = 799;
velocity.x = -velocity.x;
}
if (position.x =< 0)
{
position.x = 1;
velocity.x = -velocity.x;
}
move(velocity * dt);
updateAABB(); // updating AABB collision

but still just going past the edge and offscreen.

Seems very simple and this logic should work i'm at a loss as to what i'm doing wrong.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: Inverting velocity to bounce an object (along x-axis)
« Reply #6 on: April 15, 2017, 09:24:18 pm »
Enemy3 is derived from sf::Sprite?
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

anoynlk

  • Newbie
  • *
  • Posts: 15
    • View Profile
Re: Inverting velocity to bounce an object (along x-axis)
« Reply #7 on: April 15, 2017, 09:28:12 pm »
No it inherits from: sf::RectangleShape

Mortal

  • Sr. Member
  • ****
  • Posts: 284
    • View Profile
Re: Inverting velocity to bounce an object (along x-axis)
« Reply #8 on: April 15, 2017, 09:57:04 pm »
i think you need to check this link for 2D physics.
http://trederia.blogspot.co.uk/2016/02/2d-physics-101-pong.html

anoynlk

  • Newbie
  • *
  • Posts: 15
    • View Profile
Re: Inverting velocity to bounce an object (along x-axis)
« Reply #9 on: April 16, 2017, 04:38:33 pm »
This post is talking about collision however I am simply looking to invert the x velocity when it reaches a certain point in the window.

Is there somthing else to it other than velocity.x = -velocity.x ?

I should note if I move the enemy by updating the position using step variable such as
Code: [Select]
if (position.x >= 800)
{
    position.x = 799;
    step = -step;
}
if (position.x <= 0)
{
    position.x = 1;
    step = -step;
}
position.x += position * step;
setPosition(position);

This works.

However if I do it this way the object will not increase its Y velocity as time goes on.

This is a problem because ALL enemies take the player velocity every frame and use it as there own. The player does not actually move, however it calculates what its velocity should be each frame so that all other objects in the game can use it (and apply it negativley to there own y direction) to give the illusion of the player falling. (this was the best way I could code this to make sure all enemies move at the same speed).

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Inverting velocity to bounce an object (along x-axis)
« Reply #10 on: April 17, 2017, 01:17:12 am »
Your "this works" code does not include "updateAABB()". Maybe it's this that's causing the problems.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

anoynlk

  • Newbie
  • *
  • Posts: 15
    • View Profile
Re: Inverting velocity to bounce an object (along x-axis)
« Reply #11 on: April 17, 2017, 10:49:09 am »
Sorry I really should have just linked the entire .update(); functions code, it is actually still there I just linked the code I was using to move the object.

Is it worth sitting transparent objects at the side of the screen and trying to invert the velocity when they hit them? I feel this would not be efficient as it is pretty much unnecessary collision checks being made every frame.

anoynlk

  • Newbie
  • *
  • Posts: 15
    • View Profile
Re: Inverting velocity to bounce an object (along x-axis)
« Reply #12 on: April 17, 2017, 02:44:18 pm »
Ok so I have managed to solve this!

in the IF statement I changed it to getPosition().x instead of position.x

So it looks like this:
Code: [Select]
void Enemy3::update(float dt)
{
// moving from left to right of the screen and having negative gravity effecting the y movement
if ([b]getPosition.x[/b] >= 800)
{
velocity.x= -velocity.x;
}
if ([b]getPosition.x[/b] <= 0)
{
velocity.x = -velocity.x;
}
move(velocity * dt);
updateAABB(); // updating AABB collision
}

Incase anyone else has a similar problem!