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

Author Topic: Cant get Sprite position relative to view [solved]  (Read 2463 times)

0 Members and 1 Guest are viewing this topic.

HailBee

  • Newbie
  • *
  • Posts: 25
    • View Profile
    • Email
Cant get Sprite position relative to view [solved]
« on: September 23, 2013, 11:22:30 am »
Here's what I have so far, I can get it working perfectly fine if the object to set the position on is the mouse but cant seem to work out how to do it with a sf::Sprite:

Quote
for(int i = 0; i < bomb.size(); i++)
      sf::Vector2f playerPos = playSprite.getPosition();
      int playX = playerPos.x;
      int playY = playerPos.y;
      sf::Vector2i convertedPos;
      convertedPos.x = playX;
      convertedPos.y = playY;
      sf::Vector2f worldPos = win.mapPixelToCoords(convertedPos);
      bomb[ i ]->getBullet().setPosition(worldPos.x, worldPos.y);
I didnt use win.getView() in the mapPixelToCoords function for my mouse but saw it in the documentation so giving it a shot during my debugging :P

Any help much appreciated

edit: removed irrelevant function
« Last Edit: September 23, 2013, 12:49:04 pm by HailBee »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Cant get Sprite position relative to view(sf::vector2f to 2i problems?)
« Reply #1 on: September 23, 2013, 11:42:43 am »
I'm not sure to understand what you want to achieve, but I can already tell you that passing a sprite position to the mapPixelToCoords is wrong: the sprite position is already in world coordinates, not in pixel coordinates (a typical example of what's in pixel coordinates is the mouse position).

The fact that you have to manually convert a Vector2f to a Vector2i is usually a good indicator that something's wrong.
Laurent Gomila - SFML developer

HailBee

  • Newbie
  • *
  • Posts: 25
    • View Profile
    • Email
Re: Cant get Sprite position relative to view(sf::vector2f to 2i problems?)
« Reply #2 on: September 23, 2013, 11:50:42 am »
When the player moves from 0,0 the placed sprites appear on an offset between 0,0 and the playerSprite's position.

I figured this was due to the view changing?

The function gets the sprites ready for a projectile move function that moves all bomb's and works perfectly when the player is at 0,0.

//maybe I should add my view stops moving when a player is beyond a given range(so that it isnt always centered)
« Last Edit: September 23, 2013, 12:06:10 pm by HailBee »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Cant get Sprite position relative to view(sf::vector2f to 2i problems?)
« Reply #3 on: September 23, 2013, 12:05:23 pm »
Your player and bombs positions are both in world coordinates, they can already interact with each other without intermediate conversions. I don't know why you want to use the view and pixel coordinates to update your logic, this is totally unrelated.
Laurent Gomila - SFML developer

HailBee

  • Newbie
  • *
  • Posts: 25
    • View Profile
    • Email
Re: Cant get Sprite position relative to view(sf::vector2f to 2i problems?)
« Reply #4 on: September 23, 2013, 12:07:56 pm »
Thanks for the tip about it not being about the view but was just trying it to test it it worked, currently I have:

Quote
      
for(int i = 0; i < bomb.size(); i++)
   {
        sf::Vector2f playerPos = playSprite.getPosition();
             bomb->getBullet().setPosition(playerPos.x, playerPos.y);
    }

Which works fine but like I said only at 0,0 and then gets offset.


EDIT: Fixed, it was a problem with the movement of the sprite after it's position was set. Thanks for your tips.
« Last Edit: September 23, 2013, 12:48:50 pm by HailBee »

 

anything