The above video goes away if you are a member and logged in, so log in now!

 Would you like to get all the newest Gaming News fromQJ.NET in your email each day? Want to learn more about the team who brings you the QJ news?

## [GUIDE] Lua Rotating movement

This is a discussion on [GUIDE] Lua Rotating movement within the PSP Development Forum forums, part of the PSP Development, Hacks, and Homebrew category; This tutorial will show how to rotate a player and move in that direction. It's quite simple, but a bit ...

 Tweet
1. ## [GUIDE] Lua Rotating movement

This tutorial will show how to rotate a player and move in that direction. It's quite simple, but a bit of prior LUA knowledge is needed as well as if you want to understand the math part you need a bit of trigonometry knowledge.

First, we need to make a player image. We’ll just make a red square (16x16)

--create the color

red=Color.new(255, 0, 0)

--create the player image

plrimg=Image.createEmpty( 16, 16)

--Color the player

plrimg:clear(red)
Next, we make the player object and all it’s variables.

--define the player

player={x=0, y=0, hspeed=0, vspeed=0, direction=0}
Now for the main while loop

--start the while loop

while true do
Then, an important part to the movement code – sin and cos. Using the angle player.direction (in degrees), we find theta in radians. Theta is an angle on a right angled triangle (that isn’t the right angle). To find the x and y needed, we use cos and sin respectively.
--find the x and y increase. The math.rad converts it to radians so we can
--use cos and sin. the *2 means it moves 2 times faster

So we now have our vertical and horizontal speeds – now for moving the player with them.

First, check the controls

--Check the controls

Then, if up is held, change the x and y relative to the h and vspeed.
--move. Increase the player.x and player.y.

player.x=player.x+player. hspeed
player.y=player.y+player. vspeed
end
Now, to turn. To do this, we just change the player.direction variable by an amount if a button is held.

--turn

player.direction=player.d irection+3
end
player.direction=player.d irection-3
end
Then, clear the last frame.

--clear the screen

screen:clear()
Next, draw the players image at its x and y.

--draw the player

screen:blit(player.x, player.y, plrimg)
With that done, all that’s left is to freeze the screen and then end the while loop.

--freeze the screen

screen.waitVblankStart()
end

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•

All times are GMT -8. The time now is 09:52 PM.

Use of this Web site constitutes acceptance of the TERMS & CONDITIONS and PRIVACY POLICY