I want to calculate a 3D position using lua, there's some built in functions and I have code to move the player forwards, but I want to move the player backwards.
local x, y, z = getElementPosition ( localPlayer )
local r = getPedRotation ( localPlayer )
x = x - math.sin ( math.rad ( r ) ) * 1.5
y = y + math.cos ( math.rad ( r ) ) * 1.5
setElementPosition ( localPlayer, x, y, z )
setElementRotation ( localPlayer, 0, 0, r )
To explain the above, getElementPosition returns 3 values, x, y and z... getPedRotation returns one, the rotation of the ped (in this case the player) and localPlayer is the player, the rest is pretty clear, setElementPosition sets the players position, same for rotation...
So in my mind the following should work to make the player move back
local x, y, z = getElementPosition ( localPlayer )
local r = getPedRotation ( localPlayer )
x = x + math.sin ( math.rad ( r ) ) * 1.5
y = y - math.cos ( math.rad ( r ) ) * 1.5
setElementPosition ( localPlayer, x, y, z )
setElementRotation ( localPlayer, 0, 0, r )
It doesn't work unfortunately, and no matter if I add/subtract 180º to "r" it still does the same, the script is for MTA San Andreas, so if anyone can see where I've went wrong would be nice of you to tell me, was never good at math, but learning lots by doing little things like this.
Thanks in advance.
Related
I would like to know how to set an objects x, y and z co-ordinates. could the solution also involve compatability for
math.random(0, 100)
thanks!
From https://developer.roblox.com/en-us/api-reference/property/BasePart/Position
local part = workspace.Part
part.Position = part.Position + Vector3.new(5, 20, 100)
https://developer.roblox.com/en-us/articles/positioning-objects
local x = math.random(0,100)
local y = math.random(0,100)
local z = math.random(0,100)
-- for debugging, if needed:
print(x, y, z)
local object = workspace.Part
object.Position = Vector3.new(x, y, z)
Using this:
local W, H = 100, 50
function love.draw()
love.graphics.translate(love.graphics.getWidth()/2,love.graphics.getHeight()/2)
for i = 1, 360 do
local I = math.rad(i)
local x,y = math.cos(I)*W, math.sin(I)*H
love.graphics.line(0, 0, x, y)
end
end
I can connect a line with the center of an ellipse (with length W and height H) and the edge. How do you 'rotate' the ellipse around it's center, with a parameter R? I know you can sort of do it with love.graphics.ellipse and love.graphics.rotate but is there any way I can get the coordinates of the points on a rotated ellipse?
This is a Trigonometry problem, here is how the basic 2D rotation work. Imagine a point located at (x,y). If you want to rotate that point around the origin(in your case 0,0) by the angle θ, the coordinates of the new point would be located at (x1,y1) by using the following transformation
x1 = xcosθ − ysinθ
y1 = ycosθ + xsinθ
In your example, I added a new ellipse after rotations
function love.draw()
love.graphics.translate(love.graphics.getWidth()/2,love.graphics.getHeight()/2)
for i = 1, 360, 5 do
local I = math.rad(i)
local x,y = math.cos(I)*W, math.sin(I)*H
love.graphics.setColor(0xff, 0, 0) -- red
love.graphics.line(0, 0, x, y)
end
-- rotate by angle r = 90 degree
local r = math.rad(90)
for i = 1, 360, 5 do
local I = math.rad(i)
-- original coordinates
local x = math.cos(I) * W
local y = math.sin(I) * H
-- transform coordinates
local x1 = x * math.cos(r) - y * math.sin(r)
local y1 = y * math.cos(r) + x * math.sin(r)
love.graphics.setColor(0, 0, 0xff) -- blue
love.graphics.line(0, 0, x1, y1)
end
end
I'm having a mathematical issue that I can't get through.
I'm making a model browser in Lua where mouse movement is hooked to camera position, camera position has 2 modes: fixed and free, free works flawlessly whereas fixed seems to have issue with calculating proper Z.
X and Y is calculated properly and works without any issue, but Z seems to scale with X, Y way too much as seen here: http://puu.sh/oTN1v/5846343f82.webm (These camera warps happen whenever I click right mouse button, which happens even if I don't move mouse )
function self:RightMouseClick()
local cx, cy = mousepos()
local radius = math.sqrt( math.pow( campos.x, 2 ) + math.pow( campos.y, 2 ) )
local ang = ( camorigin - campos ):Angle()
function self:Think()
if input.IsMouseDown( MOUSE_RIGHT ) then
local x = camorigin.x + radius * math.cos( math.rad( 1 ) * ( 180 + ang.yaw + ( cx - mousex() ) * 0.5 ) )
local y = camorigin.y + radius * math.sin( math.rad( 1 ) * ( 180 + ang.yaw + ( cx - mousex() ) * 0.5 ) )
local z = camorigin.z + radius * math.sin( math.rad( 1 ) * ( ang.pitch + ( cy - mousey() ) * 0.5 ) )
campos = Vector( x, y, z )
end
end
end
#Edit: If you have no clue what this code means, you might aswell just tell me how to properly calculate Z for camera movement around axis
Removing camorigin from calculation of x, y, z worked out.
I made a equilateral triangle:
local W = 540
local H = 700
local r = 150
local r_x = math.sqrt(math.pow(r,2)-math.pow((r/2),2))
local vertices = { 0,-r, r_x , r/2 , -r_x , r/2 }
local block = display.newPolygon( W, H, vertices )
physics.addBody( block )
physics.setGravity(0,0)
block.angularVelocity= 100
I want the triangle to rotate on its axis but it does not happen. why?
I thinks it related to the object's anchor but I dont know how to fix this.
Try yourself if you don't understand what I mean.
Just take all the objects and insert them into a group. Then just rotate the group using the rotate API.
I managed to make points via the print function appear in a circle shape and animated it to go in a constant rotation with the circle variable. However my attempt to auto arrange the points into a 1/number of points segments of the circle to make them laid out evenly without inputting the angle of each one seems instead to make them go around far more than 360 degrees around the circle, as it feeds into itself.
For example for 5 points I'd want each circle in 1/5th of the 360 degrees with even space on each side, which should make a regular pentagon shape if you joined up the dots
function love.load() --Only run at startup
cycle = 0
points = 9 -- should work on any value
radius = 0.5
love.window.setMode(90, 90)
end
function love.update()
cycle = cycle + 0.05
if cycle >= 360 then
cycle = 0
--prevent huge values
end
end
function love.draw()
i = 0
while i < points do
x = radius * math.deg(math.sin(cycle + (360 * (i / points )) ) )
y = radius * math.deg(math.cos(cycle + (360 * (i / points )) ) )
--cycle to move and i + 1 / points to auto arrange
b = (i / points )
b = round(b, 2)
love.graphics.print( b , 33 + x, 33 + y)
i = i + 1
end
end
function round(num, idp) --rounding function for display
local mult = 10^(idp or 0)
return math.floor(num * mult + 0.5) / mult
end
What currently happens:
In your loop, you're doing
x = radius * math.deg(math.sin(cycle + (360 * (i / points )) ) )
y = radius * math.deg(math.cos(cycle + (360 * (i / points )) ) )
whereas, you want to have:
b = i / points
local c = cycle + (360 * b) -- to lessen the computation cost
x = radius * math.sin( math.rad(c) )
y = radius * math.cos( math.rad(c) )
You just needed to convert that 360 to radians using the lua math.rad() function, as lua's math.sin(O) and math.cos() functions go by radians rather than degrees. the excalamation point have nothing to do with code and are just there to grab your attention to the problem.
x = math.deg(radius*math.sin(cycle + (!!!math.rad(360)!!! * (i / points )) ) )
y = math.deg(radius*math.cos(cycle + (!!!math.rad(360)!!! * (i / points )) ) )
You need to use cos for x and sin for y to get the right order:
x = radius * math.deg(math.cos(cycle + (360 * (i / points )) ) )
y = radius * math.deg(math.sin(cycle + (360 * (i / points )) ) )