[solved] how to turn a player/character?

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
User avatar
hairy puppy
Prole
Posts: 29
Joined: Tue Apr 22, 2014 3:34 pm

[solved] how to turn a player/character?

Post by hairy puppy »

hello all,

im working on some things and testing some stuff. and one that is burning me is getting players/characters to turn.
now what it is i have already is the movement, which is fine and dandy really. but im not one to just use 'up goes up, down goes down' etc. what im after is up always means 'going forward', you have to turn your character to go forward in the direction you need. only the left & right are used to turn. down can be used to reverse. i guess like micro machines, if that helps

here is the code i have already, which works well if you want 8 dimension control where up is up down is down and so forth, and im just figuring out the turning, but im running intho a block. i actualy figured this out in gamemaker, but cant seem to get it with LOVE, though more than likely its an easy outcome

Code: Select all

------ moving player ------
function player_move(dt)
------ best so far ------
	if love.keyboard.isDown ("up") then
      playerY = math.floor(playerY - 3)
   end
   if love.keyboard.isDown("down") then
      playerY = math.floor(playerY + 3)
   end
   if love.keyboard.isDown("left") then
      playerX = math.floor(playerX - 3)
   end
   if love.keyboard.isDown("right") then
      playerX = math.floor(playerX + 3)
   end
end
Last edited by hairy puppy on Wed Apr 23, 2014 4:31 pm, edited 1 time in total.
lewis lepton
------
http://smokingbunny.net
User avatar
veethree
Inner party member
Posts: 877
Joined: Sat Dec 10, 2011 7:18 pm

Re: how to turn a player/character?

Post by veethree »

Give you player an angle value, Then use math.sin and math.cos to move him.
something like this:

Code: Select all

player.x = player.x + math.cos(player.angle) * player.velocity * dt
	player.y = player.y + math.sin(player.angle) * player.velocity * dt
I wrote a little demo for this that i've attached.
Attachments
movement.love
(546 Bytes) Downloaded 62 times
User avatar
hairy puppy
Prole
Posts: 29
Joined: Tue Apr 22, 2014 3:34 pm

Re: [solved] how to turn a player/character?

Post by hairy puppy »

great, that does look much like my gamemaker code as well. just the obvious changes in code.

thank you very much
lewis lepton
------
http://smokingbunny.net
davisdude
Party member
Posts: 1154
Joined: Sun Apr 28, 2013 3:29 am
Location: North Carolina

Re: [solved] how to turn a player/character?

Post by davisdude »

Just to put in some input, if you wanted to keep the player dx and dy values separate for some reason, you could also do this (may be obvious, I don't really know):

Code: Select all

-- All in function love.update
Dx = Speed * math.cos( Rotation )
Dy = Speed * math.sin( Rotation )

x = x - Dx * dt
y = y - Dx * dt
I dunno. I think that looks nicer, anyway.
Also, you can rotate the thing towards other things (such descriptive words here) by using this:

Code: Select all

Rotation = math.atan2( ( ObjectY - TargetY ), ( ObjectX - TargetX ) )
GitHub | MLib - Math and shape intersections library | Walt - Animation library | Brady - Camera library with parallax scrolling | Vim-love-docs - Help files and syntax coloring for Vim
User avatar
hairy puppy
Prole
Posts: 29
Joined: Tue Apr 22, 2014 3:34 pm

Re: [solved] how to turn a player/character?

Post by hairy puppy »

cool spot, ill keep that in mind.
im still getting used to the language. just got into the habit of using 'local' in my files. reminds me of header files.
but its all good, plus ordered the programming in lua book to have by my side. always helpful
lewis lepton
------
http://smokingbunny.net
davisdude
Party member
Posts: 1154
Joined: Sun Apr 28, 2013 3:29 am
Location: North Carolina

Re: [solved] how to turn a player/character?

Post by davisdude »

Yeah, using locals is great for keeping your code clean. The variables can be changed, of course. I was just trying to make them make sense.
Lua is (in my opinion) pretty easy to learn and get the hang of. The hard thing about Lua is getting used to some funky things Lua does.
Check out this.
GitHub | MLib - Math and shape intersections library | Walt - Animation library | Brady - Camera library with parallax scrolling | Vim-love-docs - Help files and syntax coloring for Vim
User avatar
hairy puppy
Prole
Posts: 29
Joined: Tue Apr 22, 2014 3:34 pm

Re: [solved] how to turn a player/character?

Post by hairy puppy »

yeah there have been a few things that i have noticed. but like any language, there are things that just can make understanding it a bit strange. but really, Lua/love2d has been the best language ive ever started learning
lewis lepton
------
http://smokingbunny.net
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 2 guests