Game Development Reference
In-Depth Information
Figure 11-6. Mirroring an image
Turning the Player
In your game, you might want to rotate the player graphic based on where you click—for example, in
a tower defense-type game where the player is holding a gun.
As mentioned before, the math or the code can be applied to any framework, so to determine the
angle of rotation based on a point on the screen, we can use the following formula:
theAngle = math.atan2(posY - startY, posX- startX)
startX and startY are the coordinates of the player (as in our preceding example), and posX and
posY are the coordinates of a point on the screen.
Using this knowledge, let's make an image on the screen that shall always point to the direction of
the mouse cursor.
local _W, _H = love.graphics.getWidth(), love.graphics.getHeight()
local dirX, dirY = 10, 10
local theImage, x, y
local theAngle = 0
function love.load()
theImage = love.graphics.newImage("myImage.png")
x = 50
y = 50
c
 
Search WWH ::




Custom Search