• Create Account

## SFML - Sprite position and movement

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

3 replies to this topic

### #1mypel16000  Members

14
Like
0Likes
Like

Posted 06 January 2013 - 05:11 PM

Hi, just a quick question. I am trying to make the player in a 2D game seen from above look at the mouse cursor as an aiming system. I have a pHd in maths, so I'm pretty clear how to use vectors and tan-1 to get a rotational angle, but the problem is that when I try to find my sprite's position by using sprite.getPosition() I only get 0 for X and for Y. the only problem I can think of is that SFML might be using a relative position to the start, but after starting it on 0,0 and movin it to the center of the screen, the problem persists. How can I get the sprite's position in my window so I can measure a vector. You could also explain how to find the vector in case someone in need reads this topic.

Oh, and one last thing, how can I do so my player can move diagonally when UP and RIGHT are pressed for example. I'm not keen on just having 4 direction movement. I have tried using && to check for both keys, but it doesnt work.

### #2BeerNutts  Members

4288
Like
1Likes
Like

Posted 06 January 2013 - 09:51 PM

This is a copy of my CheckInput() method from my Smash PC Game (detailed in my old Blog, linked in my Signature).  It will set the velocity of the player depending on the keys pressed (and it handles diagonal), as well as faces the player towards the mouse using SFML.  It uses atan2 to find it based on the location of player and mouse, not a vector.

Also, assume Utilities::KeyIsDown(), just checks a sf::Input::IsKeyDown() or IsMouseButtonDown()


</p><div>/******************************************************************************</div>
<div>*</div>
<div>* CheckInput() - Check for input and handle accordingly</div>
<div>*</div>
<div>******************************************************************************/</div>
<div>void SmashPcPlayer::CheckInput(void)</div>
<div>{</div>
<div>    const sf::Input& Input = mpApp->GetInput();</div>
<div>    cpFloat xForce = 0, yForce = 0;</div>
<div>    cpVect Vel;</div>
<div>    SmashPcBullet *pBullet = NULL;</div>
<div> </div>
<div>    // Set all forces to 0</div>
<div>    mpBody->f = cpvzero;</div>
<div> </div>
<div>    Vel = cpBodyGetVel(mpBody);</div>
<div> </div>
<div>    // We'll just assign velocities to a hardcoded value for now</div>
<div>    if (Utilities::KeyIsDown(Input, GAME_KEY_LEFT))</div>
<div>        xForce -= PLAYER_FORCE;</div>
<div>    else if (Vel.x < 0.01f)</div>
<div>        mpBody->v.x = 0;</div>
<div> </div>
<div>    if (Utilities::KeyIsDown(Input, GAME_KEY_RIGHT))</div>
<div>        xForce += PLAYER_FORCE;</div>
<div>    else if (Vel.x > 0.01f)</div>
<div>        mpBody->v.x = 0;</div>
<div> </div>
<div>    if (Utilities::KeyIsDown(Input, GAME_KEY_DOWN))</div>
<div>        yForce += PLAYER_FORCE;</div>
<div>    else if (Vel.y > 0.01f)</div>
<div>        mpBody->v.y = 0;</div>
<div> </div>
<div>    if (Utilities::KeyIsDown(Input, GAME_KEY_UP))</div>
<div>        yForce -= PLAYER_FORCE;</div>
<div>    else if (Vel.y < 0.01f)</div>
<div>        mpBody->v.y = 0;</div>
<div> </div>
<div>    mpBody->f = cpv(xForce, yForce);</div>
<div> </div>
<div>    // Check for fire</div>
<div>    if (Utilities::KeyIsDown(Input, GAME_KEY_FIRE))</div>
<div>    {</div>
<div>        pBullet = mpWeapon->FireBullet(mpBody, TRUE);</div>
<div>        if (pBullet)</div>
<div>        {</div>
<div>        }</div>
<div>    }</div>
<div>    else if (Utilities::KeyIsDown(Input, GAME_KEY_SPECIAL_FIRE))</div>
<div>    {</div>
<div>        // Handle later</div>
<div>    }</div>
<div> </div>
<div>    // Check for user choosing weapon (number pad)</div>
<div>    sf::Key::Code KeyDown = Utilities::CheckKeyRange(</div>
<div>                                Input, sf::Key::Num1, sf::Key::Num9);</div>
<div> </div>
<div>    if (KeyDown != sf::Key::Count)</div>
<div>    {</div>
<div>        std::string BulletToUse;</div>
<div>        mpWeapon->GetBulletName((U32)(KeyDown - sf::Key::Num1), BulletToUse);</div>
<div> </div>
<div>        mpWeapon->SelectBullet((U32)(KeyDown - sf::Key::Num1));</div>
<div>    }</div>
<div> </div>
<div>    // Finally, find location of Mouse and point player towards it</div>
<div>    cpFloat MouseX = (cpFloat)Input.GetMouseX();</div>
<div>    cpFloat MouseY = (cpFloat)Input.GetMouseY();</div>
<div> </div>
<div>    cpFloat Angle = atan2(MouseY - (cpFloat)(mpApp->GetHeight()/2),</div>
<div>                          MouseX - (cpFloat)(mpApp->GetWidth()/2));</div>
<div>    cpBodySetAngle(mpBody, Angle);</div>
<div> </div>
<div>    return;</div>
<div>}</div>
<div>

My Gamedev Journal: 2D Game Making, the Easy Way

---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)

### #3mypel16000  Members

14
Like
0Likes
Like

Posted 07 January 2013 - 12:59 PM

Good answer BeerNuts, I was expecting a simple concept, but by looking at your code, I can actually see how you have done the movement by having cummulative x and y forces.

Anyways, can anyone actually answer my first problem ( how to get the real position of the object relatively to the game window ) as the normal functions aren't doing the work

### #4Milcho  Members

1177
Like
0Likes
Like

Posted 07 January 2013 - 01:13 PM

Good answer BeerNuts, I was expecting a simple concept, but by looking at your code, I can actually see how you have done the movement by having cummulative x and y forces.

Anyways, can anyone actually answer my first problem ( how to get the real position of the object relatively to the game window ) as the normal functions aren't doing the work

sf::Spirte::getPosition returns the absolute position of the sprite in the world - not necessarily the window. The window displays a sf::View - which is normally exactly the size of the window.

Bottom line, sf::Sprite::getPosition should return the position. Perhaps if you post your code we could see what else could be affecting it. (in SFML2 there's an origin which can affect position)

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.