Newbie trigonometry help

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.
User avatar
Taehl
Dreaming in associative arrays
Posts: 1025
Joined: Mon Jan 11, 2010 5:07 am
Location: CA, USA
Contact:

Re: Newbie trigonometry help

Post 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.
Earliest Love2D supporter who can't Love anymore. Let me disable pixel shaders if I don't use them, dammit!
Lenovo Thinkpad X60 Tablet, built like a tank. But not fancy enough for Love2D 0.10.0+.
kurai
Prole
Posts: 8
Joined: Fri Mar 18, 2011 9:52 pm

Re: Newbie trigonometry help

Post 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 :)
User avatar
Taehl
Dreaming in associative arrays
Posts: 1025
Joined: Mon Jan 11, 2010 5:07 am
Location: CA, USA
Contact:

Re: Newbie trigonometry help

Post by Taehl »

Your player.png has the player facing up, yes? An angle of 0 means up.
Earliest Love2D supporter who can't Love anymore. Let me disable pixel shaders if I don't use them, dammit!
Lenovo Thinkpad X60 Tablet, built like a tank. But not fancy enough for Love2D 0.10.0+.
User avatar
slime
Solid Snayke
Posts: 3160
Joined: Mon Aug 23, 2010 6:45 am
Location: Nova Scotia, Canada
Contact:

Re: Newbie trigonometry help

Post by slime »

An angle of 0 means right, I thought.
User avatar
Taehl
Dreaming in associative arrays
Posts: 1025
Joined: Mon Jan 11, 2010 5:07 am
Location: CA, USA
Contact:

Re: Newbie trigonometry help

Post 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.
Earliest Love2D supporter who can't Love anymore. Let me disable pixel shaders if I don't use them, dammit!
Lenovo Thinkpad X60 Tablet, built like a tank. But not fancy enough for Love2D 0.10.0+.
User avatar
Adamantos
Prole
Posts: 27
Joined: Sun May 16, 2010 10:47 pm

Re: Newbie trigonometry help

Post 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
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Newbie trigonometry help

Post by Robin »

Or just draw images facing right instead of up. ;)
Help us help you: attach a .love.
User avatar
Adamantos
Prole
Posts: 27
Joined: Sun May 16, 2010 10:47 pm

Re: Newbie trigonometry help

Post by Adamantos »

...waaaay too easy :ultraglee:
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 3 guests