Sign in to follow this  
EvanBlack

Moving Sprite from one point to another.

Recommended Posts

Ok. So I am trying to move a sprite with SFML across the screen from one point to another point.



say I have a screen (0,0,100,100)

I have sprite at point (50, 50)


How can I move the sprite to position (75, 25) over time.


So what I came up with was this:

[source c++]

/*
Square is a structure that holds information about a specific area of pixels.

The variables used:

Square* oldSquare; t= top l= left (x,y)
Square* atSquare;
Square* newSquare;
bool isMoving;



*/


// Creates the function to move the game object.
void GameObject::move(float ElapsedTime)
{
//sets the last position to the current position.
this->oldSquare = this->atSquare;

if(this->isMoving = true)
{
if(this->atSquare->t != this->newSquare->t)
{
if(this->atSquare->t > this->newSquare->t)
{
std::cout << "Move by " << ((this->atSquare->t - this->newSquare->t)/2) * ElapsedTime << std::endl;
Sprite.Move(((this->atSquare->t - this->newSquare->t)/2) * ElapsedTime, 0);
}
if(this->atSquare->t < this->newSquare->t)
{
std::cout << "Move by " << ((this->atSquare->t - this->newSquare->t)/2) * ElapsedTime << std::endl;
Sprite.Move(0, ((this->atSquare->t - (this->atSquare->t - this->newSquare->t))/2) * ElapsedTime);
}
}
else{this->isMoving = false;}

if(this->atSquare->l != this->newSquare->l)
{
if(this->atSquare->l > this->newSquare->l)
{
std::cout << "Move by " << ((this->atSquare->l - this->newSquare->l)/2) * ElapsedTime << std::endl;
Sprite.Move(((this->atSquare->l - this->newSquare->l)/2) * ElapsedTime, 0);
}
if(this->atSquare->l < this->newSquare->l)
{
std::cout << "Move by " << ((this->atSquare->l - this->newSquare->l)/2) * ElapsedTime << std::endl;
Sprite.Move(0,((this->atSquare->l - (this->atSquare->l - this->newSquare->l))/2) * ElapsedTime);
}
}
else
{
this->isMoving = false;
}
}
}





The problem is that this doesn't work. It only makes the square go in one direction; in this case, straight down.

Share this post


Link to post
Share on other sites
There are a couple of bugs in your code... if this->isMoving = true is assigning 'true' to this->isMoving... you'll want to use == - or better, just leave the whole comparison against 'true' out - isMoving is a boolean itself already.

I'm not sure why you're using Square pointers... why allocate such small objects on the heap? Why complicate memory management and risk leaking stuff? Actually, this is probably the cause of your problem - both pointers end up pointing at the same Square instance.


Anyway, some pseudo-code. Velocity controls movement - setting velocity to (0, 0) stops movement:
class GameObject
{
Point2D position, velocity;

void move(float delta)
{
// The following code can be simplified by implementing the * operator for
// the Point2D class, but whatever:
position.x += velocity.x * delta;
position.y += velocity.y * delta;
};
};


If reliably physics are important to your game, you may want to read this article. Going all the way is probably overkill if you're just getting started - so just clamp the delta to a certain range, so a loading hiccup won't send all your objects flying through the walls. ;)

Share this post


Link to post
Share on other sites
Quote:
Original post by Captain P
There are a couple of bugs in your code... if this->isMoving = true is assigning 'true' to this->isMoving... you'll want to use == - or better, just leave the whole comparison against 'true' out - isMoving is a boolean itself already.

I'm not sure why you're using Square pointers... why allocate such small objects on the heap? Why complicate memory management and risk leaking stuff? Actually, this is probably the cause of your problem - both pointers end up pointing at the same Square instance.


Anyway, some pseudo-code. Velocity controls movement - setting velocity to (0, 0) stops movement:
*** Source Snippet Removed ***

If reliably physics are important to your game, you may want to read this article. Going all the way is probably overkill if you're just getting started - so just clamp the delta to a certain range, so a loading hiccup won't send all your objects flying through the walls. ;)


That's a great article!

Cheers,
Bach

Share this post


Link to post
Share on other sites
umm, what?

I will fix the pointers thank you, but I don't understand the code you used. How does that move it to a position?


I have newSquare pointer pointing to the square that was clicked, while I have oldSquare pointing to the square I was at, while I have atSquare pointing to the square I am currently on. Equaling 3 different points.

The squares are stored in a vector of squares which equal the entire map.


Say I have squares:

0,0; 0,1; 0,2;
1,0; 1,1; 1,2;
2,0; 2,1; 2,2;


Now my position is atSquare->t == 0, atSquare->l == 0;

I click on a square and it returns that square and then I get newSquare->t == 2, newSquare->l == 2;

So now I have 2 different points.

How do I move object atSquare 0 to newSquare 9

so I know I have to change the xposition and yposition of my sprite over time by a certain incremental amount... I may have answered my own question... and some help of you pseudo code I might be able to make something to understand whats happening. Thank you.

ok.. well what I thought would work didn't actually do the trick...

I have no idea but I am going to rework all my code for the function because I realize I messed up on it pretty bad..

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this