Page 1 of 2

Apply Force Relative To Rotation?

Posted: Fri Apr 17, 2009 4:13 pm
by Pawnda
Okay guys. I have been trying for hours now... and have searched all over the forum...
I have a box that i rotate with right and left arrow on the keyboard. Then i want it to go forward when i press the up key.
But... Apply impulse only works with x and y coordinates. So i need a way to translate the coordinates + the rotation to something i can input to apply force.

Could you please help me, thank you.

Re: Apply Force Relative To Rotation?

Posted: Fri Apr 17, 2009 4:34 pm
by lejeunerenard
if you are storing the angle, say as theta, and give the amount of force you want to apply as R, then you would get something like this:
something:applyForce(R*math.cos(theta),R*math.sin(theta))

I have to warn you though, Impulse and Force have been giving me problems and from what I hear are broken. Ive bee using setVelocity() to move my character around...

Re: Apply Force Relative To Rotation?

Posted: Fri Apr 17, 2009 5:27 pm
by Pawnda
Thanks alot. But it's acting very strangely...
Here's the code:

Code: Select all

function load()
	local font = love.graphics.newFont("WildRide.ttf",20)
	love.graphics.setFont(font)
	love.graphics.setColor(0,0,0,255)
	love.graphics.setBackgroundColor(255,255,255)
	world = love.physics.newWorld(2000,2000)
	world:setGravity(0,0)
	box_body = love.physics.newBody(world,100,100)
	box_shape = love.physics.newRectangleShape(box_body,35,35)
	box_body:setMassFromShapes()
	box_body:setDamping(10)
	box_body:setAngularDamping(20)
end
function update(dt)
	love.timer.sleep(10)
	world:update(dt)
end

function draw()
	love.graphics.setColor(0,0,0,255)
	love.graphics.draw("FPS: " .. love.timer.getFPS(), 2, 15)
	box_body:applyImpulse(0,0)
	if love.keyboard.isDown(love.key_right) then
		box_body:setSpin(100)
	end
	if love.keyboard.isDown(love.key_left) then
		box_body:setSpin(-100)
	end
	if love.keyboard.isDown(love.key_up) then
		box_body:applyImpulse(10000*math.cos(box_body:getAngle()),10000*math.sin(box_body:getAngle()))
	end
	love.graphics.polygon(love.draw_line,box_shape:getPoints())
end

function keypressed(key)
	if key == love.key_escape then
		love.system.exit()
	end
end

Re: Apply Force Relative To Rotation?

Posted: Fri Apr 17, 2009 7:43 pm
by lejeunerenard
yeah I didnt run your code but I know for whatever reason the physics engine likes small numbers like less than 10 so what you can do is scale the physics part of your game down and then upscale them for drawing. You can do this via CAMERA which havent gotten to work but thats because Ive been using the Envy framwork, or you can use a small set of methods that I wrote. Ill attach my scale.lua which you need to include by typing:

Code: Select all

love.filesystem.include("game/scale.lua");
somewhere in the beginning of your document. Then you use the methods scale(thing_to_scale) and descale(thing_to_return_to_normal_size) to shrink and return to normal size respectively... also descaleArray() is for arrays but with output each element like so... x1,y1,x2,y2,x3,y3... etc... setScale is used to set the constant scale rate you want to use.

Besides scaling, as I said, applyForce() and applyImpulse() dont work. They wil be patched as soon as Alex's patch is actually applied but who knoes when.

Also you probably should do the physics modifications such as changing spin in the update function instead of the draw... I dont think it makes a difference but its atleast a habit.

Re: Apply Force Relative To Rotation?

Posted: Fri Apr 17, 2009 7:46 pm
by Alex
Do not use applyForce() or applyImpulse(). They are broken hard. I've reported the bug and sent a patch, but until it goes through, use setVelocity(). It is not broken, and it's possible to model acceleration and jerk using only setVelocity and the time increment.

Aside from that, lejeaunerenard is right. Derive your new x and y using the amount of force you want to apply (R) and the the angle that the object is rotated (theta):

Code: Select all

vx, vy = object:getVelocity()
newX, newY = R*math.cos(theta), R*math.sin(theta)
object:setVelocity(newX + vx, newY + vy)

Re: Apply Force Relative To Rotation?

Posted: Sat Apr 18, 2009 2:33 pm
by Pawnda
Still doesen't work... I thing the getAngle is bugged...

Code: Select all

function load()
	local font = love.graphics.newFont("WildRide.ttf",20)
	love.graphics.setFont(font)
	love.graphics.setColor(0,0,0,255)
	love.graphics.setBackgroundColor(255,255,255)
	world = love.physics.newWorld(2000,2000)
	world:setGravity(0,0)
	box_body = love.physics.newBody(world,100,100)
	box_shape = love.physics.newRectangleShape(box_body,35,35)
	box_body:setMassFromShapes()
	box_body:setDamping(10)
	box_body:setAngularDamping(20)
end
function update(dt)
	love.timer.sleep(10)
	world:update(dt)
end

function draw()
	love.graphics.setColor(0,0,0,255)
	love.graphics.draw("FPS: " .. love.timer.getFPS(), 2, 15)
	love.graphics.draw("Angle: " .. math.floor(box_body:getAngle()),2,35)
	box_body:applyImpulse(0,0)
	if love.keyboard.isDown(love.key_right) then
		box_body:setSpin(100)
	end
	if love.keyboard.isDown(love.key_left) then
		box_body:setSpin(-100)
	end
	if love.keyboard.isDown(love.key_up) then
		theta = math.floor(box_body:getAngle())
		vx, vy = box_body:getVelocity()
		newX, newY = 10*math.cos(theta), 10*math.sin(theta)
		box_body:setVelocity(newX + vx, newY + vy)
	end
	love.graphics.polygon(love.draw_line,box_shape:getPoints())
end

function keypressed(key)
	if key == love.key_escape then
		love.system.exit()
	end
end

Re: Apply Force Relative To Rotation?

Posted: Sun Apr 19, 2009 1:49 am
by Alex
getAngle() returns the angle in degrees, not radians as would be expected. This will change in the 6.0 release. Until then, use math.rad() or math.deg() to switch from degrees to radians or radians to degrees.

Hopefully you know the difference? If not, it's an essential topic to learn about if you're doing even the simplest trig. If you do, then don't forget to read the love documentation to learn what values the functions are actually returning, not to mention the Lua reference to see what values the math functions expect.

Re: Apply Force Relative To Rotation?

Posted: Sun Apr 19, 2009 8:40 pm
by Pawnda
Works perfectly :) didn't even notice it was using degrees ^^
But theres just one more problem now... I think it's a limitation in the engine?
I can't push more than 2-4 keys at the same time on the keyboard...?

Re: Apply Force Relative To Rotation?

Posted: Mon Apr 20, 2009 2:10 am
by Alex
Hmm. This isn't a problem I've encountered, and I've been using I think a max of four keys at a time or so. It could very well be an issue one of many levels, though. Different keyboards support a maximum number of keys, so this could very well be your problem. Other than that, the OS you're using, SDL, or love all have to track the key presses, although I have a feeling your OS and SDL can handle more key presses than that.

Anyone else seen keypress limitations?

Re: Apply Force Relative To Rotation?

Posted: Mon Apr 20, 2009 5:18 am
by Pawnda
Just tried it on my other computer (stationary)
Works perfectly ^^
So i think you are right... It's either the OS or the keyboard...