Page 1 of 1

How to make basic AI?

Posted: Fri Oct 11, 2019 10:33 pm
by ilovelove2019
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!

Re: How to make basic AI?

Posted: Fri Oct 11, 2019 10:38 pm
by ilovelove2019
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.

Re: How to make basic AI?

Posted: Sat Oct 12, 2019 10:03 am
by pgimeno
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.

Re: How to make basic AI?

Posted: Sat Oct 12, 2019 10:25 am
by ilovelove2019
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 :(

Re: How to make basic AI?

Posted: Sat Oct 12, 2019 10:32 am
by ilovelove2019
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?

Re: How to make basic AI?

Posted: Sat Oct 12, 2019 11:01 am
by zorg
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

Re: How to make basic AI?

Posted: Sat Oct 12, 2019 11:03 am
by pgimeno
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.

Re: How to make basic AI?

Posted: Sat Oct 12, 2019 12:03 pm
by ilovelove2019
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.

Re: How to make basic AI?

Posted: Sat Oct 12, 2019 12:04 pm
by ilovelove2019
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!