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

Author Topic: Sprite following Mouse && Mouse.Pos == Sprite.Pos ?  (Read 3287 times)

0 Members and 1 Guest are viewing this topic.

Falke88

  • Jr. Member
  • **
  • Posts: 74
    • View Profile
Sprite following Mouse && Mouse.Pos == Sprite.Pos ?
« on: August 10, 2014, 04:20:41 pm »
Hey guys,

Just came along another tricky thingie again. I'm sure the solution is quite easy - once you heared about it ;)

I want to let my mouse first "spawn" direcly onto my sprite. There I wanted to add the sprites position to the mouse' new SetPosition(new Vector2i) method.

sadly anyhow these two kinds of positions dont match. I already read in the forums that there needs to be some kind of transformation from the local coords and the global screen coords.
Thought the 2nd Parameter for the SetPosition Method of Mouse would help here (Window toRelation). made it way worser.

Since this won't work my sprite/mouse movement morph won't work anyway. Actually I managed to follow the sprite by my mouse motions - but you know - it looks crappy if the sprite vanishes anywhere behind the window nether ;)

heres a screeny:



The red cursor on the topleft position...thats where my cursor actually was while taking the screeny

and some code snippets of my tryout:


float x = sprite.Position.X;
float y = sprite.Position.Y;
Mouse.SetPosition(new Vector2i((int)x, (int)y));

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Sprite following Mouse && Mouse.Pos == Sprite.Pos ?
« Reply #1 on: August 10, 2014, 05:03:35 pm »
Ok... and what's your problem exactly?
Laurent Gomila - SFML developer

Ztormi

  • Jr. Member
  • **
  • Posts: 71
  • Web developer by day. Game developer by night.
    • View Profile
Re: Sprite following Mouse && Mouse.Pos == Sprite.Pos ?
« Reply #2 on: August 10, 2014, 05:15:46 pm »
Wouldn't the cursor appear on the window if your using
Mouse.SetPosition(x,y, window)
?

Anyways you want to take your view into account aswell incase you someday want to move the view.

float x = sprite.Position.X - window.GetView().Center.X + window.GetView().Size.X / 2;
float y = sprite.Position.Y - window.GetView().Center.Y + window.GetView().Size.Y / 2;
Mouse.SetPosition(new Vector2i((int)x, (int)y), window);

Plus, remember to set origin for your sprite.
« Last Edit: August 10, 2014, 05:19:23 pm by Ztormi »

Falke88

  • Jr. Member
  • **
  • Posts: 74
    • View Profile
Re: Sprite following Mouse && Mouse.Pos == Sprite.Pos ?
« Reply #3 on: August 10, 2014, 05:16:07 pm »
Ha not obvious? ^^

Alright - the Problem is...I want my cursor exactly at the center of the sprite (the top-left center actually since sprites do center there not in their own visual middle)

But since yet the cursor only stays on the top left of the desktopscree (or windowscreen if I use the Window toRelative Parameter aswell). I'm pretty sure that


this is the snippet for the sprite follow mouse. I think here is the mistake @ home ;)
sprite.Position = new Vector2f((float)Mouse.GetPosition().X, (float)Mouse.GetPosition().Y);

Falke88

  • Jr. Member
  • **
  • Posts: 74
    • View Profile
Re: Sprite following Mouse && Mouse.Pos == Sprite.Pos ?
« Reply #4 on: August 10, 2014, 05:20:58 pm »
Hey there!

Yea I already was taking care about the view thingie.

btw I found the mistake.

Since GetPositions has an overload to use Relation to the window as Parameter it now works :X sry guys for wasting time

 

anything