Rotation gone wrong
Hello, I am making a shooter game and what has happened is that I made the gun and player successfully, but the gun is rotating around its own center point. But I want it to rotate around the player. How do I do that?
Hello, I am making a shooter game and what has happened is that I made the gun and player successfully, but the gun is rotating around its own center point. But I want it to rotate around the player. How do I do that?
You need to rotate around the central axis (the player's position) by a certain angle and radius.
init = function()
screen.setAlpha( 0.5 )
screen.setColor("#FFFFFF")
angle = 0
player = object
x = 0
y = 0
angle = 0
arm = 50
arm_x = 0
arm_y = 0
icon = 70
icon_x = 0
icon_y = 0
end
end
update = function()
player.x += random.nextInt( 2 ) * 2 - 1
player.y += random.nextInt( 2 ) * 2 - 1
player.angle += 1
player.arm_x = player.x + player.arm * sind( player.angle )
player.arm_y = player.y + player.arm * cosd( player.angle )
player.icon_x = player.x + player.icon * sind( player.angle )
player.icon_y = player.y + player.icon * cosd( player.angle )
end
draw = function()
screen.clear()
screen.drawRound( player.x, player.y, 20, 20 )
screen.drawRound( player.arm_x, player.arm_y, 10, 10 )
screen.drawLine( player.x, player.y, player.arm_x, player.arm_y)
screen.setDrawRotation( -player.angle )
screen.drawSprite("icon", player.icon_x, player.icon_y, 10, 10 )
end
can you please explain this code to me? I just wanted to understand what was going on
//-- Initialize function to set up initial parameters and variables
init = function()
//-- Set screen transparency to 0.5 (50%)
screen.setAlpha(0.5)
//-- Set screen color to white
screen.setColor("#FFFFFF")
//-- Initialize angle variable to 0
angle = 0
//-- Initialize player object with properties
player = object
x = 0 //-- Initial x-coordinate of player
y = 0 //-- Initial y-coordinate of player
angle = 0 //-- Initial angle of player
arm = 50 //-- Length of player's arm
arm_x = 0 //-- Initial x-coordinate of arm
arm_y = 0 //-- Initial y-coordinate of arm
icon = 70 //-- Length of player's icon
icon_x = 0 //-- Initial x-coordinate of icon
icon_y = 0 //-- Initial y-coordinate of icon
end
end
//-- Update function to handle changes to player's position and angle
update = function()
//-- Randomly adjust player's x and y coordinates
player.x = player.x + random.nextInt(2) * 2 - 1
player.y = player.y + random.nextInt(2) * 2 - 1
//-- Increment player's angle by 1 degree
player.angle = player.angle + 1
//-- Calculate new coordinates for arm based on player's angle
player.arm_x = player.x + player.arm * sind(player.angle)
player.arm_y = player.y + player.arm * cosd(player.angle)
//-- Calculate new coordinates for icon based on player's angle
player.icon_x = player.x + player.icon * sind(player.angle)
player.icon_y = player.y + player.icon * cosd(player.angle)
end
//-- Draw function to render player and related elements on screen
draw = function()
//-- Clear the screen
screen.clear()
//-- Draw a round shape representing the player
screen.drawRound(player.x, player.y, 20, 20)
//-- Draw a round shape representing the player's arm
screen.drawRound(player.arm_x, player.arm_y, 10, 10)
//-- Draw a line connecting player's position to the end of the arm
screen.drawLine(player.x, player.y, player.arm_x, player.arm_y)
//-- Set the draw rotation to the negative of player's angle
screen.setDrawRotation(-player.angle)
//-- Draw a sprite (icon) representing the player's icon
screen.drawSprite("icon", player.icon_x, player.icon_y, 10, 10)
end
copy the code to a new project and run .
Code lines 35 and 36 are responsible for the rotation around the point where you are playing for the account value and the radius length.
Line 50 is responsible for providing the screen
object with information that now all graphic elements are to be drawn at the angle we pass in the variable.
Line 52 is responsible for drawing the icon (weapon) at the screen location (coordinates) you calculated in lines 35 and 36.
so how do i make the weapon point to mouse pointer while maintaining an orbit around player?
//-- Initialize function to set up initial parameters and variables
init = function()
//-- Set screen transparency to 0.5 (50%)
screen.setAlpha(0.5)
//-- Set screen color to white
screen.setColor("#FFFFFF")
//-- Initialize angle variable to 0
angle = 0
//-- Initialize player object with properties
player = object
x = 0 //-- Initial x-coordinate of player
y = 0 //-- Initial y-coordinate of player
angle = 0 //-- Initial angle of player
arm = 50 //-- Length of player's arm
arm_x = 0 //-- Initial x-coordinate of arm
arm_y = 0 //-- Initial y-coordinate of arm
icon = 70 //-- Length of player's icon
icon_x = 0 //-- Initial x-coordinate of icon
icon_y = 0 //-- Initial y-coordinate of icon
icon_angle = 0
end
end
//-- Update function to handle changes to player's position and angle
update = function()
//-- Randomly adjust player's x and y coordinates
player.x = player.x + random.nextInt(2) * 2 - 1
player.y = player.y + random.nextInt(2) * 2 - 1
//-- Increment player's angle by 1 degree
player.angle = player.angle + 1
//-- Calculate new coordinates for arm based on player's angle
player.arm_x = player.x + player.arm * sind(player.angle)
player.arm_y = player.y + player.arm * cosd(player.angle)
//-- Calculate new coordinates for icon based on player's angle
player.icon_x = player.x + player.icon * sind(player.angle)
player.icon_y = player.y + player.icon * cosd(player.angle)
player.icon_angle = atan2d( player.icon_x - mouse.x, player.icon_y - mouse.y )
end
//-- Draw function to render player and related elements on screen
draw = function()
//-- Clear the screen
screen.clear()
screen.setDrawRotation( 0 )
//-- Draw a round shape representing the player
screen.drawRound(player.x, player.y, 20, 20)
//-- Draw a round shape representing the player's arm
screen.drawRound(player.arm_x, player.arm_y, 10, 10)
//-- Draw a line connecting player's position to the end of the arm
screen.drawLine(player.x, player.y, player.arm_x, player.arm_y)
//-- Set the draw rotation to the negative of player's angle
screen.drawRect( mouse.x, mouse.y, 10, 10, "red")
screen.drawLine( mouse.x, mouse.y, player.icon_x, player.icon_y, "green")
screen.setDrawRotation( -player.icon_angle )
//-- Draw a sprite (icon) representing the player's icon
screen.drawSprite("icon", player.icon_x, player.icon_y, 10, 50)
end
Thank you so much for the help!