SFML community forums

Help => General => Topic started by: Tweezy on June 29, 2014, 02:12:08 am

Title: Change origin of rotation
Post by: Tweezy on June 29, 2014, 02:12:08 am
Hi all. I'm making a little 2D tank game, it works relatively to where you click on the screen. So, when you click on the screen it grabs a current location, desired location, does some ATAN2, normalisation and works out where the click was located, how much to rotate by and then when to shoot.

The tank moves by the perfect amount, stops rotating and then fires it projectile to that position.

I have on issue. The rotation of the projectile isn't correct for where the rotation of the tank sprite is. I know the issue, but I'm not quite sure on how to fix it.

At the moment the projectile is being rotated on the top left corner (Default position). I need some help on how to change it to the bottom middle position, this will make the sprite rotate correctly when I call the rotate function.

How can I accomplish this?

Any help would be really, really helpful!

Thanks :)

-- EDIT --

Code

Projectile class


void projectile::init()
{
        laserShape.setTexture(*laserTexturePointer);
        laserShape.setOrigin(laserShape.getLocalBounds().width/2.0f, laserShape.getLocalBounds().height/2.0f);
        laserShape.setPosition(580,280);
}
void projectile::input(sf::RenderWindow &window)
{
        p.current.x = laserShape.getPosition().x - laserShape.getGlobalBounds().width/2.f;
        p.current.y = laserShape.getPosition().y - laserShape.getGlobalBounds().height/2.f;

        p.desired.x = sf::Mouse::getPosition(window).x;
        p.desired.y = sf::Mouse::getPosition(window).y;
        laserShape.setRotation(p.rotateToTarget(p).x);

        //std::cout<<"X Rotation Amount :"<<p.rotateToTarget(p).x<<std::endl;
}
void projectile::update()
{
        laserShape.move(p.fireTo(p).x, p.fireTo(p).y);
}

void projectile::draw(sf::RenderWindow &window)
{
        window.draw(laserShape);
}

Rotation function


vector2D position::rotateToTarget(position p)
{
        delta.x = desired.x - current.x;
        delta.y = desired.y - current.y;

        rotationAmount = atan2(delta.x, delta.y)*180/PI;

        return rotationAmount;
}

How I call it all


      
if (sf::Mouse::isButtonPressed(sf::Mouse::Left))
                {      
                        bullet.input(window);  
                        bulletVec.push_back(bullet);
                        std::cout<<bulletVec.size()<<std::endl;
                }

Then in my update function I just iterate over my bulletObject vector to call the projectile::update() function.

The code it pretty simple, not much happening at the moment.
Title: Re: Change origin of rotation
Post by: G. on June 29, 2014, 03:09:31 am
Your sprites rotates around its origin, which is by default in (0, 0) its top left corner. If you want it to rotate around its bottom center, then set its origin (http://www.sfml-dev.org/documentation/2.1/classsf_1_1Sprite.php#a56c67bd80aae8418d13fb96c034d25ec) to its bottom center. ;)
Title: Re: Change origin of rotation
Post by: Tweezy on June 29, 2014, 01:01:03 pm
I know I need to set its origin, but whenever I try and change it based of the width and height it always goes really, really strange and weird. This is the code i've tried;


float width = myShape.getGlobalBounds().Width;
float height = myShape.getGlobalBounds().Height;

This returns the width and height of by object. From here I don't know how to actually get the bottom middle point. Think i need a coffee break to logically think about what the hell I'm wanting to do haha
Title: Re: Change origin of rotation
Post by: Hapax on June 29, 2014, 02:47:45 pm
Try getLocalBounds() instead of getGlobalBounds() as the global version takes into account any transformations.

Also, bottom-middle would be (width/2, height) or (getLocalBounds().width/2, getLocalBounds().height).
Title: Re: Change origin of rotation
Post by: Tweezy on June 29, 2014, 03:55:16 pm
thanks for the reply Eagle. This doesn't work. Perhaps there is an issue somewhere else. I'll post my code up (See OP)
Title: Re: Change origin of rotation
Post by: Hapax on June 29, 2014, 04:04:12 pm
If you want the bottom, don't divide height by 2; that would give the centre.
Title: Re: Change origin of rotation
Post by: Tweezy on June 29, 2014, 04:09:13 pm
If you want the bottom, don't divide height by 2; that would give the centre.

Decided the general center of the sprite would be a better location to rotate it by so that it doesn't alter its position as it carries on rotating (Which will screw it up).

Still not helping though, this is damn strange.
Title: Re: Change origin of rotation
Post by: Tweezy on June 29, 2014, 04:12:48 pm
Ah shit, i've found an issue with my rotateToTarget function.

It return -180 to 180 degrees, but whenever it is enabled it keeps shifting the position of the laserShape while actually rotating it. This is strange
Title: Re: Change origin of rotation
Post by: Hapax on June 29, 2014, 04:30:13 pm
At least your rotation of the sprite is now around the origin you wanted ;)

