Proper enemies / Multiple enemies.

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
User avatar
Ryne
Party member
Posts: 444
Joined: Fri Jan 29, 2010 11:10 am

Proper enemies / Multiple enemies.

Post by Ryne »

Hello again. I have a question about forcing enemies to follow the player. I've already achieved this (somewhat) but it's obviously very sloppy. The code I'm currently using is this:

Code: Select all


	if CheckCollision(player, enemy) == false then
	enemy.x = enemy.x + (player.x - enemy.x) * (dt - 0.01)
	enemy.y = enemy.y + (player.y - enemy.y) * (dt - 0.01)
	end

So basically if the enemy isn't in contact with the player, then it's following them. The way it is now causes the zombie to be faster or slower depending on the distance from the player. I would like the zombie to spawn in a specific position and slowly walk toward the player ,Like a traditional zombie would behave. This way it would work great once there are more than one zombie on the field.

I actually thought that the way It is currently would be okay, but it turns out it won't be great. So I'm wondering if there is a more efficient way to achieve this?

Thank's again guys.

A link to my current project can be found at the bottom of the first post in this thread:

http://love2d.org/forums/viewtopic.php?f=5&t=2098
Last edited by Ryne on Fri Nov 12, 2010 9:41 pm, edited 1 time in total.
@rynesaur
User avatar
vrld
Party member
Posts: 917
Joined: Sun Apr 04, 2010 9:14 pm
Location: Germany
Contact:

Re: Proper enemies.

Post by vrld »

You need to get the direction to the player. The direction is the normalized vector, i.e. the vector divided by it's length, from enemy to player. You already do get the vector to the player, so it's only a small modification:

Code: Select all

-- the vector from enemy to the player
local vx = player.x - enemy.x
local vy = player.y - enemy.y

-- normalize it, i.e. divide it by it's length
local length = math.sqrt(vx * vx + vy * vy)
vx = vx / length
vy = vy / length

-- move the zombie towards the player
enemy.x = enemy.x + vx * enemy.speed * dt
enemy.y = enemy.y + vy * enemy.speed * dt
I have come here to chew bubblegum and kick ass... and I'm all out of bubblegum.

hump | HC | SUIT | moonshine
User avatar
nevon
Commander of the Circuloids
Posts: 938
Joined: Thu Feb 14, 2008 8:25 pm
Location: Stockholm, Sweden
Contact:

Re: Proper enemies.

Post by nevon »

One way to do it is to get the vector between the enemy and the player, and use that for the zombie movement. Subtract the zombie's position from the player's position, and you've got the vector. Then divide it by the length to normalize it. So, you'd do this:

Code: Select all

if not CheckCollision(player, enemy) then
    local dx = player.x-enemy.x
    local dy = player.y-enemy.y

    local magnitude = math.sqrt( math.pow(dx,2) + math.pow(dy, 2) )
    dx = dx/length
    dy = dy/length
    
    enemy.x = enemy.x + dx*enemy.speed*dt
    enemy.y = enemy.y + dy*enemy.speed*dt
end
I haven't actually tested it, so I might have fucked up at some point. But if you want to learn more about it, check out this fantastic guide: http://blog.wolfire.com/2009/07/linear- ... rs-part-1/

EDIT: Argh, ninja'd by vrld!
User avatar
Ryne
Party member
Posts: 444
Joined: Fri Jan 29, 2010 11:10 am

Re: Proper enemies.

Post by Ryne »

Thanks a lot guys, I appreciate it.
@rynesaur
User avatar
partymetroid
Citizen
Posts: 80
Joined: Thu Feb 18, 2010 6:06 am
Location: Branson, State of Misery
Contact:

Re: Proper enemies.

Post by partymetroid »

mind = blown :awesome:

[edit] mind, not mine >_>

[edit2] From what I can tell, you're making the zombies slow down exponentially so that, when he "arrives" at the player, he's slowed down to ~zilch? If so, I would like to give my suggestion:

