Newbie trigonometry help
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
- Taehl
- Dreaming in associative arrays
- Posts: 1025
- Joined: Mon Jan 11, 2010 5:07 am
- Location: CA, USA
- Contact:
Re: Newbie trigonometry help
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+.
Lenovo Thinkpad X60 Tablet, built like a tank. But not fancy enough for Love2D 0.10.0+.
Re: Newbie trigonometry help
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:
Thank you for your help! Learning so much here
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
- Taehl
- Dreaming in associative arrays
- Posts: 1025
- Joined: Mon Jan 11, 2010 5:07 am
- Location: CA, USA
- Contact:
Re: Newbie trigonometry help
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+.
Lenovo Thinkpad X60 Tablet, built like a tank. But not fancy enough for Love2D 0.10.0+.
- slime
- Solid Snayke
- Posts: 3170
- Joined: Mon Aug 23, 2010 6:45 am
- Location: Nova Scotia, Canada
- Contact:
Re: Newbie trigonometry help
An angle of 0 means right, I thought.
- Taehl
- Dreaming in associative arrays
- Posts: 1025
- Joined: Mon Jan 11, 2010 5:07 am
- Location: CA, USA
- Contact:
Re: Newbie trigonometry help
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+.
Lenovo Thinkpad X60 Tablet, built like a tank. But not fancy enough for Love2D 0.10.0+.
Re: Newbie trigonometry help
hi,
please just use my code block and everything is fine
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
hope I could help
please just use my code block and everything is fine
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
- Robin
- The Omniscient
- Posts: 6506
- Joined: Fri Feb 20, 2009 4:29 pm
- Location: The Netherlands
- Contact:
Re: Newbie trigonometry help
Or just draw images facing right instead of up.
Help us help you: attach a .love.
Re: Newbie trigonometry help
...waaaay too easy
Who is online
Users browsing this forum: Bing [Bot] and 10 guests