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

Author Topic: Converting Coords and moving with mouse issue [SOLVED]  (Read 2166 times)

0 Members and 1 Guest are viewing this topic.

coldkillerlips

  • Newbie
  • *
  • Posts: 4
    • View Profile
Converting Coords and moving with mouse issue [SOLVED]
« on: February 06, 2012, 06:21:21 pm »
Ok so i'm totally stumped. I'm trying to move the character using the mouse on a right click. When i move right click it'll sometimes go to the correct position and stop, other times it'll go non-stop or stop at a weird offset. Can anyone tell me what i'm doing wrong?

Code: [Select]

void DynamicEntity::MoveEvent(sf::RenderWindow &Window, sf::View View)
{
if(sf::Mouse::IsButtonPressed(sf::Mouse::Right))
{
sf::Vector2f MousePos(Window.ConvertCoords(sf::Mouse::GetPosition(Window).x, sf::Mouse::GetPosition(Window).y, View));

this->Dest.x = (MousePos.x - this->GetSprite().GetPosition().x);
this->Dest.y = (MousePos.y - this->GetSprite().GetPosition().y);

this->NewLoc = normalize(Dest);

this->SetIsMoving(true);
}
}

void DynamicEntity::Move()
{
if(this->GetIsMoving() == true)
{
if(this->GetSprite().GetPosition().x <= (Dest.x - 5.f) || this->GetSprite().GetPosition().x >= (Dest.x + 5.f))
{
this->MoveSprite(NewLoc.x * movSpeed, 0);
}

if(this->GetSprite().GetPosition().y <= (Dest.y - 5.f) || this->GetSprite().GetPosition().y >= (Dest.y + 5.f))
{
this->MoveSprite(0, NewLoc.y * movSpeed);
}
}
else
{
SetDest(this->GetSprite().GetPosition());
this->NewLoc = this->GetSprite().GetPosition();
this->SetIsMoving(false);
}
}

RedIrony

  • Newbie
  • *
  • Posts: 23
    • View Profile
Converting Coords and moving with mouse issue [SOLVED]
« Reply #1 on: February 06, 2012, 07:28:37 pm »
Why do you have
Code: [Select]
this->Dest.x = (MousePos.x - this->GetSprite().GetPosition().x);
this->Dest.y = (MousePos.y - this->GetSprite().GetPosition().y);
?

It looks like you're creating a distance vector relative to the position of the sprite, but in your move function you're treating it like it was relative to the origin.

coldkillerlips

  • Newbie
  • *
  • Posts: 4
    • View Profile
Converting Coords and moving with mouse issue [SOLVED]
« Reply #2 on: February 06, 2012, 07:39:26 pm »
Quote from: "RedIrony"
Why do you have
Code: [Select]
this->Dest.x = (MousePos.x - this->GetSprite().GetPosition().x);
this->Dest.y = (MousePos.y - this->GetSprite().GetPosition().y);
?

It looks like you're creating a distance vector relative to the position of the sprite, but in your move function you're treating it like it was relative to the origin.


Thank you very much. That was the issue.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Converting Coords and moving with mouse issue [SOLVED]
« Reply #3 on: February 06, 2012, 07:50:25 pm »
By the way, why not
Code: [Select]
Dest = MousePos - GetSprite().GetPosition();instead of
Code: [Select]
this->Dest.x = (MousePos.x - this->GetSprite().GetPosition().x);
this->Dest.y = (MousePos.y - this->GetSprite().GetPosition().y);
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

coldkillerlips

  • Newbie
  • *
  • Posts: 4
    • View Profile
Converting Coords and moving with mouse issue [SOLVED]
« Reply #4 on: February 07, 2012, 06:41:15 am »
I'm not sure why I chose to do it separately. I didn't think it mattered aside from being shorter though.

 

anything