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

Author Topic: Change origin of rotation  (Read 8449 times)

0 Members and 1 Guest are viewing this topic.

Tweezy

  • Newbie
  • *
  • Posts: 43
    • View Profile
Change origin of rotation
« 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.
« Last Edit: June 29, 2014, 03:58:24 pm by Tweezy »

G.

  • Hero Member
  • *****
  • Posts: 1593
    • View Profile
Re: Change origin of rotation
« Reply #1 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 to its bottom center. ;)

Tweezy

  • Newbie
  • *
  • Posts: 43
    • View Profile
Re: Change origin of rotation
« Reply #2 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

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Change origin of rotation
« Reply #3 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).
« Last Edit: June 29, 2014, 02:50:56 pm by Golden Eagle »
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Tweezy

  • Newbie
  • *
  • Posts: 43
    • View Profile
Re: Change origin of rotation
« Reply #4 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)

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Change origin of rotation
« Reply #5 on: June 29, 2014, 04:04:12 pm »
If you want the bottom, don't divide height by 2; that would give the centre.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Tweezy

  • Newbie
  • *
  • Posts: 43
    • View Profile
Re: Change origin of rotation
« Reply #6 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.

Tweezy

  • Newbie
  • *
  • Posts: 43
    • View Profile
Re: Change origin of rotation
« Reply #7 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

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Change origin of rotation
« Reply #8 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.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Tweezy

  • Newbie
  • *
  • Posts: 43
    • View Profile
Re: Change origin of rotation
« Reply #9 on: June 29, 2014, 04:30:50 pm »
Check OP eagle, edited it with the my code.

Tweezy

  • Newbie
  • *
  • Posts: 43
    • View Profile
Re: Change origin of rotation
« Reply #10 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.

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Change origin of rotation
« Reply #11 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 or, maybe, show us a video of what it's doing and tell us what it's not doing correctly?
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Tweezy

  • Newbie
  • *
  • Posts: 43
    • View Profile
Re: Change origin of rotation
« Reply #12 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

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Change origin of rotation
« Reply #13 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?
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Tweezy

  • Newbie
  • *
  • Posts: 43
    • View Profile
Re: Change origin of rotation
« Reply #14 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