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

Author Topic: trying to make enemy walk in a specific direction  (Read 3704 times)

0 Members and 1 Guest are viewing this topic.

Fantasy

  • Newbie
  • *
  • Posts: 47
    • View Profile
    • Email
trying to make enemy walk in a specific direction
« on: October 31, 2011, 02:22:56 pm »
Hello
i'm trying to make an image to move in a specific direction. for example i want it to first move right, if it reach 50 pixel it stops and then move down, if it reach 50 pixel again it moves left and so on. but the problem is when it try to move left it can't, it just start to shake and not move.

here is the code:
Code: [Select]

#include <SFML\Graphics.hpp>
#include <iostream>

class mov
{
public:
mov();
~mov();
void drawmap(sf::RenderWindow &window);

private:
sf::Image grassimage, dirtimage;
sf::Sprite grasssprite, dirstsprite;
};



Code: [Select]

#include "mov.h"


mov::mov()
{
dirtimage.LoadFromFile("dirt.png");
dirstsprite.SetPosition(0,0);
}


mov::~mov()
{
}

void mov::drawmap(sf::RenderWindow &window)
{
float ElapsedTime = window.GetFrameTime();
dirstsprite.SetImage(dirtimage);

int posX = dirstsprite.GetPosition().x;
int posY = dirstsprite.GetPosition().y;

int Speed = 8;

int Up = -10;
int Down = 10;
int Right = 10;
int Left = -10;

dirstsprite.Move(Right * ElapsedTime * Speed, 0);

if(posX >=50)
dirstsprite.Move( -(Right * ElapsedTime * Speed), Down * ElapsedTime * Speed);

if(posY >=50)
dirstsprite.Move( Left * ElapsedTime * Speed, -(Down * ElapsedTime * Speed));

window.Draw(dirstsprite);

}




Code: [Select]
#include <SFML\Graphics.hpp>
#include "mov.h"

int main()
{
sf::RenderWindow window(sf::VideoMode(800,600,32),"AI Follow Grass Road");

mov SM;

while(window.IsOpened())
{
window.SetFramerateLimit(60);

sf::Event Event;
while(window.GetEvent(Event))
{

}

window.Clear();
SM.drawmap(window);
window.Display();
}
return 0;
}

slotdev

  • Sr. Member
  • ****
  • Posts: 385
    • View Profile
trying to make enemy walk in a specific direction
« Reply #1 on: October 31, 2011, 02:52:12 pm »
Have you run this under a debugger?
What are the values being passed to Move ?
SFML 2.1

Fantasy

  • Newbie
  • *
  • Posts: 47
    • View Profile
    • Email
trying to make enemy walk in a specific direction
« Reply #2 on: October 31, 2011, 02:53:34 pm »
Quote from: "slotdev"
Have you run this under a debugger?
What are the values being passed to Move ?


yes and
values are :
Code: [Select]

int Speed = 8;

int Up = -10;
int Down = 10;
int Right = 10;
int Left = -10;

dirstsprite.Move(Right * ElapsedTime * Speed, 0);

if(posX >=50)
dirstsprite.Move( -(Right * ElapsedTime * Speed), Down * ElapsedTime * Speed);

if(posY >=50)
dirstsprite.Move( (Left * 2 ), -(Down * ElapsedTime * Speed));
[/code]

slotdev

  • Sr. Member
  • ****
  • Posts: 385
    • View Profile
trying to make enemy walk in a specific direction
« Reply #3 on: October 31, 2011, 03:45:44 pm »
No, I mean, what values from those calculations/algorithms are being passed to Move?

Are they what you expect them to be?
SFML 2.1

Fantasy

  • Newbie
  • *
  • Posts: 47
    • View Profile
    • Email
trying to make enemy walk in a specific direction
« Reply #4 on: October 31, 2011, 03:59:10 pm »
Quote from: "slotdev"
No, I mean, what values from those calculations/algorithms are being passed to Move?

Are they what you expect them to be?


i don't know how to answer you first question but no they are not doing what they should do.

