Jump to content

Movement in a circle


iPanda

Recommended Posts

local rotation = 0 
function yourFunction () 
dxDrawImage ( screenWidth/2 - 50, 0, 100, 240, 'image.png', rotation, 0, 0 ) 
rotation = rotation + 1 
    if ( rotation >= 359 ) then 
        rotation = 0 
    end 
end 
addEventHandler("onClientRender",root,yourFunction) 

Link to comment

Yes, i didn't read the topic correctly or maybe at all i just saw the image

anyway, try something like that not tested but maybe it will give you a light to start

  
    rotation = rotation + 1 
    if rotation > 359 then rotation = 0 end 
    setElementRotation(objectname,0,0,rotation) 
  

Link to comment

I went ahead and wrote a little interactive example for you. I tried to comment it all the best I could, hopefully you can learn from it. :)

Basically, what you need is trigonometric calculations. Trigonometry is very confusing at first, but once you start picking it up it's a lot of fun as you can make a lot of interesting stuff with it. If you want to read up on trigonometry, I would highly recommend the video lessons available for free over at Khan Academy.

How To Use:

There are two commands to use this script, so if you wanna change values you have to do that in the script itself. The variables should be rather self-explanatory.

Type /SpawnObject to spawn the two objects (center and outer)

Type /ToggleCamera to toggle an overhead camera based on the center object's position

Client.lua

-- General Variables 
local screenW, screenH = guiGetScreenSize() 
  
-- Gameplay Variables 
local cameraOverheadViewActive = false 
  
local circularRadius = 5 
  
local centerObject_Model = 1337 
local centerObject_Element = nil 
local centerObject_Data = {} 
  
local outerObject_Model = 1337 
local outerObject_Element = nil 
local outerObject_Data = {} 
  
-- Utility Functions 
function getPositionFromElementOffset(targetElement, offsetX, offsetY, offsetZ) 
    if(isElement(targetElement) == true) then 
        local theMatrix = getElementMatrix(targetElement)  -- Get the matrix 
        local posX = offsetX * theMatrix[1][1] + offsetY * theMatrix[2][1] + offsetZ * theMatrix[3][1] + theMatrix[4][1] 
        local posY = offsetX * theMatrix[1][2] + offsetY * theMatrix[2][2] + offsetZ * theMatrix[3][2] + theMatrix[4][2] 
        local posZ = offsetX * theMatrix[1][3] + offsetY * theMatrix[2][3] + offsetZ * theMatrix[3][3] + theMatrix[4][3] 
        return posX, posY, posZ 
    else 
        return false 
    end 
end 
  
-- Actual Code 
local gui_AngleValueLabel = guiCreateLabel(0.0140625*screenW, 0.4240740740740741*screenH, 0.1869791666666667*screenW, 0.0138888888888889*screenH, "Current Angle: 0", false) 
    guiSetFont(gui_AngleValueLabel, "default-bold-small") 
    guiLabelSetHorizontalAlign(gui_AngleValueLabel, "center", false) 
    guiLabelSetVerticalAlign(gui_AngleValueLabel, "center") 
local gui_AngleValueScroller = guiCreateScrollBar(0.0119791666666667*screenW, 0.437962962962963*screenH, 0.1942708333333333*screenW, 0.0185185185185185*screenH, true, false) 
  
function SpawnCenterObject() 
    -- Center Object Creation 
    local spawnX, spawnY, spawnZ = getPositionFromElementOffset(localPlayer, 0, 5, 0)   -- Get a 3D Vector(X, Y, Z) in front of the player 
    centerObject_Element = createObject(centerObject_Model, spawnX, spawnY, spawnZ)     -- Create the center object 
    table.insert(centerObject_Data, {spawnX, spawnY, spawnZ})                           -- Update the center object's data table with the position 
    -- Outer Object Creation - this will rotate around the center object 
    local spawnX, spawnY, spawnZ = getPositionFromElementOffset(localPlayer, 0, 9, 0)   -- Again get a 3D Vector(X, Y, Z) in front of the player, this one is further away 
    outerObject_Element = createObject(centerObject_Model, spawnX, spawnY, spawnZ)      -- Create the outer object (This is the one that will rotate) 
    table.insert(outerObject_Data, {spawnX, spawnY, spawnZ})                            -- Update the outer object's data table with the position 
    local spawnX, spawnY, spawnZ = getPositionFromElementOffset(localPlayer, 0, 5, 0)   -- Get a 3D Vector(X, Y, Z) in front of the player 
    showCursor(true)                                                                    -- Toggle the cursor so you can interact with the scroller 
end 
addCommandHandler("SpawnObject", SpawnCenterObject, false) 
  
function MoveTheObject(scrolledElement) 
    -- Calculate the new angle. A Scrollbar has a default range of 0.0 to 100.0 
    -- If we multiply this by 3.6, we get a range of 0.0 to 360.0 
    local theAngle = math.rad(guiScrollBarGetScrollPosition(scrolledElement)*3.6)                                   -- Calculate the value and store it in a local variable (theAngle) 
    guiSetText(gui_AngleValueLabel, tostring(math.floor(guiScrollBarGetScrollPosition(scrolledElement)*3.6)))       -- Update the GUI Label with the new angle 
    outerObject_Data[1][1] = centerObject_Data[1][1]+math.sin(theAngle)*circularRadius                              -- Take the center object's X Position and add the sine value of the current angle. Multiply with the radius 
    outerObject_Data[1][2] = centerObject_Data[1][2]+math.cos(theAngle)*circularRadius                              -- Take the center object's Y Position and add the cosine value of the current angle. Multiply with the radius (Those two together forms the "circular path") 
    setElementPosition(outerObject_Element, outerObject_Data[1][1], outerObject_Data[1][2], outerObject_Data[1][3]) -- Update the outer object's position 
end 
addEventHandler("onClientGUIScroll", root, MoveTheObject) 
  
function ToggleOverheadCameraView() 
    if(cameraOverheadViewActive == false) then 
        cameraOverheadViewActive = true 
        local cameraX, cameraY, cameraZ = getPositionFromElementOffset(localPlayer, 0, 5, 20) 
        setCameraMatrix(cameraX, cameraY, cameraZ, cameraX, cameraY, cameraZ-20) 
    else 
        cameraOverheadViewActive = false 
        setCameraTarget(localPlayer) 
    end 
end 
addCommandHandler("ToggleCamera", ToggleOverheadCameraView, false) 

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...