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

Author Topic: Direction Working out  (Read 2617 times)

0 Members and 1 Guest are viewing this topic.

Jalfor

  • Jr. Member
  • **
  • Posts: 62
    • View Profile
Direction Working out
« on: August 30, 2011, 06:12:19 am »
I know...title is awful though I couldn't think of anything better.

I need some help trying to get this little thing for my program working. There are a number of problems I am having. Firstly, I want the center to actually stay in the center. Secondly, I need the thing that will actually work out the new position of the thing after it has gone in a certain direction a certain amount. Before you start flaming me for not being able to work out a formula properly when I don't really even understand what half the commands are doing  exactly (Could you please tell me?) and that I'm a self taught 13 year old...

Anyway, here it is:

Code: [Select]
#include <SFML\Graphics.hpp>
using namespace sf;
RenderWindow App(VideoMode(800,600), "Space Game");

int main()
{
sf::Image Image;
Image.LoadFromFile("./arrow-upload-icone-9348-48.png");
Sprite SpaceShip(Image);
SpaceShip.SetCenter(110, 120);
sf::Vector2f shipPosition;
shipPosition.x = 110;
shipPosition.y = 120;
while (App.IsOpened())
{
shipPosition = SpaceShip.GetPosition();
float shipRotation = SpaceShip.GetRotation();
shipPosition.x += std::sin(shipRotation);
shipPosition.y -= std::cos(shipRotation);
App.Clear();
App.Draw(SpaceShip);
App.Display();
}
return EXIT_SUCCESS;
}


Thanks
If a picture tells 1000 words, then a YouTube video clearly tells 1000000 making a YouTube video obviously a larger work than the Lord Of The Rings, or some other massive novel. Thus are the laws of YouTube videos.

Disch

  • Full Member
  • ***
  • Posts: 220
    • View Profile
Direction Working out
« Reply #1 on: August 30, 2011, 07:04:20 am »
Can you be more descriptive?

The center of the thing not being the center of the other thing?  I can't follow you.  What are the things, and what are they doing that you want to change?

thePyro_13

  • Full Member
  • ***
  • Posts: 156
    • View Profile
Direction Working out
« Reply #2 on: August 30, 2011, 08:44:04 am »
Relax, no one's going to flame you.

It just looks like you don't understand how SetCenter() works.

It controls the centre of your sprite(and only that sprite), by default it points to the top left corner of your sprite.

You can set it to (imageswidth /2, imagehight/2) to make it easier to wrap your head around moving your sprite. This way all you positions can be based on the centre of your ship rather than the top left corner.

To actually move your sprite use SpaceShip.SetPosition(x,y) or SpaceShip.SetPosition(shipPosition) to pass in the vector you've already created.

Tell us which commands are still confusing you and what you want your sprite to do and we can try to help.

Jalfor

  • Jr. Member
  • **
  • Posts: 62
    • View Profile
Direction Working out
« Reply #3 on: August 30, 2011, 09:23:08 am »
Ok, which commands are confusing me:

The center thing, how does the *.GetPosition affect it? What I Really need to know, is how to get that SpaceShip Rotating around it's center, which means I guess making the center according to the computer the same as the one according to me...

Secondly, *.GetRotation; What is the number it gives you, how many degrees  is it in what direction away from what?

Finally, if someone could give me the formula for getting the new position of the ship after it has moved a certain amount in a certain direction, or at least point me in the right direction? I've only just started trigonometry at school so this is a little beyond me...
If a picture tells 1000 words, then a YouTube video clearly tells 1000000 making a YouTube video obviously a larger work than the Lord Of The Rings, or some other massive novel. Thus are the laws of YouTube videos.

thePyro_13

  • Full Member
  • ***
  • Posts: 156
    • View Profile
Direction Working out
« Reply #4 on: August 30, 2011, 10:22:50 am »
Get position doesn't effect the centre. The centre controls which point the sprite will rotate around. So for your situation you want it the be the centre of the image. For a 32x32px image you would want to the centre to be (16,16).

Your sprites position is counted from the top left of the screen(coord (0,0)) towards the bottom right of your screen. The higher the first number the closer you are to the right hand side of the screen, the higher the second number,  the closer you are to the bottom of the screen.

