Page 1 of 1

[SOLVED] Physics mixed with "stupid" movement

Posted: Mon Feb 20, 2012 2:55 pm
by opatut
So I am making a physics puzzle game where there are some tiny characters that move around. The plan is to make them move at a specific speed on the ground (say 10px/s) and react to collisions with walls and other objects. And I want them to change the direction (left <-> right) when they hit a wall.

I have no problem setting up all the physics stuff, works perfectly. I solved the "stupid" movement (by stupid I mean the old-style, "position += velocity" way of moving things) by using the physics engine and constantly applying a linear force.

Problem is detecting when a collision with a wall occurs. Because the little fellows are walking on a tile-based ground, they sometimes collide with a block underneath... Thus they turn around even though there was no wall. Any ideas how to fix this? Thanks!

This is what it looks like:
poyfW.png
poyfW.png (5.14 KiB) Viewed 380 times

Re: Physics mixed with "stupid" movement

Posted: Mon Feb 20, 2012 3:25 pm
by tentus
I'm assuming that your AI has a sort of "if this left of that then turn left, elseif this right of that then turn right" kind of dealio, right?

What you need is a second if that wraps around the whole if statement that checks for that having an edge within a tolerance. A tolerance of say four pixels is probably all you need. So, in psuedocode:

Code: Select all

local tolerance = 4
if (that.y + that.radius <= this.y + this.radius - tolerance) or (that.y - that.radius >= this.y - this.radius + tolerance) then
	if that.x > this.x then
		turn_left()
	elseif that.x < this.x then
		turn_right()
	end
end
(In this example I use radius because it's easier to type than height / 2)

Re: Physics mixed with "stupid" movement

Posted: Mon Feb 20, 2012 3:46 pm
by opatut
Oh my god, thank you. I completely forgot that I could do the collision detection stuff with the old-style AABBs (which all my entites provide anyway) instead of using the love.physics Contact object... This is probably far easier to understand and implement than using Normals, Velocities and stuff... Thanks for the idea, I'll probably get the rest.

Now one more thing. What do you recommend for making the AI move at a constant speed while actually moving the physics body around? Applying a force makes it accelerate, but I'd rather like the "all-or-none" style movement (immediate direction change). Also, I'd like to control the speed exactly, not with fiddling around with force and damping/friction parameters. Any ideas on that?

Re: Physics mixed with "stupid" movement

Posted: Mon Feb 20, 2012 3:52 pm
by tentus
I think Body:setLinearVelocity is what you're asking for, but I'm not 100%. It's been a little while since I played with physics.

Re: Physics mixed with "stupid" movement

Posted: Mon Feb 20, 2012 9:10 pm
by opatut
Thanks. It did the trick ;)