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

Author Topic: Problem with animated background in its own sf::View  (Read 1966 times)

0 Members and 1 Guest are viewing this topic.

NGM88

  • Full Member
  • ***
  • Posts: 162
    • View Profile
Problem with animated background in its own sf::View
« on: December 02, 2017, 01:08:26 pm »
I have a background image - a seamless repeated texture.

I increment the xy coordinates of the texture every game cycle to animate it. It gives a nice parallax sky effect.

This texture is drawn on "viewWindow", a view with a fixed center.

I have a player sprite. This sprite moves (6, 0) to the right or (-6, 0) to the left of the screen.

I have a "viewWorld" that always centers on the player sprite. The player sprite is drawn in this view.

Here's the problem:

When the player moves right, the background image no longer moves on the x axis. It looks like it's just going up where as it should be going up AND left. And when the player moves left, the background image looks like it's going twice as fast on the x axis.

To explain it in another way:

When the player is still, the background moves (-1, -1). I want it to always move like this.

But when the player goes right, the background looks like it's moving (0, 1); and when the player goes left, the background looks like it's moving (2, 1).

Can anybody explain why this is happening and how I can fix this? It makes no sense to me because the background image and the player image is on different sf::Views.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10848
    • View Profile
    • development blog
    • Email
Re: Problem with animated background in its own sf::View
« Reply #1 on: December 02, 2017, 01:16:39 pm »
It's just relative movement.

When the player stands still and the animation is going at say 1px/s, then the realtive movement is 1px/s.

When the player moves at 1px/s with the direction of the animation, then the relative movement is 0.

When the player moves in the opposite direction of the animation, then the relative movement is 2px/s.

A solution to make it not a weird looking is to have different speeds for player and animation, plus you could define a certain area in which the player can move without changing the views position.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

NGM88

  • Full Member
  • ***
  • Posts: 162
    • View Profile
Re: Problem with animated background in its own sf::View
« Reply #2 on: December 02, 2017, 01:22:31 pm »
When the player moves at 1px/s with the direction of the animation, then the relative movement is 0.

What makes zero sense to me is that the player is not moving 1 pixel per cycle, it's moving 6 pixels per cycle. and the relative movement of the background stays the same no matter how "fast" I make the player, like when I double the speed to 12 pixels per cycle.

I guess I'll just give player a direction variable and make a switch statement for the background to update at different speeds like you said. Thank you eXpl0it3r.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10848
    • View Profile
    • development blog
    • Email
Re: Problem with animated background in its own sf::View
« Reply #3 on: December 02, 2017, 09:04:13 pm »
If the values you think you've set don't match, then start a debugger and find out what the actual values are. Maybe you're using the wrong variable somewhere.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

 

anything