Artificial Stupidity

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

User avatar
lotios611
Chaos Rift Regular
Chaos Rift Regular
Posts: 160
Joined: Sun Jun 14, 2009 12:05 pm
Current Project: Game engine for the PC, PSP, and maybe more.
Favorite Gaming Platforms: Gameboy Micro
Programming Language of Choice: C++

Artificial Stupidity

Post 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);
}
"Why geeks like computers: unzip, strip, touch, finger, grep, mount, fsck, more, yes, fsck, fsck, fsck, umount, sleep." - Unknown
User avatar
dandymcgee
ES Beta Backer
ES Beta Backer
Posts: 4709
Joined: Tue Apr 29, 2008 3:24 pm
Current Project: https://github.com/dbechrd/RicoTech
Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
Programming Language of Choice: C
Location: San Francisco
Contact:

Re: Artificial Stupidity

Post 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);
}
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches! :twisted:
User avatar
lotios611
Chaos Rift Regular
Chaos Rift Regular
Posts: 160
Joined: Sun Jun 14, 2009 12:05 pm
Current Project: Game engine for the PC, PSP, and maybe more.
Favorite Gaming Platforms: Gameboy Micro
Programming Language of Choice: C++

Re: Artificial Stupidity

Post 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...
"Why geeks like computers: unzip, strip, touch, finger, grep, mount, fsck, more, yes, fsck, fsck, fsck, umount, sleep." - Unknown
User avatar
dandymcgee
ES Beta Backer
ES Beta Backer
Posts: 4709
Joined: Tue Apr 29, 2008 3:24 pm
Current Project: https://github.com/dbechrd/RicoTech
Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
Programming Language of Choice: C
Location: San Francisco
Contact:

Re: Artificial Stupidity

Post 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.
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches! :twisted:
User avatar
lotios611
Chaos Rift Regular
Chaos Rift Regular
Posts: 160
Joined: Sun Jun 14, 2009 12:05 pm
Current Project: Game engine for the PC, PSP, and maybe more.
Favorite Gaming Platforms: Gameboy Micro
Programming Language of Choice: C++

Re: Artificial Stupidity

Post by lotios611 »

That change made the second player travel diagonally. My project is all in main.cpp, which I've uploaded to pastebin here.
"Why geeks like computers: unzip, strip, touch, finger, grep, mount, fsck, more, yes, fsck, fsck, fsck, umount, sleep." - Unknown
User avatar
dandymcgee
ES Beta Backer
ES Beta Backer
Posts: 4709
Joined: Tue Apr 29, 2008 3:24 pm
Current Project: https://github.com/dbechrd/RicoTech
Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
Programming Language of Choice: C
Location: San Francisco
Contact:

Re: Artificial Stupidity

Post 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.
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches! :twisted:
User avatar
lotios611
Chaos Rift Regular
Chaos Rift Regular
Posts: 160
Joined: Sun Jun 14, 2009 12:05 pm
Current Project: Game engine for the PC, PSP, and maybe more.
Favorite Gaming Platforms: Gameboy Micro
Programming Language of Choice: C++

Re: Artificial Stupidity

Post 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.
"Why geeks like computers: unzip, strip, touch, finger, grep, mount, fsck, more, yes, fsck, fsck, fsck, umount, sleep." - Unknown
User avatar
dandymcgee
ES Beta Backer
ES Beta Backer
Posts: 4709
Joined: Tue Apr 29, 2008 3:24 pm
Current Project: https://github.com/dbechrd/RicoTech
Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
Programming Language of Choice: C
Location: San Francisco
Contact:

Re: Artificial Stupidity

Post 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);
}
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches! :twisted:
User avatar
lotios611
Chaos Rift Regular
Chaos Rift Regular
Posts: 160
Joined: Sun Jun 14, 2009 12:05 pm
Current Project: Game engine for the PC, PSP, and maybe more.
Favorite Gaming Platforms: Gameboy Micro
Programming Language of Choice: C++

Re: Artificial Stupidity

Post 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.
"Why geeks like computers: unzip, strip, touch, finger, grep, mount, fsck, more, yes, fsck, fsck, fsck, umount, sleep." - Unknown
User avatar
dandymcgee
ES Beta Backer
ES Beta Backer
Posts: 4709
Joined: Tue Apr 29, 2008 3:24 pm
Current Project: https://github.com/dbechrd/RicoTech
Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
Programming Language of Choice: C
Location: San Francisco
Contact:

Re: Artificial Stupidity

Post 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?
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches! :twisted:
User avatar
lotios611
Chaos Rift Regular
Chaos Rift Regular
Posts: 160
Joined: Sun Jun 14, 2009 12:05 pm
Current Project: Game engine for the PC, PSP, and maybe more.
Favorite Gaming Platforms: Gameboy Micro
Programming Language of Choice: C++

Re: Artificial Stupidity

Post 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.
"Why geeks like computers: unzip, strip, touch, finger, grep, mount, fsck, more, yes, fsck, fsck, fsck, umount, sleep." - Unknown
User avatar
EccentricDuck
Chaos Rift Junior
Chaos Rift Junior
Posts: 305
Joined: Sun Feb 21, 2010 11:18 pm
Current Project: Isometric "2.5D" Airship Game
Favorite Gaming Platforms: PS2, SNES, GBA, PC
Programming Language of Choice: C#, Python, JScript
Location: Edmonton, Alberta

Re: Artificial Stupidity

Post 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?
User avatar
lotios611
Chaos Rift Regular
Chaos Rift Regular
Posts: 160
Joined: Sun Jun 14, 2009 12:05 pm
Current Project: Game engine for the PC, PSP, and maybe more.
Favorite Gaming Platforms: Gameboy Micro
Programming Language of Choice: C++

Re: Artificial Stupidity

Post 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.
"Why geeks like computers: unzip, strip, touch, finger, grep, mount, fsck, more, yes, fsck, fsck, fsck, umount, sleep." - Unknown
User avatar
EccentricDuck
Chaos Rift Junior
Chaos Rift Junior
Posts: 305
Joined: Sun Feb 21, 2010 11:18 pm
Current Project: Isometric "2.5D" Airship Game
Favorite Gaming Platforms: PS2, SNES, GBA, PC
Programming Language of Choice: C#, Python, JScript
Location: Edmonton, Alberta

Re: Artificial Stupidity

Post 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 ^
User avatar
lotios611
Chaos Rift Regular
Chaos Rift Regular
Posts: 160
Joined: Sun Jun 14, 2009 12:05 pm
Current Project: Game engine for the PC, PSP, and maybe more.
Favorite Gaming Platforms: Gameboy Micro
Programming Language of Choice: C++

Re: Artificial Stupidity

Post 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.
"Why geeks like computers: unzip, strip, touch, finger, grep, mount, fsck, more, yes, fsck, fsck, fsck, umount, sleep." - Unknown
Post Reply