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

Author Topic: Sprite boundaries  (Read 5388 times)

0 Members and 1 Guest are viewing this topic.

vro

  • Newbie
  • *
  • Posts: 44
    • View Profile
Sprite boundaries
« on: July 24, 2011, 05:41:25 am »
Hi everyone, I am having trouble getting boundaries to work in a simple game.

It's easy to do if my sprite only moves up, down, left, and right, because all I would have to do in that case is put in an if statement blocking the movement if sprite position was less or greater than the size of the window, etc.

What's making it hard for me to figure out is that my sprite rotates and can move in any direction.

Code: [Select]

if (App.GetInput().IsKeyDown(sf::Key::Up))
Ship.Move(-sin(Ship.GetRotation() * (3.14159265 / 180)) * ShipSpeed * App.GetFrameTime(), -cos(Ship.GetRotation() * (3.14159265 / 180)) * ShipSpeed * App.GetFrameTime());

if (App.GetInput().IsKeyDown(sf::Key::Down))
Ship.Move(sin(Ship.GetRotation() * (3.14159265 / 180)) * ShipSpeed * App.GetFrameTime(), cos(Ship.GetRotation() * (3.14159265 / 180)) * ShipSpeed * App.GetFrameTime());

if (App.GetInput().IsKeyDown(sf::Key::Left))
Ship.Rotate(300 * App.GetFrameTime());

if (App.GetInput().IsKeyDown(sf::Key::Right))
Ship.Rotate(-300 * App.GetFrameTime());


any hints? suggestions?

Disch

  • Full Member
  • ***
  • Posts: 220
    • View Profile
Sprite boundaries
« Reply #1 on: July 24, 2011, 06:27:18 am »
What difference does it make whether or not the sprite is rotating?

It still has an x/y coord, and that x/y coord can just as easily be checked to see if it's within the window bounds.

vro

  • Newbie
  • *
  • Posts: 44
    • View Profile
Sprite boundaries
« Reply #2 on: July 24, 2011, 06:29:54 am »
IDK maybe I'm tired.

I tried an if statement before the above code, that checked the x and y coords.

It didn't work because if triggered, it would stop the sprite from moving, but completely, so i couldn't turn around and go the other direction.

I am probably just tired but I can't wrap my head around this right now lol

Disch

  • Full Member
  • ***
  • Posts: 220
    • View Profile
Sprite boundaries
« Reply #3 on: July 24, 2011, 07:03:19 am »
If you want to check to see if the move is valid before you move, then you shouldn't call move directly like you are.

You should do something like this:

Code: [Select]

sf::Vector2f move( x_to_move, y_to_move );

if(move.x < 0) // moving left
{
  // check to make sure they're not moving off the left bound
}
else if(move.x > 0) // moving right
{
  // check to make sure they're not moving off the right bound
}

// do the same for y

if( move_is_within_bounds )
  Ship.Move(move);

vro

  • Newbie
  • *
  • Posts: 44
    • View Profile
Sprite boundaries
« Reply #4 on: July 24, 2011, 08:11:49 am »
thank you for the suggestions. I couldn't get that to work for some reason (not your fault of course), but I ended up getting the bounds to work by checking the sprite's position, and if it was on the edge of the screen, I would just change the x or y direction to line up with whichever edge of the screen the sprite was up against.

I don't know if that's a bad way to do it, but it's working really well.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Sprite boundaries
« Reply #5 on: July 24, 2011, 10:55:53 am »
You can find out the sf::Sprite corner positions as described here:
Quote from: "Laurent"
The points in local coordinates are:
- (0, 0)
- (0, sprite.GetSubRect().GetHeight())
- (sprite.GetSubRect().GetWidth(), 0)
- (sprite.GetSubRect().GetWidth(), sprite.GetSubRect().GetHeight())

The points in global coordinates are:
- sprite.TransformToGlobal(px)
where px is one of the local points.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Contadotempo

  • Full Member
  • ***
  • Posts: 167
  • Firelink Shrine
    • View Profile
Sprite boundaries
« Reply #6 on: July 25, 2011, 05:18:56 pm »
Probably a silly question but what are the differences between local and global coordinates of a sprite?

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Sprite boundaries
« Reply #7 on: July 25, 2011, 05:38:48 pm »
The origin of the global coordinate system is the left-upper corner of the window (if no custom view is used), so the global sprite coordinates are relative to that point. You usually access them with GetPosition() and SetPosition().

The local coordinates are relative to the sprite's left-upper corner. They don't change if the sprite is transformed.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Contadotempo

  • Full Member
  • ***
  • Posts: 167
  • Firelink Shrine
    • View Profile
Sprite boundaries
« Reply #8 on: July 26, 2011, 02:59:40 pm »
Understood, Thank you!

 

anything