AI movement

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
Maxzolythus
Prole
Posts: 5
Joined: Wed Jan 14, 2015 9:52 pm

AI movement

Post by Maxzolythus »

I've researched AI movement quite a lot on the forums, yet no one seems to have had the same problem as I. Below is my code, and I believe I understand vectors and distance and what not, but when I'm apparently "doing it right" the AI enemy disappears or is invisable. All I really want this enemy to do is while the player is in a certain area the enemy will activate and run to the player (and then attack them, but right now all I really want it to do is move.)

Code: Select all

mobs = {}

mobs.spawn = {}

--Follower of Anasi
mobs.FoA = {
	health = 50,
	mana = 70,
	speed = 350,
	attack = math.random(5,15)}


function mobs.spawn.FoA(x,y,actx,acty,actx2)

	table.insert(mobs.FoA, {x = x, y = y})
	mobs.FoA.body = love.physics.newBody(world, mobs.FoA.x, mobs.FoA.y, "dynamic")
	mobs.FoA.shape = love.physics.newCircleShape(18)
	mobs.FoA.fixture = love.physics.newFixture(mobs.FoA.body, mobs.FoA.shape)

end

function mobs.FoA.AI(dt,actx,acty,actx2)
	if char_selection == 1 then
		if Max_b:getX() > actx or Max_b:getX() < actx2 then
			for i, FoA in ipairs(mobs.FoA) do

				dx = FoA.x - Max_b:getX()
				dy = FoA.y - Max_b:getY()
				dist = math.sqrt(dx^2+dy^2)

				FoA.x = FoA.x + dx/dist * FoA.speed * dt
				FoA.y = FoA.y + dy/dist * FoA.speed * dt


			end
		end
	end
end

function mobs.FoA.draw()
	lg.setColor(200, 0, 0)
	lg.circle("fill", mobs.FoA.body:getX(), mobs.FoA.body:getY(), mobs.FoA.shape:getRadius())
	lg.setColor(255,255,255)
end
User avatar
Foxcraft
Prole
Posts: 49
Joined: Sat Mar 22, 2014 8:56 pm

Re: AI movement

Post by Foxcraft »

Hello Maxzolythus,

Not sure going by the code standalone. I prefer to play with things in action and see how it relates to the other parts.

The way I'd go about it is figuring out what is actually happening to the enemy. If you can better understand what's actually going wrong, then you can better narrow it down to the problem spot.

My first thought is if you mean the enemy actually goes invisible or if they disappear off the screen? Do you not know? I'd make a quick output in the corner that prints off the enemy's position so I can watch and see if it's randomly shooting skyward when the player comes near, or something like that. Include any other variables you might want to keep an eye on.
Maxzolythus
Prole
Posts: 5
Joined: Wed Jan 14, 2015 9:52 pm

Re: AI movement

Post by Maxzolythus »

Upon further review the enemy's Y coordinate seems to increase rapidly, however I'm not sure why since I haven't done much to modify it.
User avatar
Doctory
Party member
Posts: 441
Joined: Fri Dec 27, 2013 4:53 pm

Re: AI movement

Post by Doctory »

could you please upload a .love?
Maxzolythus
Prole
Posts: 5
Joined: Wed Jan 14, 2015 9:52 pm

Re: AI movement

Post by Maxzolythus »

I couldn't figure out to change the .zip into a .love, so is it okay if I just give you this
Attachments
S2_JtA.zip
(6.05 MiB) Downloaded 262 times
User avatar
s-ol
Party member
Posts: 1077
Joined: Mon Sep 15, 2014 7:41 pm
Location: Cologne, Germany
Contact:

Re: AI movement

Post by s-ol »

Maxzolythus wrote:I couldn't figure out to change the .zip into a .love, so is it okay if I just give you this
Just rename it... If you're on windows enable extensions in folder options.

s-ol.nu /blog  -  p.s-ol.be /st8.lua  -  g.s-ol.be /gtglg /curcur

Code: Select all

print( type(love) )
if false then
  baby:hurt(me)
end
Maxzolythus
Prole
Posts: 5
Joined: Wed Jan 14, 2015 9:52 pm

Re: AI movement

Post by Maxzolythus »

I, obviously, already tried that, but whenever I do it to .zip files it just names it filename.love.zip other files for insistence a text file will convert to a .lua or whatever. Hence why I asked if a .zip was okay, which I would have to reason that it would work similarly.
User avatar
Jasoco
Inner party member
Posts: 3727
Joined: Mon Jun 22, 2009 9:35 am
Location: Pennsylvania, USA
Contact:

Re: AI movement

Post by Jasoco »

If you Get Properties on the file you should be able to change it directly rather than via icon view in Explorer.
User avatar
s-ol
Party member
Posts: 1077
Joined: Mon Sep 15, 2014 7:41 pm
Location: Cologne, Germany
Contact:

Re: AI movement

Post by s-ol »

Maxzolythus wrote:I, obviously, already tried that, but whenever I do it to .zip files it just names it filename.love.zip other files for insistence a text file will convert to a .lua or whatever. Hence why I asked if a .zip was okay, which I would have to reason that it would work similarly.
That's what I said, you need to disable the crappy windows feature that hides extensions; it actually poses kind of a security risk for less computer-savvy people.

Folder Options -> View -> Hide Extensions for known File Types (uncheck this)

s-ol.nu /blog  -  p.s-ol.be /st8.lua  -  g.s-ol.be /gtglg /curcur

Code: Select all

print( type(love) )
if false then
  baby:hurt(me)
end
Maxzolythus
Prole
Posts: 5
Joined: Wed Jan 14, 2015 9:52 pm

Re: AI movement

Post by Maxzolythus »

Didn't work last time, thankfully it worked this time.
Attachments
S2_JtA.love
(8.34 MiB) Downloaded 92 times
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 14 guests