Page 1 of 1

I need an orbit type thing for a game I'm working on.

Posted: Fri Mar 18, 2022 8:24 pm
by Xx_MaskedIdiot_xX
Can someone please help me with making a UFO orbit a planet. I need it so the the ufo will obit the planet when the scroll wheel in spun.
main.lua
(561 Bytes) Downloaded 107 times

Re: I need an orbit type thing for a game I'm working on.

Posted: Sat Mar 19, 2022 12:49 am
by nehu
Hi!
For making the ufo orbit over the planet we need to use the most people's worst enemy... :o:
MATH
anyways, i will leave the code and at the end the article i used for making this.

Code: Select all


function love.load()
    
    ...    
    
    ufo = {} --we define our ufos data
    ufo.radius = 100 --radius of the circle
    ufo.x = 0 --vertical positon of the ufo
    ufo.y = 0 --horizontal postion of the ufo
    ufo.angle = 0 --initial angle
    ufo.image = love.graphics.newImage('ufo.png') --image for drawing
    ufo.speed = 0 --speed of movement

end

function love.update(dt)
    
    ufo.angle = ufo.angle + ufo.speed*dt --this changes the angle based on the speed of the ufo

    ufo.x = love.graphics.getWidth()/2 + math.cos(ufo.angle)*ufo.radius; --heres the math stuff for x
    ufo.y = love.graphics.getHeight()/2 + math.sin(ufo.angle)*ufo.radius; --and for y

    ufo.speed = ufo.speed * 0.9 --makes the ufo have a S M O O T H effect, so it looks NICE

end

function love.draw()

    ...
    
    love.graphics.draw(ufo.image,ufo.x,ufo.y,0,2,2,16,16) --here we draw our ufo! :D

end

function love.wheelmoved(x, y)

     ...
	
     ufo.speed = ufo.speed + y*2 --this changes the ufo speed, so you actually can move it
end


ok, so... :awesome:
the load function makes a table that contains the ufo data,
the update function changes the ufo angle based on the speed it is going, uses math for moving the ufo and finally multiplies the ufo speed than 0.9, so it has a cool ice effect
the draw function draws the ufo, in my case i used an example image, but you can use

Code: Select all

love.graphics.circle('fill',ufo.x,ufo.y,10)
or whatever you want
and finally, the wheelmoved function, we change the speed of the ufo based on the y velocity of the mouse.
also, you used

Code: Select all

if y > 0 or y < 0 then

        ...

    end
i dont know if there is some reason to check if the y is not 0, since that function only is called when the wheel is moved (y will never be 0, otherwise the fucntion is not called), but if theres no reason, then you dont need to check if y is not 0 :huh:

anyways, heres the article i used for making the ufo orbit(aka: MATHS): https://gamedev.stackexchange.com/quest ... cular-path

hope it helped! :crazy: