Jump to content

View more

Image of the Day

Boxes as reward for our ranking mode. ヾ(☆▽☆)
#indiedev #gamedev #gameart #screenshotsaturday https://t.co/ALF1InmM7K
IOTD | Top Screenshots

The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.


Sign up now

Rotating a sprite to look at mouse problem - SFML

4: Adsense

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.


  • You cannot reply to this topic
4 replies to this topic

#1 mypel16000   Members   

14
Like
0Likes
Like

Posted 07 January 2013 - 04:42 PM

I just cannot fix this problem about rotating sprites around the mouse. The formula seems fine to me and it is similar to others I have googled. I just cannot figure out why it is behaveing in such a weird way.

 

void Animation::checkRotation(int x, int y, sf::Sprite& backSprite, int px, int py)
{
    yOffset = y - py;
    xOffset = x - px;
    backSprite.SetCenter(px +200, py + 400);
    a = (atan2(yOffset, xOffset)) * 180 / 3.141592;
    backSprite.SetRotation( a );
}

 

where x and y are the mouse coordinates and px and py the sprite image coordinates. I thought that maybe when the sprite rotates, the x and the y also change dynamically, in that case, would there be a solution?

 

I just need a way to get my image looking at the mouse cursor. The sprite.getCenter() doesn't work so I cannot get the center of the image coordinates. Any ideas?

 

P.S: I am using SFML 1.6

 



#2 BeerNutts   Members   

4377
Like
0Likes
Like

Posted 07 January 2013 - 04:59 PM

You should not be calling SetCenter.  SetCenter sets the center point of the object, and it's relative to the object; so, typically the "center" is the upper left of the sprite (0, 0), but you could set it to (ImageWidth/2,ImageHeight/2) and the "Center" would be the center of the image.  This would then be where the "position" of the sprite used.

 

So, don't use SetCenter here; it should be used (if at all) only once when the sprite is created, unless you're doing some really specilized stuff.  Just use setPosition() and getPosition() and assume the sprite uses upper left for it's position and you'll be good.

 

Otherwise, the code looks OK.  the x and y position of the sprite should not change at all.  BTW, the sfml-dev.org forums are very good, you can always ask the SFML specific questions there too.

 

Good luck.


My Gamedev Journal: 2D Game Making, the Easy Way

---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)

#3 ultramailman   Prime Members   

1720
Like
0Likes
Like

Posted 08 January 2013 - 01:46 AM

I've never used sfml before, but some quick googling tells me that getCenter and setCenter is not related to the sprite's position. What it actually does is setting the center for transformations.
source: http://en.sfml-dev.org/forums/index.php?topic=1968.msg12860#msg12860

#4 minibutmany   Members   

1998
Like
0Likes
Like

Posted 08 January 2013 - 11:03 AM

Instead of using atan2, use a regular arc tangent and give the slope.


Stay gold, Pony Boy.

#5 Khatharr   Members   

8633
Like
0Likes
Like

Posted 09 January 2013 - 10:34 PM

Instead of using atan2, use a regular arc tangent and give the slope.

Why?


void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.




Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.