# G4MR

Member

5

103 Neutral

• Rank
Newbie
1. ## SFML sprite movement (think super meat boy's rocket levels)

Another version someone posted on /r/gamedev:   while(game_is_running) { handle_input_and_move_player(); //get vector vx = player.x - zombie.x; vy = player.y - zombie.y; //get length of vector (distance from zombie to player) length = sqrt(vx*vx + vy*vy); //check length is above the hit distance from player to zombie here //if it's below the distance then the zombie has eaten the player //length must be above zero or the normalisation will error (divide by zero) //normalise vx = vx/length; vy = vy/length; zombie.x += vx*zombie.speed*frame_time; zombie.y += vy*zombie.speed*frame_time; //draw everything draw_everything(); }   Which also works.
2. ## SFML sprite movement (think super meat boy's rocket levels)

This worked :D
3. ## SFML sprite movement (think super meat boy's rocket levels)

So I attempted to do what you said and I had to make the speed the same for the player and the zombie.   Problem/Question 1: It seems I can't make both units on the screen different velocities (eg.: player1 = zombie with a speed of 2 and player2 = player with a speed of 3 [slightly faster]), if I do this and I go in a diagonal direction my player1 sprite will go in weird circular patterns unless I stop moving.   Problem/Question 2: It seems that if I allow diagonal movement (holding W and D or A & S, etc...) it will do this small curl up until I stop moving.  What's the best way to approach this problem?     Problem/Question 3: There's also the issue when holding just down or right (S or D) and the sprite will just sit on the X or Y axis until I stop moving the unit on the screen and then proceed to move towards my unit.  How do I sort this? Example: Try what I'm talking about: http://morezombies.net/Zoms.zip (Virus Total Link: https://www.virustotal.com/url/1bb28babfa7e3049adc0f11e3f85c4a063728b799c20d4968e1c6c25921fc474/analysis/1357552685/) Source:   #include "GameApp.h" void GameApp::Setup() { //setup the game window window.create(VideoMode(width, height), "Zoms"); //set framerate window.setFramerateLimit(60); player1.setSize(10, 20); player1.setPos(10, 10); player2.setSize(10,10); //lets say theg ames running now running = true; } void GameApp::Run() { //Setup game Setup(); //used to get the current angle of the line between the two points //then we can properly move the sprite to the player double angle, x, y, newx, newy; //sprite speed int speed = 2; //are the wasd buttons pressed? bool mw = false; bool ma = false; bool ms = false; bool md = false; //setup the player2 location int ranx = 300; int rany = 300; player2.setPos(ranx, rany); float timeprev, timecurr; while(window.isOpen()) { //loop through the active events (keyboard, mouse, etc...) while(window.pollEvent(event)) { if(event.type == Event::Closed) { window.close(); } if(event.type == Event::KeyPressed) { switch(event.key.code) { case Keyboard::W: mw = true; break; case Keyboard::A: ma = true; break; case Keyboard::S: ms = true; break; case Keyboard::D: md = true; break; } } if(event.type == Event::KeyReleased) { switch(event.key.code) { case Keyboard::W: mw = false; break; case Keyboard::A: ma = false; break; case Keyboard::S: ms = false; break; case Keyboard::D: md = false; break; } } } //did we move the player? if(mw) { rany -= speed; } if(ma) { ranx -= speed; } if(ms) { rany += speed; } if(md) { ranx += speed; } //get the angle of the line between both player locations (zombie/player) angle = (atan2(rany - player1.sprite.getPosition().y, ranx - player1.sprite.getPosition().x) * 180) / M_PI; //steps between movement x = cos(angle) * speed; y = sin(angle) * speed; newx = player1.sprite.getPosition().x + x; newy = player1.sprite.getPosition().y + y; //move the sprites on the screen player1.sprite.setPosition(Vector2f(newx, newy)); player2.sprite.setPosition(Vector2f(ranx, rany)); window.clear(); window.draw(player1.sprite); window.draw(player2.sprite); //update the screen window.display(); } }     Pastie link: http://pastie.org/private/dfjwphmllmhnqdh0htyhdg
4. ## SFML sprite movement (think super meat boy's rocket levels)

Thank you, I will try this ^_^
5. ## SFML sprite movement (think super meat boy's rocket levels)

I'm a complete newbie when it comes to game development and I'm catching up on my c++ (would consider myself below average in c++ programming), so I've been messing around with SFML 2.0 (which is amazing compared to my experience with SDL) and I've been playing with some code which takes two rects -> draws them on the screen and one rect is controlled using WASD and the other rect attempts to go in the direction of the other rect's current location on the screen. Right now I've figured out with a little googling how to get the angle of a line of two points and then send the current rect to the other one eg.: angle = (atan2(player.y - zombie.y, player.x - zombie.x) * 180) / M_PI; x = cos(angle); y = sin(angle); Then I could properly send the zombie to the player based on it's current position. The Problem: Every time I move my player around the window, the zombie stops moving in place (im assuming because the angle is constantly being calculated in the game loop) until I stop moving my player on the screen then it proceeds to move towards the player again. How do I make it so the zombie rect smoothly continues moving towards the player on the screen regardless of where I move the player on my current screen as it's moving on the screen?     I guess the best way to think of it is my code works when the player sprite isn't moving, but when it's moving it'll stop moving completely.  What I want to do is basically pull off what super meat boy did with their rockets and it'll keep chasing you regardless of the position: http://www.gamespot.com/super-meat-boy/videos/super-meat-boy-unforgiving-rocket-level-gameplay-movie-6282320/