Get rotation returns a number indicating the degrees of your current rotation. if it returns 0 then the sprite is facing the same way it was when it loaded, if it returns 180 then it is upside down. 360 is back the right way up again, however you've gone the whole way round. This is all stuff you'll end up learning in school at some point. You can search google for more info specific to this, or ask your maths teacher(they'll be thrilled that you sound like you're interested in maths)

For a simple asteroid-type game all you want to do for now is call GetRotation(), increase or decrease the returned value by a little bit and then SetRotation, the altered value to give it back to the sprite.


GetPosition and SetPosition do all this for you. You can Move() to move by a small around, or SetPosition to 'teleport' to a specific location. GetPosition will always return the location where your sprite is, no matter which method you use to move it. If you ever what to know where you are,  call GetPostion().

Note that in your earlier code, you never actually gave a new position to the sprite. You calculated it, and then drew the sprite again without telling it that you wanted it to be.

You need to call SpaceShip.SetPosition(shipPosition) before you draw it.

Jalfor

  • Jr. Member
  • **
  • Posts: 62
    • View Profile
Direction Working out
« Reply #5 on: August 30, 2011, 11:10:10 am »
Firstly, I do know what degrees and stuff are, after all, it's a bit hard to do trigonometry without them, I was just wondering how many degrees off what it is. Does it go clockwise or anticlockwise btw.

Ok, so I've been trying to fix this thing up, though I'm having some issues; the position thing doesn't seem to be working and I've looked over it a million times, and I can't work out what I did wrong. The other issue, is that when I do the scale command which I have commented out it breaks the picture. Anyway, here is the code:

Code: [Select]
#include <SFML\Graphics.hpp>
using namespace sf;
RenderWindow App(VideoMode(800,600), "Space Game");

int main()
{
sf::Image Image;
Image.LoadFromFile("./arrow-upload-icone-9348-48.png");
Sprite SpaceShip(Image);
//SpaceShip.SetScale(32, 32);
Vector2f shipPosition;
shipPosition.x = 110;
shipPosition.y = 120;
SpaceShip.SetPosition(shipPosition);
while (App.IsOpened())
{
SpaceShip.SetCenter(SpaceShip.GetPosition().x + 16, SpaceShip.GetPosition().y + 16);
shipPosition = SpaceShip.GetPosition();

const Input & Input = App.GetInput();
bool left  = Input.IsKeyDown(Key::Left);
bool right = Input.IsKeyDown(Key::Right);
bool up    = Input.IsKeyDown(Key::Up);
bool down  = Input.IsKeyDown(Key::Down);

Event Event;
while (App.GetEvent(Event));

if (left)
SpaceShip.SetRotation(SpaceShip.GetRotation() + 1);
if (right)
SpaceShip.SetRotation(SpaceShip.GetRotation() - 1);
if (up)
; //for now
SpaceShip.SetPosition(shipPosition);
App.Clear();
App.Draw(SpaceShip);
App.Display();
}
return EXIT_SUCCESS;
}


[/code]
If a picture tells 1000 words, then a YouTube video clearly tells 1000000 making a YouTube video obviously a larger work than the Lord Of The Rings, or some other massive novel. Thus are the laws of YouTube videos.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Direction Working out
« Reply #6 on: August 30, 2011, 11:19:23 am »
Quote
SpaceShip.SetScale(32, 32);

A scale is not a size in pixels, it's a factor. Here you're multiplying the original image size by 32 :)

Quote
Firstly, I do know what degrees and stuff are, after all, it's a bit hard to do trigonometry without them, I was just wondering how many degrees off what it is. Does it go clockwise or anticlockwise btw.

Rotations are clockwise in SFML 2, and anticlockwise in SFML 1.6.
The initial rotation of the sprite is 0 degrees, so if GetRotation returns 45 it means that the sprite is rotated by 45 degrees compared to its initial pose.

I don't know exactly what your problem is about position and center (I didn't read the full thread, sorry), but remember that SetCenter doesn't only change the center of rotation, it also changes the center of scaling and translation. It's the "hot spot", "reference point" of the sprite, it's just a way to have something else than the default top-left corner.
so when you call SetPosition(x, y), it's the center which will be moved to (x, y), not the top-left corner.
Laurent Gomila - SFML developer