AI acting weird

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
blackout_alex
Prole
Posts: 9
Joined: Thu Apr 26, 2012 9:07 pm

AI acting weird

Post by blackout_alex »

I have been working on some AI, it doesn't do much but walk around and change direction every few seconds, but it seems to be acting weird. All of the AI end up near the bottom of the screen, even though it should be random. The random number assigned to "down" it 1. Is there a way that math.random(4) is selecting 1 much more frequently than other numbers? You can try it yourself:
random.love
(38.31 KiB) Downloaded 114 times
User avatar
dreadkillz
Party member
Posts: 223
Joined: Sun Mar 04, 2012 2:04 pm
Location: USA

Re: AI acting weird

Post by dreadkillz »

I got an error message for HardonCollider. You should repackage your .love with it?

EDIT: Nevermind got it working just removed the require line for HC
Last edited by dreadkillz on Tue Jun 19, 2012 8:24 pm, edited 1 time in total.
User avatar
OmarShehata
Party member
Posts: 259
Joined: Tue May 29, 2012 6:46 pm
Location: Egypt
Contact:

Re: AI acting weird

Post by OmarShehata »

Hmm, code seems to be correct.

If you're not happy with it currently you could always add an extra check so that if he's down, he's more likely to go up. And same for all directions.

Code: Select all

if AiIsGoingDown then
    local normalized = (ai.y - (screenHeight/2)) / (screenHeight/2) --Gets a value between 0 and 1. 0 means least down (in the center) and 1 is most down, bottom

if math.random() <= normalized then --As the "normalized" gets bigger, the more likely this check will be true
  ChooseAnotherDirection()
end
    
end
Where it would keep running "ChooseAnotherDirection()" until the AI is no longer going down.

Or a much simpler way, just not make it go towards a certain direction when it's past a certain limit. Etc... there's a lot of way you can experiment to tweak the AI's performance.
User avatar
dreadkillz
Party member
Posts: 223
Joined: Sun Mar 04, 2012 2:04 pm
Location: USA

Re: AI acting weird

Post by dreadkillz »

I think I figured out what's wrong with your code. You should do this:

Code: Select all

math.random(0,3) -- choose between [0,3]
You have math.random(4) which chooses a number between 1 and 4 so your AI never actually go up because up is 0.
User avatar
OmarShehata
Party member
Posts: 259
Joined: Tue May 29, 2012 6:46 pm
Location: Egypt
Contact:

Re: AI acting weird

Post by OmarShehata »

dreadkillz wrote:I think I figured out what's wrong with your code. You should do this:

Code: Select all

math.random(0,3) -- choose between [0,3]
You have math.random(4) which chooses a number between 1 and 4 so your AI never actually go up because up is 0.
Boy do I feel stupid for missing that.

But this is why I always do math.random() * 4 instead, and round it if needed.
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 2 guests