Page 1 of 1

[Fixed] Simple sine/radians problem - I think

Posted: Mon Sep 20, 2021 12:38 pm
by togFox
I got this off a random 2 year old github and found a bug almost instantly. A tiny 100 line file is attached. This is the old lunar lander 'game' but this one line seems to bug out:

Code: Select all

  if love.keyboard.isDown("up") then
    Lander.engineOn = true
    local angle_radian = math.rad(Lander.angle)
    local force_x = math.cos(angle_radian) * (Lander.speed * dt)
    local force_y = math.sin(angle_radian) * (Lander.speed * dt)
	
-- this will sometimes be zero meaning zero force meaning the lander never slows down
print(force_y)
--

    Lander.vx = Lander.vx + force_x
    Lander.vy = Lander.vy + force_y

  else
    Lander.engineOn = false
  end
I think, with my basic knowledge - a certain combination of radian + sine will = zero and the ship can't break and can't land properly.

Use UP key for thrust and left/right key to rotate.

(actually kinda fun)


LunarLander.zip
(2.05 KiB) Downloaded 102 times
Edit: I made a zip file and not a .love file in case anyone thought this was 'production ready'. It's not. Just my own little habits I guess.

Re: Simple sine/radians problem - I think

Posted: Mon Sep 20, 2021 1:23 pm
by MrFariator
It's not really about the radians, but rather this peculiar line at the top of main.lua:

Code: Select all

Lander.speed = love.math.random(0,1)
For whatever reason, the lander's speed is set randomly at startup, and if love.math.random is used this way it will return only either 0 or 1 (as per wiki). As such, sometimes you can move the ship, sometimes you can't, because multiplying by zero is zero.

Re: Simple sine/radians problem - I think

Posted: Mon Sep 20, 2021 1:27 pm
by togFox
hmm. Interesting. I didn't see that line. I guess the intention was to spawn the lander with a random velocity to force an immediate reaction.

I shall review and fix. Thanks.

Re: [Fixed] Simple sine/radians problem - I think

Posted: Mon Sep 20, 2021 1:31 pm
by pgimeno
That's more like engine power anyway, not speed, since it applies to the force. Naming the variables appropriately can protect you from some bugs.

Re: [Fixed] Simple sine/radians problem - I think

Posted: Mon Sep 20, 2021 2:20 pm
by darkfrei
pgimeno wrote: Mon Sep 20, 2021 1:31 pm That's more like engine power anyway, not speed, since it applies to the force. Naming the variables appropriately can protect you from some bugs.
It can be something like specific impulse, that is like-a-speed and has unit as m/s (or just in seconds in other notation).

Re: [Fixed] Simple sine/radians problem - I think

Posted: Mon Sep 20, 2021 9:28 pm
by togFox
I've already converted some variable names from French to English. It clearly has bugs and is not finished. That said - beggars can't be choosers and it has given me a head start.