How to make basic AI?

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
ilovelove2019
Citizen
Posts: 55
Joined: Wed Sep 11, 2019 10:38 am

How to make basic AI?

Post by ilovelove2019 » Fri Oct 11, 2019 10:33 pm

Hello everyone. Let's go to the main problem. I try to create an AI enemy for my game. I want them to move freely around a certain range. When the player gets close enough, they will chase the player. When the player runs far enough, the enemy will return to their original position and continue moving freely. My code seems reasonable, but I don't know why it doesn't work. The enemy moves freely for a certain amount of time and then automatically stays still even though I always create a new target point when the enemy has reached the old target point. You will see more clearly when viewing my .love file. Hope everyone help me as soon as possible. Improve my algorithm. I thank you very much!
Attachments
halloween.love
Help me solve the problem please!
(435.72 KiB) Downloaded 5 times

ilovelove2019
Citizen
Posts: 55
Joined: Wed Sep 11, 2019 10:38 am

Re: How to make basic AI?

Post by ilovelove2019 » Fri Oct 11, 2019 10:38 pm

Please take a look at my file. In ghost.lua, I have only tested the enemy's free movement so I have not written the code for the part (if self.state == "chasing"), but it is not relevant. The problem I am facing right now is the free movement of the enemy. Please be patient, this error will take some time to find out, and make sure that when observing you must keep your distance from it, otherwise it will switch to self.state = "chasing" then the enemy will stand. in place and it's harder to find fault.

User avatar
pgimeno
Party member
Posts: 1818
Joined: Sun Oct 18, 2015 2:58 pm

Re: How to make basic AI?

Post by pgimeno » Sat Oct 12, 2019 10:03 am

You're using integer coordinates and comparing the position with the exact integer value of the target. When two ghosts collide, the position becomes noninteger, and then the result will never be equal to the target.

An easy workaround would be to use mousejoints to move the ghosts, rather than setX/setY.

ilovelove2019
Citizen
Posts: 55
Joined: Wed Sep 11, 2019 10:38 am

Re: How to make basic AI?

Post by ilovelove2019 » Sat Oct 12, 2019 10:25 am

pgimeno wrote:
Sat Oct 12, 2019 10:03 am
You're using integer coordinates and comparing the position with the exact integer value of the target. When two ghosts collide, the position becomes noninteger, and then the result will never be equal to the target.
Oh! You really professional! That is really the cause of my problem.

Can you specify how to use mousejoint? I'm really poor :( I don't know how to use it. I use windfield but I don't know much about it, I can only follow the instructions here (https://github.com/adnzzzzZ/windfield#create-joints). I don't know what to do. Please my fault for that :(

ilovelove2019
Citizen
Posts: 55
Joined: Wed Sep 11, 2019 10:38 am

Re: How to make basic AI?

Post by ilovelove2019 » Sat Oct 12, 2019 10:32 am

Check out this link: https://docs.w3cub.com/love/mousejoint/. Do game developers with love2d have to learn all these commands as a minimum requirement to be able to create good enough games?

User avatar
zorg
Party member
Posts: 2695
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: How to make basic AI?

Post by zorg » Sat Oct 12, 2019 11:01 am

ilovelove2019 wrote:
Sat Oct 12, 2019 10:32 am
Check out this link: https://docs.w3cub.com/love/mousejoint/. Do game developers with love2d have to learn all these commands as a minimum requirement to be able to create good enough games?
Depends on what your definition of good enough would be. One could make a game without ever using love.physics (Box2D).
Also, not sure how up to date those sites are, best would be to always use the actual wiki: https://love2d.org/wiki/MouseJoint
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.

User avatar
pgimeno
Party member
Posts: 1818
Joined: Sun Oct 18, 2015 2:58 pm

Re: How to make basic AI?

Post by pgimeno » Sat Oct 12, 2019 11:03 am

ilovelove2019 wrote:
Sat Oct 12, 2019 10:32 am
Check out this link: https://docs.w3cub.com/love/mousejoint/. Do game developers with love2d have to learn all these commands as a minimum requirement to be able to create good enough games?
You're supposed to look into what a MouseJoint does. Once you understand it, you're supposed to learn the functions that let you do what you want. In this case, apart from the constructor, it's MouseJoint:setTarget (and possibly MouseJoint:getTarget).

Also please look at this post: https://love2d.org/forums/viewtopic.php?f=3&t=64151 especially point 7.

ilovelove2019
Citizen
Posts: 55
Joined: Wed Sep 11, 2019 10:38 am

Re: How to make basic AI?

Post by ilovelove2019 » Sat Oct 12, 2019 12:03 pm

zorg wrote:
Sat Oct 12, 2019 11:01 am
ilovelove2019 wrote:
Sat Oct 12, 2019 10:32 am
Check out this link: https://docs.w3cub.com/love/mousejoint/. Do game developers with love2d have to learn all these commands as a minimum requirement to be able to create good enough games?
Depends on what your definition of good enough would be. One could make a game without ever using love.physics (Box2D).
Also, not sure how up to date those sites are, best would be to always use the actual wiki: https://love2d.org/wiki/MouseJoint
You are right! My simple game don't need to use that physics. I just want to use the collision available in windfield library. Because I'm too stupid and I don't know how to detect collision. I would be happy if you guide me on that. Thanks you.

ilovelove2019
Citizen
Posts: 55
Joined: Wed Sep 11, 2019 10:38 am

Re: How to make basic AI?

Post by ilovelove2019 » Sat Oct 12, 2019 12:04 pm

pgimeno wrote:
Sat Oct 12, 2019 11:03 am
ilovelove2019 wrote:
Sat Oct 12, 2019 10:32 am
Check out this link: https://docs.w3cub.com/love/mousejoint/. Do game developers with love2d have to learn all these commands as a minimum requirement to be able to create good enough games?
You're supposed to look into what a MouseJoint does. Once you understand it, you're supposed to learn the functions that let you do what you want. In this case, apart from the constructor, it's MouseJoint:setTarget (and possibly MouseJoint:getTarget).

Also please look at this post: https://love2d.org/forums/viewtopic.php?f=3&t=64151 especially point 7.
Ye. I'm learning how to use it!

Post Reply

Who is online

Users browsing this forum: No registered users and 10 guests