[SOLVED] Physics mixed with "stupid" 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
opatut
Prole
Posts: 7
Joined: Mon Feb 20, 2012 2:34 pm

[SOLVED] Physics mixed with "stupid" movement

Post 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 419 times
Last edited by opatut on Mon Feb 20, 2012 9:10 pm, edited 1 time in total.
User avatar
tentus
Inner party member
Posts: 1060
Joined: Sun Oct 31, 2010 7:56 pm
Location: Appalachia
Contact:

Re: Physics mixed with "stupid" movement

Post 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)
Kurosuke needs beta testers
opatut
Prole
Posts: 7
Joined: Mon Feb 20, 2012 2:34 pm

Re: Physics mixed with "stupid" movement

Post 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?
User avatar
tentus
Inner party member
Posts: 1060
Joined: Sun Oct 31, 2010 7:56 pm
Location: Appalachia
Contact:

Re: Physics mixed with "stupid" movement

Post 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.
Kurosuke needs beta testers
opatut
Prole
Posts: 7
Joined: Mon Feb 20, 2012 2:34 pm

Re: Physics mixed with "stupid" movement

Post by opatut »

Thanks. It did the trick ;)
Post Reply

Who is online

Users browsing this forum: Amazon [Bot], rabbitboots, Semrush [Bot] and 5 guests