Page 1 of 1

Create a IA

Posted: Sun Jan 29, 2012 5:12 pm
by lecrafteur
Hello everyone, sorry this translation because normally I'm French.
(google translation spotted)
Here is my problem, I would like to make an AI that moves the mob to me!
I made this code:

monstrea = love.graphics.newImage("mob.png")
monstrea.x = 22
monstrea.y = 12

But the problem is that it is moving and that's all, I would really like coming to the player.
If you want more information, ask me!
Thank you in advance :)
ps: I'm new

Re: Create a IA

Posted: Sun Jan 29, 2012 5:16 pm
by tentus
Hmm, you've got a problem in the code you posted that you need to address first.

Code: Select all

monstrea = love.graphics.newImage("mob.png")
monstrea.x = 22
monstrea.y = 12
...needs to be...

Code: Select all

monstrea = {}
monstrea.img = love.graphics.newImage("mob.png")
monstrea.x = 22
monstrea.y = 12
You are trying to set variable in a subtable that doesn't exist.

Re: Create a IA

Posted: Sun Jan 29, 2012 5:25 pm
by lecrafteur
Thank you but it is just displayed on the screen and nothing: (
I want the player that goes on here is that its code:
function love.load()
man = love.graphics.newImage("man.png")
x = 50
y = 50
speed = 50
end

function love.draw()
love.graphics.draw(man, x, y)
end

Thank you in advance

Re: Create a IA

Posted: Sun Jan 29, 2012 5:34 pm
by tentus
Word to the wise: [ code ] tags will encourage Lovers to help you. It just makes your code much easier to read.

What we need is to update the game each frame. We do that by adding in love.update()

Code: Select all

function love.load()
   man = love.graphics.newImage("man.png")
   x = 50
   y = 50
   speed = 50
end

function love.update(dt)
   -- do stuff!
end

function love.draw()
   love.graphics.draw(man, x, y)
end
love.update has a special parameter, dt. dt will let us know how much time has passed since we last updated. It's essential to making your game run consistently on different computers.

Now, we need a monster to chase the player. I'm calling it mob in the following code example.

Code: Select all

function love.load()
   man = love.graphics.newImage("man.png")
   x = 50
   y = 50
   speed = 50
   mob = {}
   mob.img = love.graphics.newImage("man.png")
   mob.x = 200
   mob.y = 200
   mob.speed = 50
end

function love.update(dt)
   -- do stuff!
end

function love.draw()
   love.graphics.draw(man, x, y)
   love.graphics.draw(mob.img, mob.x, mob.y)
end
Now, in update we need to do some trig. Yes, that math subject no-one liked in school. We need to use trig to figure out where the player is relative to the mob. Can you guess how that will work?

Hint: you're gonna need to use math.cos() and math.sin().

Re: Create a IA

Posted: Sun Jan 29, 2012 5:35 pm
by runyonave
The way you would get the monster to approach the player is to get the monster's x,y axis and increment it until it is the same as the player's x,y axis.

For example, if the monstrea.x = 20, monstrea.y = 25, player.x =40, player.y = 50 you want to make a function that increments the monster's x,y axis based on the angle it is from the player's x,y axis. You then need that function to loop until the monster's x,y axis are nearly the same as the player's, or the player takes out the monster, or a wall is blocking the monsters. I'm not very good with trigonometry, so maybe someone else can provide a formula to calculate this.

Re: Create a IA

Posted: Sun Jan 29, 2012 5:40 pm
by lecrafteur
I know not :D

Re: Create a IA

Posted: Sun Jan 29, 2012 5:57 pm
by tentus
This is coming off the top of my head, so don't take it too seriously- I could be way off. But the trig is going to end up looking like this, if I'm right:

Code: Select all

function love.update(dt)
	local angle = math.atan2(y - mob.y, x - mob.x)
	mob.x = mob.x + (math.cos(angle) * mob.speed * dt)
	mob.y = mob.y + (math.sin(angle) * mob.speed * dt)
end
What's doing is figuring out the angle we need to move at using atan2, a very handy math function. Then I update the mob x and y by using cosine and sine multiplied by the mob speed (allowing different mobs to have different speeds if you want), multiplied by the dt (to make it consistent from machine to machine).

We can improve this code by clamping it a bit, because what's probably going to happen now is the mob will get to your position and then quiver on it, constantly overstepping by a fraction of a pixel. Can you guess how clamping might work?

Re: Create a IA

Posted: Sun Jan 29, 2012 6:07 pm
by lecrafteur
12 ?
1 ?
2 ?
EDIT:
Merci pour ton aide,le personnage bouge

Re: Create a IA

Posted: Sun Jan 29, 2012 6:17 pm
by tentus
No problem, glad I could help. :D

Re: Create a IA

Posted: Sun Jan 29, 2012 7:17 pm
by MarekkPie
If you are ever looking for more sophisticated steering algorithms, a good place to start would be OpenSteer, which was written by the guy who basically invented most of it. It's C++, but if you have an understand of that language you can parse through the source code to get an idea. For a more general tutorial, just search "steering algorithm" in your search engine of choice.