The zombies would walk around the map, almost aimlessly until the player would come "in sight" (within a given distance; not angle (though I guess you could try implementing that as well)). And then they become active and run toward the player, and when nearing him they become a little crazy, movements becoming erratic (some randomization formulae would probably work).

I'll think of some other stuff too. :3

[edit3] Oh, and using some fancy interpolating/tweening would make for some awesome effects, too. :)
User avatar
Ryne
Party member
Posts: 444
Joined: Fri Jan 29, 2010 11:10 am

Re: Proper enemies.

Post by Ryne »

partymetroid wrote:mind = blown :awesome:

[edit] mind, not mine >_>

[edit2] From what I can tell, you're making the zombies slow down exponentially so that, when he "arrives" at the player, he's slowed down to ~zilch? If so, I would like to give my suggestion:

The zombies would walk around the map, almost aimlessly until the player would come "in sight" (within a given distance; not angle (though I guess you could try implementing that as well)). And then they become active and run toward the player, and when nearing him they become a little crazy, movements becoming erratic (some randomization formulae would probably work).

I'll think of some other stuff too. :3

[edit3] Oh, and using some fancy interpolating/tweening would make for some awesome effects, too. :)
That is a possibility. The way I have it programmed now is that they walk faster if they are farther away. I thought this was cool since it kind of gives the illusion that they are always about to catch up to you, though they cant. The poses problems such as the one in my demo, where at the beginning of the level the zombie near-instantly translates to the player. I would like them to approach from the right or left, nice, slow, and stupid. Like Zombies should be.

Edit: I also just realized that your Idea would be quite easy since I already have a "player direction", and an "enemy direction variable". I could make the enemy direction "random" until it sees a player at which point it would walk towards him. So if you were on a new "level" the zombie would be standing aimlessly(just like in the movies), until he sees the player. Good idea!
@rynesaur
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Proper enemies.

Post by Robin »

Ryne wrote:Edit: I also just realized that your Idea would be quite easy since I already have a "player direction", and an "enemy direction variable". I could make the enemy direction "random" until it sees a player at which point it would walk towards him. So if you were on a new "level" the zombie would be standing aimlessly(just like in the movies), until he sees the player. Good idea!
Flocking! Flocking! :crazy: How about that?
Help us help you: attach a .love.
User avatar
Ryne
Party member
Posts: 444
Joined: Fri Jan 29, 2010 11:10 am

Re: Proper enemies.

Post by Ryne »

Robin wrote:
Ryne wrote:Edit: I also just realized that your Idea would be quite easy since I already have a "player direction", and an "enemy direction variable". I could make the enemy direction "random" until it sees a player at which point it would walk towards him. So if you were on a new "level" the zombie would be standing aimlessly(just like in the movies), until he sees the player. Good idea!
Flocking! Flocking! :crazy: How about that?
That's next I suppose. Though I'm curious how to do it. When I think of program's I just think of Conditionals and if statements to program ANYTHING. So "flocking could obviously be handled like that. I now, however, have a good understanding of Functions. So I guess I could attempt to create a function to handle such zombie behavior. I would obviously need to be able to spawn multiples first though. The only issue is that I only know how to create enemies separately right now, would you mind helping me? I think the best thing would be to have zombies spawning randomly but at a radius from the player.

When I think of game play in my head, the player walks into a room and zombies are scattered about though when they see the enemy they would walk toward him. If they spawn to close it wouldn't be very good.

Any Ideas?
@rynesaur
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Proper enemies / Multiple enemies.

Post by Robin »

You could check for the distance with the player first:

Code: Select all

too_close = 30 -- or something
function dist(ax, ay, bx, by)
    return math.sqrt((ax-bx)^2+(ay-by)^2)
end
function new_zombie()
    local x, y = math.random(maxx), math.random(maxy)
    while dist(player.x, player.y, x, y) < too_close do
        x, y = math.random(maxx), math.random(maxy)
    end
    return {x = x, y = y}
end
The general rule with things like that is "try, and while it's not acceptable, keep trying."
Help us help you: attach a .love.
Post Reply

Who is online

Users browsing this forum: No registered users and 7 guests