Page 1 of 2

Artificial Stupidity

Posted: Sat May 28, 2011 8:33 am
by lotios611
I'm working on a remake of Pickin' Sticks, and am trying to get some AI so that you can have a second player that is controlled by the computer. My problem is, the second player just walks right past the gem, going down. For example, say the gem spawns at 200, 200 and the second player spawns at 0, 0. The second player walks down, which it is supposed to do, but then walks right past the gem, and continues walking down.

Here is my code:

Code: Select all

if ((int)player2Pos.x > (int)gemPos.x)
{
	player2Direction = UP;
	player2Animating = true;
	player2.Move(0, -2);
}
else if ((int)player2Pos.x < (int)gemPos.x)
{
	player2Direction = DOWN;
	player2Animating = true;
	player2.Move(0, 2);
}
else if ((int)player2Pos.y > (int)gemPos.y)
{
	player2Direction = LEFT;
	player2Animating = true;
	player2.Move(-2, 0);
}
else if ((int)player2Pos.y < (int)gemPos.y)
{
	player2Direction = RIGHT;
	player2Animating = true;
	player2.Move(2, 0);
}

Re: Artificial Stupidity

Posted: Sat May 28, 2011 12:26 pm
by dandymcgee
Try this. ;)

Code: Select all

if ((int)player2Pos.y > (int)gemPos.y)
{
   player2Direction = UP;
   player2Animating = true;
   player2.Move(0, -2);
}
else if ((int)player2Pos.y < (int)gemPos.y)
{
   player2Direction = DOWN;
   player2Animating = true;
   player2.Move(0, 2);
}
else if ((int)player2Pos.x > (int)gemPos.x)
{
   player2Direction = LEFT;
   player2Animating = true;
   player2.Move(-2, 0);
}
else if ((int)player2Pos.x < (int)gemPos.x)
{
   player2Direction = RIGHT;
   player2Animating = true;
   player2.Move(2, 0);
}

Re: Artificial Stupidity

Posted: Sat May 28, 2011 1:26 pm
by lotios611
It's still not working. Now it works when the gem is to the right or above the second player, but not when it is to the left of the player.

Edit: It turns out it works sometimes, but not other times...

Re: Artificial Stupidity

Posted: Sat May 28, 2011 5:22 pm
by dandymcgee
Well I just double checked my post, and it looks fine. Can you upload your project somewhere, or send it to me?

Edit: Also, trying changing this line (the second else if)

Code: Select all

else if ((int)player2Pos.x > (int)gemPos.x)
to

Code: Select all

if ((int)player2Pos.x > (int)gemPos.x)
and let me know what happens.

Re: Artificial Stupidity

Posted: Sat May 28, 2011 6:15 pm
by lotios611
That change made the second player travel diagonally. My project is all in main.cpp, which I've uploaded to pastebin here.

Re: Artificial Stupidity

Posted: Sat May 28, 2011 7:01 pm
by dandymcgee
lotios611 wrote:That change made the second player travel diagonally.
Toward the gem?

I don't have SFML, so I can't compile that. I dunno it still looks fine to me though.

Re: Artificial Stupidity

Posted: Sun May 29, 2011 7:20 am
by lotios611
dandymcgee wrote:
lotios611 wrote:That change made the second player travel diagonally.
Toward the gem?
Yes, but I want the second player to travel in straight lines to the gem.

Re: Artificial Stupidity

Posted: Sun May 29, 2011 9:23 am
by dandymcgee
Assuming the x,y coords correspond to the top left corner of both the player and the gem, and assuming the width and height of both objects is greater than 5, try this and let me know what happens.

Code: Select all

if ((int)player2Pos.x > (int)gemPos.x + 5)
{
   player2Direction = UP;
   player2Animating = true;
   player2.Move(0, -2);
}
else if ((int)player2Pos.x + 5 < (int)gemPos.x)
{
   player2Direction = DOWN;
   player2Animating = true;
   player2.Move(0, 2);
}
else if ((int)player2Pos.y > (int)gemPos.y + 5)
{
   player2Direction = LEFT;
   player2Animating = true;
   player2.Move(-2, 0);
}
else if ((int)player2Pos.y + 5 < (int)gemPos.y)
{
   player2Direction = RIGHT;
   player2Animating = true;
   player2.Move(2, 0);
}

Re: Artificial Stupidity

Posted: Sun May 29, 2011 4:10 pm
by lotios611
That causes the player to walk past the gem. The x and y coords do correspond to the top left corner of both, and the width and height of both objects in greater than 5.

Re: Artificial Stupidity

Posted: Sun May 29, 2011 5:00 pm
by dandymcgee
lotios611 wrote:That causes the player to walk past the gem. The x and y coords do correspond to the top left corner of both, and the width and height of both objects in greater than 5.
Have you tried stepping through with the debugger and checking the values as the player goes past the gem?

Re: Artificial Stupidity

Posted: Sun Jun 19, 2011 5:44 pm
by lotios611
Alright, I've fixed it so that the AI no longer walks past the gem. However, it now gets stuck when the AI is to the left of the gem.

Here's my code:

Code: Select all

if ((int)player2Pos.x < (int)gemPos.x)
{
    player2Direction = RIGHT;
    player2Animating = true;
    player2.Move(3, 0);
}
else if ((int)player2Pos.x > 
           (int)gemPos.x)
{
    player2Direction = LEFT;
    player2Animating = true;
    player2.Move(-3, 0);
}
else if ((int)player2Pos.y < 
           (int)gemPos.y)
{
    player2Direction = UP;
    player2Animating = true;
    player2.Move(0, 3);
}
else if ((int)player2Pos.y > 
           (int)gemPos.y)
{
    player2Direction = DOWN;
    player2Animating = true;
    player2.Move(0, -3);
}
Edit: It also gets stuck when the AI is on top of the gem.

Re: Artificial Stupidity

Posted: Tue Jun 21, 2011 4:48 am
by EccentricDuck
lotios611 wrote:Alright, I've fixed it so that the AI no longer walks past the gem. However, it now gets stuck when the AI is to the left of the gem.
What was the fix to the initial problem?

My suggestion would be to print the value of player2Pos.x and gemPos.x side by side so you can see if the values are as they should be. If that doesn't work, I'd set a breakpoint before and inside the if statement to see if it's being properly executed.
lotios611 wrote:Edit: It also gets stuck when the AI is on top of the gem.
Do you store a reference to the gem and is that reference updated when the AI gets to the gem?

Re: Artificial Stupidity

Posted: Tue Jun 21, 2011 6:18 am
by lotios611
I tried outputting the player's and the gem's coordinates, and it turn out it happens when the second player's x coord is less than two less than the gem's x coords.

Re: Artificial Stupidity

Posted: Tue Jun 21, 2011 8:35 pm
by EccentricDuck
I just had another thought - what's your collision distance with the gem? If you don't do any fancy checks along the movement vector to detect collisions every frame it should be, at the very minimum, half of the minimum movement distance per frame so that you don't skip over the object.

X - AI
O - Gem

X____O

__X__O

____XO

_____OX

____XO

I wonder if this might be happening ^

Re: Artificial Stupidity

Posted: Wed Jun 22, 2011 2:51 pm
by lotios611
That's kind of what's happening. The AI checks to see if it's x pos is less than the gem's, and if it is, the AI moves 2 pixels right. The problem is, if for example the AI's x is 100, and the gem's x is 101, then the AI will move 2 pixels right, causing it's x pos to be greater than the gem's x pos. On the next frame, the AI moves left 2 pixels, and so on.