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

Author Topic: [SOLVED] sf::View - Seems to break rotation towards mouse after Window size  (Read 2741 times)

0 Members and 1 Guest are viewing this topic.

HailBee

  • Newbie
  • *
  • Posts: 25
    • View Profile
    • Email
So my aim is to have:

1. A 1024x768 sf::RenderWindow
2. The game world being twice as large as that so 2048x1536
3. For getRotation() on a sprite not to break when trying to find the mouse outside of the sf::RenderWindow's initial 1024x768 size  (it works perfectly within window co-ords)

Current window/view code:
Quote
SOLVED: Needed to parse the position in relative to the view(not window) with .mapPixelToCoords

//512,384 chosen as its the center of a 1024x768 window, and keeps centered on the sprite that moves the view.

Note: currently my sprite can move outside of the view but "projectiles" being shot towards the mouse seem to clip at the edge of the 1024x768 window as if there was an invisible boundary. My standard aTan2 projectile rotation code can be found here:  http://en.sfml-dev.org/forums/index.php?topic=12468.0   

Note2: Im *guessing* it is somethign to do with 'view.setSize(sf::Vector2f(x,x))' or possibly creating a bigger window and applying zoom, I have tried both and failed miserably. I have also read the http://www.sfml-dev.org/tutorials/2.0/graphics-view.php tutorial which was good but with the additional rotation error I need help!
« Last Edit: August 02, 2013, 03:49:12 pm by HailBee »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Can you please discribe your problem a bit more detailed, because I've no idea what you mean with "break getPosition".
And since description only help up to a certain point you should really write up an minimal but complete example.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

HailBee

  • Newbie
  • *
  • Posts: 25
    • View Profile
    • Email
Sorry I probably should of said it breaks the rotation of the Sprite(a projectile that travels in the direction of the mouse cursor).

So lets say you have your mouse outside of the window(or even within it if you move to the "edge" of the 1024x768 screen) then the projectiles begin to group together and start spinning and can not go any further.

It's as if there is an invisible boundary for them at the edge of the defined RenderWindow size.

Also the projectiles can not rotate towards anything past the defined window size, in fact if the player sprite moves past the defined size then the projectile will automatically turn around and face the opposite direction and can not face a direction beyond the window size.

Rotation code:
Quote
float Player::getRotation(sf::RenderWindow &win)
{
   sf::Vector2f curPos;  //current position(not cursor position, sorry for ambiguous var names!)
   curPos.x = arrowSprite.getGlobalBounds().left;
   curPos.y = arrowSprite.getGlobalBounds().top;
   sf::Vector2i position = sf::Mouse::getPosition(win);

    const float PI = 3.14159;

    float dx = position.x - curPos.x;
    float dy = position.y - curPos.y;

    float rotation = (atan2(dy, dx)) * 180 / PI;

   return rotation;
}
« Last Edit: August 02, 2013, 02:24:20 pm by HailBee »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Sounds like some fun code, but yeah this is not an issue with SFML. Can you create a minimal and complete example that reproduce the problem?
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

HailBee

  • Newbie
  • *
  • Posts: 25
    • View Profile
    • Email
No worries I didnt think it was an issue with SFML but more with my lack of understanding ;), will get onto it now but if you could please clear up one question:

Is the following code saying "a 1024x768 viewable 'view' within a 1024x768 window centered on the point 512,384" ?

Quote
sf::View view(sf::Vector2f(512, 384), sf::Vector2f(1024, 768));
RenderWindow window(VideoMode(1024, 768, 32), "Game Name");

//Because if im correct then it basically just opens up the screen size to an unlimited one (as in I dont need to set a minimum or maximum distance anywhere?)

G.

  • Hero Member
  • *****
  • Posts: 1593
    • View Profile
Aren't you supposed to use mapPixelToCoords? (see last paragraph of the view tutorial)

HailBee

  • Newbie
  • *
  • Posts: 25
    • View Profile
    • Email
Aren't you supposed to use mapPixelToCoords? (see last paragraph of the view tutorial)

That was the exact problem, working perfectly now. I had an inkling that it would be something about calling the rotation in relative to the window and not the 'view' but your awesome for being able to spot it from what little I gave you and point out the code fix to boot!

Thanks heaps :)