Page 2 of 2

Re: Newbie trigonometry help

Posted: Sun Mar 20, 2011 6:37 pm
by Taehl
Good catch, Adamantos. Yeah, that's a problem too. For the record, kurai, Love and Lua always use radians for trig, not degrees.

Re: Newbie trigonometry help

Posted: Sun Mar 20, 2011 9:12 pm
by kurai
That's t much better, but still the movement is weird. When I press the movement button without rotating, the player moves left. But when I rotate, the direction starts to mess up, and the player is not moving correctly (left and right are right, up and down switched).
The updated code:

Code: Select all

function love.load()
	love.graphics.setMode(1024, 768, false, false)
	hero = {}
	hero.angle=0;
	hero.speed=100;
	hero.x=love.graphics.getWidth()/2;
	hero.y=love.graphics.getHeight()/2;
	hero.sprite=love.graphics.newImage("art/player.png");
	
end


function love.draw()
	love.graphics.draw(hero.sprite,hero.x,hero.y,hero.angle,1,1,14,15);
end

function love.update(dt)
	if love.keyboard.isDown("left") then
		hero.angle=hero.angle-dt*3
	end
	if love.keyboard.isDown("right") then
		hero.angle=hero.angle+dt*3
	end
	if love.keyboard.isDown("up") then
      hero.x=hero.x+math.cos(hero.angle)*hero.speed*dt
      hero.y=hero.y-math.sin(hero.angle)*hero.speed*dt
	end
end
Thank you for your help! Learning so much here :)

Re: Newbie trigonometry help

Posted: Sun Mar 20, 2011 10:19 pm
by Taehl
Your player.png has the player facing up, yes? An angle of 0 means up.

Re: Newbie trigonometry help

Posted: Sun Mar 20, 2011 11:10 pm
by slime
An angle of 0 means right, I thought.

Re: Newbie trigonometry help

Posted: Mon Mar 21, 2011 12:56 am
by Taehl
No. If you draw an image with an angle of 0, it will be shown with upright - not rotated. An angle of pi/2 would be right. Unless you're using a screen transform, I suppose.

Re: Newbie trigonometry help

Posted: Mon Mar 21, 2011 12:51 pm
by Adamantos
hi,

please just use my code block and everything is fine :cool:

Your problem is the difference in the coordinate system:
The graphics.draw() function uses angle=0 for upright and rotates in a counter-clockwise manner

In the "mathematical-system" you also rotate counter-clockwise, but angle=0 is on the right.

Now...
the coordinate system for all graphical functions is mirrored, so the x-axis goes to the right and the y-axix goes downside.
So the y-axis is mirrowed and you have to add 90° ... since cos(x) = sin(x+90°) you can just use

Code: Select all

hero.x = hero.x + math.sin(hero.angle) * hero.speed * dt
hero.y = hero.y - math.cos(hero.angle) * hero.speed * dt
hope I could help

Re: Newbie trigonometry help

Posted: Mon Mar 21, 2011 5:37 pm
by Robin
Or just draw images facing right instead of up. ;)

Re: Newbie trigonometry help

Posted: Mon Mar 21, 2011 11:25 pm
by Adamantos
...waaaay too easy :ultraglee: