Page 1 of 1

[solved] how to turn a player/character?

Posted: Wed Apr 23, 2014 4:14 pm
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

Re: how to turn a player/character?

Posted: Wed Apr 23, 2014 4:29 pm
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.

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

Posted: Wed Apr 23, 2014 4:32 pm
by hairy puppy
great, that does look much like my gamemaker code as well. just the obvious changes in code.

thank you very much

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

Posted: Wed Apr 23, 2014 9:38 pm
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 ) )

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

Posted: Wed Apr 23, 2014 9:49 pm
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

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

Posted: Wed Apr 23, 2014 10:02 pm
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.

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

Posted: Wed Apr 23, 2014 10:07 pm
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