Page 1 of 1

new guy with some noob qustions

Posted: Sun May 20, 2012 8:21 am
by zarko
I have a couple of questions about Loved and Lua. First i'm having a problem with trying to get random numbers using Lua. Using Scite or using the Lua online demo, the math.random function seems broken. For example if i write something like

Code: Select all

x=math.random(10)
print (x)
x will always be 9

I'm making a simple game with cannons and i want the cannon ball to travel in a arc, not a straight line. How is this done, do i need to use complex math?

Thanks for your help.

Re: new guy with some noob qustions

Posted: Sun May 20, 2012 9:07 am
by nevon
math.random will return the same value the first couple of times you use it, for some reason. Try discarding a couple of random numbers after setting the seed:

Code: Select all

math.randomseed(os.time)
math.random()
math.random()
math.random()
As for the arc, that will require you to use a quadratic function.

Re: new guy with some noob qustions

Posted: Sun May 20, 2012 10:23 am
by richapple
zarko wrote:

Code: Select all

x=math.random(10)
print (x)
x will always be 9
That's because of included in Lua pseudo-random number generator (PRNG). The idea is very simple: the math.random returns a random number based on seed. For example default seed is 1. You execute a math.random(10) and it returns 9. Then you reopen the program and it will return the same number (basically what happened to you). Then if you seed it with math.randomseed() with for example 2 you will receive different number but this numer will be the same every time you restart your program. The best way is too seed the PRNG at the start of your program (LÖVE already does that, in love.run) Further reading: wikipedia

zarko wrote:I'm making a simple game with cannons and i want the cannon ball to travel in a arc, not a straight line. How is this done, do i need to use complex math?
I am sure that if you already implemented the straight moving of your cannon ball, you will just need to multiply the ball's y position every frame by an usuable amount.

Re: new guy with some noob qustions

Posted: Sun May 20, 2012 5:51 pm
by kikito
nevon wrote:math.random will return the same value the first couple of times you use it, for some reason. Try discarding a couple of random numbers after setting the seed:

Code: Select all

math.randomseed(os.time)
math.random()
math.random()
math.random()
As for the arc, that will require you to use a quadratic function.

I'm not 100% sure, but I think I saw somewhere that LÖVE 0.8 already called math.random several times after doing the ramdomseed. I might be wrong though.

Re: new guy with some noob qustions

Posted: Sun May 20, 2012 5:57 pm
by Nixola
It calls it 2 times, it also calls math.randomseed(os.time()), in love.run

Re: new guy with some noob qustions

Posted: Mon May 21, 2012 1:16 am
by 10$man
Hello! This is an answer regarding your question about a cannon game!
You could use a quadratic as nevon said, but there is much simpler ways that probably can be calculated much faster then squaring.
All you need is a variable that will be for the acceleration of x and a separate one for y.
Then every frame you subtract/add an amount to the acceleration variables then add those to the x and y positions of your cannon.

For example, when I launch my cannon ball, I will calculate the force and direction it should be traveling it, and set the acceleration variables likewise. Then for every frame I will subtract... let's say 0.3 off of the acceleration variable for Y to integrate gravity. Then I will subtract some amount from the X acceleration to integrate friction. That will give your cannon ball an arc. Just remember, the acceleration for X shouldn't go below 0 or else it will turn around, and the acceleration for Y shouldn't reach to high/low of a value or else collision checking will become unreliable and you'l get weird glitchy looking animation.