new guy with some noob qustions

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
zarko
Prole
Posts: 1
Joined: Wed May 16, 2012 12:25 am

new guy with some noob qustions

Post 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.
User avatar
nevon
Commander of the Circuloids
Posts: 938
Joined: Thu Feb 14, 2008 8:25 pm
Location: Stockholm, Sweden
Contact:

Re: new guy with some noob qustions

Post 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.
User avatar
richapple
Citizen
Posts: 65
Joined: Sun Dec 25, 2011 10:25 am

Re: new guy with some noob qustions

Post 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.
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: new guy with some noob qustions

Post 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.
When I write def I mean function.
User avatar
Nixola
Inner party member
Posts: 1949
Joined: Tue Dec 06, 2011 7:11 pm
Location: Italy

Re: new guy with some noob qustions

Post by Nixola »

It calls it 2 times, it also calls math.randomseed(os.time()), in love.run
lf = love.filesystem
ls = love.sound
la = love.audio
lp = love.physics
lt = love.thread
li = love.image
lg = love.graphics
10$man
Citizen
Posts: 77
Joined: Sun Apr 22, 2012 10:40 pm

Re: new guy with some noob qustions

Post 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.
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 12 guests