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
Create a IA
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
- tentus
- Inner party member
- Posts: 1060
- Joined: Sun Oct 31, 2010 7:56 pm
- Location: Appalachia
- Contact:
Re: Create a IA
Hmm, you've got a problem in the code you posted that you need to address first.
...needs to be...
You are trying to set variable in a subtable that doesn't exist.
Code: Select all
monstrea = love.graphics.newImage("mob.png")
monstrea.x = 22
monstrea.y = 12
Code: Select all
monstrea = {}
monstrea.img = love.graphics.newImage("mob.png")
monstrea.x = 22
monstrea.y = 12
Kurosuke needs beta testers
-
- Prole
- Posts: 19
- Joined: Sun Jan 29, 2012 5:03 pm
Re: Create a IA
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
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
- tentus
- Inner party member
- Posts: 1060
- Joined: Sun Oct 31, 2010 7:56 pm
- Location: Appalachia
- Contact:
Re: Create a IA
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()
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.
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().
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
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
Hint: you're gonna need to use math.cos() and math.sin().
Last edited by tentus on Sun Jan 29, 2012 5:38 pm, edited 1 time in total.
Kurosuke needs beta testers
Re: Create a IA
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.
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.
- tentus
- Inner party member
- Posts: 1060
- Joined: Sun Oct 31, 2010 7:56 pm
- Location: Appalachia
- Contact:
Re: Create a IA
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:
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?
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
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?
Kurosuke needs beta testers
-
- Prole
- Posts: 19
- Joined: Sun Jan 29, 2012 5:03 pm
Re: Create a IA
12 ?
1 ?
2 ?
EDIT:
Merci pour ton aide,le personnage bouge
1 ?
2 ?
EDIT:
Merci pour ton aide,le personnage bouge
Re: Create a IA
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.
Who is online
Users browsing this forum: Ahrefs [Bot], Bing [Bot], Google [Bot] and 10 guests