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

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
Xx_MaskedIdiot_xX
Prole
Posts: 2
Joined: Sat Mar 12, 2022 12:45 am

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

Post 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 99 times
User avatar
nehu
Prole
Posts: 11
Joined: Fri Feb 25, 2022 1:12 am
Location: Argentina, Maybe under your bed

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

Post 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:

Code: Select all

for i, v in pairs(problems) do
	fix(v)
end
Post Reply

Who is online

Users browsing this forum: No registered users and 3 guests