Page 1 of 1

AI movement

Posted: Wed Jan 14, 2015 10:01 pm
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

Re: AI movement

Posted: Thu Jan 15, 2015 6:25 am
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.

Re: AI movement

Posted: Thu Jan 15, 2015 7:42 pm
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.

Re: AI movement

Posted: Thu Jan 15, 2015 7:48 pm
by Doctory
could you please upload a .love?

Re: AI movement

Posted: Thu Jan 15, 2015 9:17 pm
by Maxzolythus
I couldn't figure out to change the .zip into a .love, so is it okay if I just give you this

Re: AI movement

Posted: Thu Jan 15, 2015 10:05 pm
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.

Re: AI movement

Posted: Fri Jan 16, 2015 12:36 am
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.

Re: AI movement

Posted: Fri Jan 16, 2015 8:54 am
by Jasoco
If you Get Properties on the file you should be able to change it directly rather than via icon view in Explorer.

Re: AI movement

Posted: Fri Jan 16, 2015 1:11 pm
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)

Re: AI movement

Posted: Fri Jan 16, 2015 7:37 pm
by Maxzolythus
Didn't work last time, thankfully it worked this time.