Code: Select all

```
x=math.random(10)
print (x)
```

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.

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

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.

Code: Select all

```
x=math.random(10)
print (x)
```

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.

- nevon
- Commander of the Circuloids
**Posts:**938**Joined:**Thu Feb 14, 2008 8:25 pm**Location:**Stockholm, Sweden-
**Contact:**

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:

As for the arc, that will require you to use a quadratic function.

Code: Select all

```
math.randomseed(os.time)
math.random()
math.random()
math.random()
```

That's because of included in Luazarko wrote:x will always be 9Code: Select all

`x=math.random(10) print (x)`

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.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?

- kikito
- Inner party member
**Posts:**3153**Joined:**Sat Oct 03, 2009 5:22 pm**Location:**Madrid, Spain-
**Contact:**

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:

As for the arc, that will require you to use a quadratic function.Code: Select all

`math.randomseed(os.time) math.random() math.random() math.random()`

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**.

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

ls = love.sound

la = love.audio

lp = love.physics

lt = love.thread

li = love.image

lg = love.graphics

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.

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.

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