the image should move 50 pixel on the x axis (right) so the coordinate of the image should be (50,0) then it should move 50 pixel on the y axis (down) so the coordinate of the image should be (50,50) and then it should move -50 pixel on the x axis (left) so the coordinate of the image should be (0,50).

direction that should be
1. ------> (right)
2._____| (down)
_______| (down)
_______v (down)
3. <------ (left)


but instead this is happening
1. ------> (right)
2._____| (down)
_______| (down)
_______v (down)
3.___<-> (shake left and right)


but what happening is the image move right (which is correct) the down (which is correct) then it doesn't move left, it just start to shake right a lift and it doesn't go lift.

Fantasy

  • Newbie
  • *
  • Posts: 47
    • View Profile
    • Email
trying to make enemy walk in a specific direction
« Reply #5 on: October 31, 2011, 05:51:15 pm »
ok i finally solved it but i think there should be a better way cuz the way i did it is very very confusing.

now the sprite does the following in order:-
1- go right
2- when rech 50 pixel on x axis go down
3- when rech 50 pixel on y axis go left
4- when rech 50 pixel on y and 0 pixel on the y axis go up
5- do the same in a loop.

code:-
Code: [Select]

#include "static_map.h"


static_map::static_map()
{
dirtimage.LoadFromFile("dirt.png");
dirstsprite.SetPosition(0,0);
Stop1=false;
Stop2=false;
Stop3=false;
}


static_map::~static_map()
{
}

void static_map::drawmap(sf::RenderWindow &window)
{
float ElapsedTime = window.GetFrameTime();
dirstsprite.SetImage(dirtimage);

int posX = dirstsprite.GetPosition().x;
int posY = dirstsprite.GetPosition().y;

int Speed = 3;

int Up = -10;
int Down = 10;
int Right = 10;
int Left = -10;



if(posX <= 50 && Stop1==false)
dirstsprite.Move(Right * ElapsedTime * Speed, 0); // go right


if(posX >=50)
{
dirstsprite.Move(-(Right * ElapsedTime * Speed), 0); // stop going right

dirstsprite.Move( 0, Down * ElapsedTime * Speed); //go down
}

if(posY >=50)
{
dirstsprite.Move( 0, -(Down * ElapsedTime * Speed)); // stop going down
Stop1=true;
}

if(Stop1 == true)
dirstsprite.Move( Left * ElapsedTime * Speed, 0); //go left

if(posX == 0 && Stop1 == true)
{
dirstsprite.Move( -(Left * ElapsedTime * Speed), 0); //stop go left
Stop2 = true;
}

if(posX == 0 && Stop2==true)
dirstsprite.Move( 0, Up * ElapsedTime * Speed); // go Up

if(posX == 0 && posY == 0 )
{
dirstsprite.Move( 0, -(Up * ElapsedTime * Speed)); // stop go Up
Stop1 = false;
}


window.Draw(dirstsprite);

}

sbroadfoot90

  • Jr. Member
  • **
  • Posts: 77
    • View Profile
trying to make enemy walk in a specific direction
« Reply #6 on: November 02, 2011, 12:11:21 am »
There are a lot of posts like this in the graphics section that really don't have anything to do with graphics. It has to do with people not being able to write sufficient control statements to get the behaviour they expect. What you need to do is step through your code line by line and work out why it's not working.

Basically on each frame you tell it to

Move right,
if you're far enough right, move down and left
if you're far enough down, move left and up

this is a pretty bad way of doing it if you ask me. What you should do is include a private member that keeps track of the state the AI is in, so it would be something like

Code: [Select]
class mov {
private:
   state myState
}


where state is an enum

Code: [Select]
enum state {
    RIGHT, DOWN, LEFT, UP
};


this way, you can code your ai with a switch statement



Code: [Select]
switch myState
case RIGHT

   move right
   if you are far enough right, change myState to DOWN
   break

case DOWN

   move DOWN
   if you are far enough down, change myState to LEFT
   break
etc