Page 1 of 1

character control in physics side scrolling

Posted: Sun Apr 19, 2009 6:10 am
by athanazio
Hey !
my first time with physics in LOVE ... And having a bad time to control the char,

here is the app
already tested to use impulse instead of force, seems to move smother but still can go inside the polygons,
is this because I'm moving the body ?

anyway Im really stuck on this.

http://www.athanazio.com/wp-content/upl ... 316am.love

Re: character control in physics side scrolling

Posted: Sun Apr 19, 2009 4:18 pm
by Alex
See other threads on physics. applyForce doesn't work(), use setVelocity(). Also you have to scale down the sizes of things for box2D to handle them sanely (CAMERA is the widely accepted solution to this).

Your game made me smile. :D

Re: character control in physics side scrolling

Posted: Sun Apr 19, 2009 6:31 pm
by Sparx
If you want to do it yourself and are interested in mathematics read these:

http://www.gamedev.net/reference/articl ... le1026.asp
http://www.gamedev.net/reference/progra ... /page2.asp

the first one is not quite correct.. my actuall programm uses these theories.

Both even work with concave polygons and use raycasting.

Notice: The first one works with circles only(but will be enough for your game I guess)/took me 3 days to find out why it doesn't work for elipsoids(the corners react wrong)

The following will be interesting to:
http://www.gamasutra.com/features/20000 ... der_01.htm

On page 2 you will find "Don’t Cross that Line".. That theorie is Perfect for non raycasted point-checks.... I'm willing to give my actuall code for the last one:
Pointx and Pointy are the polygons with index 1 up to each number you like Pointny and nx is the polygon normal for line between given point and the next one.

Code: Select all

function closestPointOnPolygon(x,y, index)
temp=1000000000
rx=0
ry=0
	for index2,content2 in pairs(Pointx[index]) do
		if(Pointx[index][(index2+1)]==nil) then index3=1 else index3=index2+1 end
		tx, ty = closestPointOnLine(x,y, Pointx[index][index2],Pointy[index][index2], Pointx[index][index3],Pointy[index][index3])
		if(((tx-x)^2+(ty-y)^2)<temp) then
			rx=tx
			ry=ty
			temp=((tx-x)^2+(ty-y)^2)
		end
	end
   return rx, ry
end

function PointInPolygon(px,py)
	for index,content in pairs(Pointx) do
	temp=0
		for index2,content2 in pairs(Pointx[index]) do
			if(Pointx[index][(index2+1)]==nil) then index3=1 else index3=index2+1 end
			if((((Pointy[index][index2]-py)<=0) and ((Pointy[index][index3]-py)>0)) or (((Pointy[index][index2]-py)>0) and ((Pointy[index][index3]-py)<=0))) then
				tx, ty = closestPointOnLine(px,py, Pointx[index][index2],Pointy[index][index2], Pointx[index][index3],Pointy[index][index3])
				if(tx>=px) then temp=1-temp end
			end
		end
		if(temp==1) then return 1, closestPointOnPolygon(px,py, index) end
	end
	return 0, 0, 0
end
To let the normals of the lines be computed upon load after giving the polygons(This function only works with the Vertexes of a polygon given clockwise):

Code: Select all

	Pointnx={}
	Pointny={}
	for index,content in pairs(Pointx) do
	Pointnx[index]={}
	Pointny[index]={}
		for index2,content2 in pairs(Pointx[index]) do
			if(Pointx[index][(index2+1)]==nil) then index3=1 else index3=index2+1 end
			linelen=math.sqrt((Pointx[index][(index3)]-Pointx[index][(index2)])^2+(Pointy[index][(index3)]-Pointy[index][(index2)])^2)
			Pointnx[index][index2]=(Pointy[index][(index3)]-Pointy[index][(index2)])/linelen
			Pointny[index][index2]=-(Pointx[index][(index3)]-Pointx[index][(index2)])/linelen
		end
	end
If you really want to controll the physics of your game try understanding these functions and what they do. Read, Read, code, find out that it's wrong, read again and go on with try and error, you will learn much doing this.

Re: character control in physics side scrolling

Posted: Mon Apr 20, 2009 4:33 am
by cag
some thoughts: I've implemented a platforming engine using something other than LOVE before. for character control, it is a good idea to stick with forces + linear velocity damping. assuming you give good values for the forces and velocity damping factor, the character will accelerate to a nice max speed. If you want a very responsive character, I would suggest high forces + high damping. I used impulses for jumps as well.
my physics was simplified, given I only needed simplistic collision detection, but you could use Box2d for this purpose if you so choose.

@sparx: why reinvent the wheel? box2d has enough collision detection capabilities for most things. handrolling your own physics engine is a _lot_ of work if it isn't simple.

Re: character control in physics side scrolling

Posted: Tue Apr 21, 2009 10:07 pm
by athanazio
thx guys I end using change of the velocity, but will give a try with high forces + high damping