We can't see any of the code for the problem you're having. If (you are certain that) your problem is in the rotateToTarget function, we should see its code.
Title: Re: Change origin of rotation
Post by: Tweezy on June 29, 2014, 04:30:50 pm
Check OP eagle, edited it with the my code.
Title: Re: Change origin of rotation
Post by: Tweezy on June 29, 2014, 04:35:53 pm
Realised I had done a stupid mistake with my rotate function, it doesn't  (Shouldn't) need to return an x / y, just an amount. Changed that bit of the code to just return a float, not a vector.

Didn't affect the outcome of the rotation though, darn it.
Title: Re: Change origin of rotation
Post by: Hapax on June 29, 2014, 04:37:48 pm
It looks like rotateToTarget just chooses the "look at" direction. Am I right?

Unfortunately, I can't really understand what the problem is. Could you create the usual complete and minimal example (http://en.sfml-dev.org/forums/index.php?topic=5559.msg36368#msg36368) or, maybe, show us a video of what it's doing and tell us what it's not doing correctly?
Title: Re: Change origin of rotation
Post by: Tweezy on June 29, 2014, 04:39:17 pm
the rotateToTarget just returns an angle to rotate by depending on where i have clicked. I'll create a video, think that'll be the easiest option. Give me 10 minutes to downloads fraps
Title: Re: Change origin of rotation
Post by: Hapax on June 29, 2014, 04:44:23 pm
Since the laser travels in a straight line to a stationary destination, and the tank is already aiming and rotated towards this destination/target, can you not use the tank's rotation for the laser?
Is the laser's problem affected different by different tank rotations?
Title: Re: Change origin of rotation
Post by: Tweezy on June 29, 2014, 04:47:39 pm
That is what I want to move onto, yes. But even then i'm going to have issues with the rotation of the sprite, so I need to get this bit sorted:


http://johns-webdesign.com/movies/My%20Movie.mp4
Title: Re: Change origin of rotation
Post by: Hapax on June 29, 2014, 05:14:09 pm
Rotation-wise the laser needs to be flipped so you could try inverting the rotation i.e. return -rotationAmount;

For a better understanding of how the laser reacts to different positions, try clicking on points all the way around the emitter ("tank") - not just horizontal and diagonals - and also different distances from it.
Title: Re: Change origin of rotation
Post by: Tweezy on June 29, 2014, 05:23:16 pm
Rotation-wise the laser needs to be flipped so you could try inverting the rotation i.e. return -rotationAmount;

For a better understanding of how the laser reacts to different positions, try clicking on points all the way around the emitter ("tank") - not just horizontal and diagonals - and also different distances from it.

Holy moly that worked, I never would of thought to do that, what is your reasoning behind inverting the rotation?
Title: Re: Change origin of rotation
Post by: Hapax on June 29, 2014, 05:28:31 pm
what is your reasoning behind inverting the rotation?
I noticed that when the target was above, it was vertical, but when you went to the top-right, it leaned left. Also, when you weren't perfectly above (you were slightly to the right), it leaned slightly to the left.

Glad it worked and that I could be of some help :)
Title: Re: Change origin of rotation
Post by: Tweezy on June 29, 2014, 05:29:51 pm
what is your reasoning behind inverting the rotation?
I noticed that when the target was above, it was vertical, but when you went to the top-right, it leaned left. Also, when you weren't perfectly above (you were slightly to the right), it leaned slightly to the left.

Glad it worked and that I could be of some help :)

You have a keen eye Sir, thank you for pointing it out!
Title: Re: Change origin of rotation
Post by: Hapax on June 29, 2014, 05:57:41 pm
If you'd have clicking all around it in a circle, you may have noticed yourself that the rotation was going in the opposite direction. I wasn't sure which is why I asked you to try that clicking in a circle thing  ;)

I'm glad to help but please don't call me Sir  ;D