# physics help needed , bouncing ball

• (2 Pages)
• 1
• 2

## 21 Replies - 1825 Views - Last Post: 07 September 2013 - 04:59 PM

### #1 bboyzeez

Reputation: 4
• Posts: 143
• Joined: 16-December 12

# physics help needed , bouncing ball

Posted 07 September 2013 - 03:49 AM

hi all first of all , i didnt see a javascript area so thought as my question is general id go with this area(if needs moving sorry)

i have also asked on a physics forum to help me with the equations but i think maybe my problem now is more coding than the formulas themselves..

so i have a ball that drops from static position here is my working out

ball(mass) = 5.0
gravity = 9.81
starting height = 30
COR = 0.75
downward velocity = 24.26108
upward velocity = 18.19581
newHeight = 16.875

now first can i check this is right...

1. ball mass is 5.0 would this be seen as 5kg or 5g?
2. the starting position of the ball on yaxis is 30 so would this be 30meters?

ok so i have this code and i know the update is called 60 times a second so my equation for the velocity falling is:

velocity = 0.5 * (Mathf.Pow((gravity * mass) * Time.time/60,2));

this shows the ball build up speed as it falls

then it hits floor which i use the above working outs to give my new velocity up 18.19581 in this equation:

velocity = 0.5 * (Mathf.Pow(upVelocity * Time.time/60,2));

but this is my problem i want the ball to lose speed so when it reaches its next height it comes to an almost stop before starting to fall instead my ball is like UP/DOWN/UP/DOWN very erratic.

am i missing a formula where speed loses over time depending on the height its having to reach somehow?

im learning physics to so i know as im in unity i could just add rigidbody and apply gravity and let it do it for me i want to understand this .thanks

Is This A Good Question/Topic? 0

## Replies To: physics help needed , bouncing ball

### #2 andrewsw

• I'm not here to twist your niblets

Reputation: 4451
• Posts: 16,299
• Joined: 12-December 12

## Re: physics help needed , bouncing ball

Posted 07 September 2013 - 03:58 AM

Moved to the Javascript forum.

BTW Java and Javascript are completely different languages, posting in Web Development or similar would have been a better alternative.

(If you click the Forums button at the top left and scroll down the Javascript forum is clearly listed.)

### #3 bboyzeez

Reputation: 4
• Posts: 143
• Joined: 16-December 12

## Re: physics help needed , bouncing ball

Posted 07 September 2013 - 04:57 AM

sorry my mistake i using javascript for game development so didnt think to look in web development now i know where its mainly used

back to my problem i think what im after is i need to slow the velocity down

so i know how far it has to travel and i know the starting velocity...what im stuck with is if i knew the time it would take then i could use this forumal to give my constantly decline in velocity :

a = (Va - Vb) / Time

am i on the right tracks?

### #4 Dormilich

• 痛覚残留

Reputation: 3742
• Posts: 11,320
• Joined: 08-June 10

## Re: physics help needed , bouncing ball

Posted 07 September 2013 - 05:04 AM

Quote

1. ball mass is 5.0 would this be seen as 5kg or 5g?
2. the starting position of the ball on yaxis is 30 so would this be 30meters?

the SI units are kg and m.

Quote

ok so i have this code and i know the update is called 60 times a second so my equation for the velocity falling is:

velocity = 0.5 * (Mathf.Pow((gravity * mass) * Time.time/60,2));

look at the units: (m/s2 * kg * s)2 = kg2 m2 / s2. definitely not a velocity.

similarly, the second equation fails equally.

besides that, the equation for the velocity is much more simple:

v(t) = v(0) + a * t (acceleration)
v(t) = v(0) – a * t (deceleration)

### #5 bboyzeez

Reputation: 4
• Posts: 143
• Joined: 16-December 12

## Re: physics help needed , bouncing ball

Posted 07 September 2013 - 05:15 AM

The equation i used accelerates over time right?
I'm sorry I don't understand the v(t) , v(0) and t(I)??

The equation I was following was h= h0 - 1/2gt(to the power of 2)

This post has been edited by Dormilich: 07 September 2013 - 05:23 AM

### #6 Dormilich

• 痛覚残留

Reputation: 3742
• Posts: 11,320
• Joined: 08-June 10

## Re: physics help needed , bouncing ball

Posted 07 September 2013 - 05:22 AM

bboyzeez, on 07 September 2013 - 02:15 PM, said:

I'm sorry I don't understand the v(t) , v(0) and t(I)??

v(0) velocity at t = 0 (i.e. starting velocity)
v(t) velocity at time t (i.e. current velocity)
t(I) definitely not used by me

bboyzeez, on 07 September 2013 - 02:15 PM, said:

The equation I was following was h= h0 - 1/2gt(to the power of 2)

h(t) = h(0) – 1/2 g t^2, the square goes only on the time.

or in Javascript:
```var h = h0 - 0.5 * g * Math.pow(t, 2);
```

### #7 Dormilich

• 痛覚残留

Reputation: 3742
• Posts: 11,320
• Joined: 08-June 10

## Re: physics help needed , bouncing ball

Posted 07 September 2013 - 05:35 AM

note: the basic equation to solve is ΔE(kin) = ΔE(pot) (the use of the difference is important, as it lets you use non-zero starting values for height and velocity)

• Cheese and Sprinkles

Reputation: 464
• Posts: 1,984
• Joined: 30-April 10

## Re: physics help needed , bouncing ball

Posted 07 September 2013 - 10:29 AM

I am not that great at physics but I ran across this site not to long ago and thought it may be of use to you now.

Physicsclassroom.com

### #9 bboyzeez

Reputation: 4
• Posts: 143
• Joined: 16-December 12

## Re: physics help needed , bouncing ball

Posted 07 September 2013 - 11:06 AM

ok so if i use this code

var h = h0 - 0.5 * g * Math.pow(t, 2);

that will bring the ball down to hit the floor

so when i use the formulas in first post to calculate the downward velocity and then the upvelocity to find the height of next bounce

so i dont see how i insert the upward velocity into this
var h = h0 - 0.5 * g * Math.pow(t, 2);

in order to move it upwards and slow down to a stop before it drops again?

### #10 bboyzeez

Reputation: 4
• Posts: 143
• Joined: 16-December 12

## Re: physics help needed , bouncing ball

Posted 07 September 2013 - 11:22 AM

also for time i am using Time.time/60 or should i be using deltaTime?

### #11 Dormilich

• 痛覚残留

Reputation: 3742
• Posts: 11,320
• Joined: 08-June 10

## Re: physics help needed , bouncing ball

Posted 07 September 2013 - 11:41 AM

Quote

so i dont see how i insert the upward velocity into this
var h = h0 - 0.5 * g * Math.pow(t, 2);

set v(0) to something different than 0 (which is why you do not see it in this equation).

if you’re comfortable with Maths, I recommit to post #7.

Quote

also for time i am using Time.time/60 or should i be using deltaTime?

a ) it depends on the units you want to use (the equation doesn’t care if you measure time in seconds, minutes or years, but you will receive according changes in the values in turn)

b ) what is deltaTime?

Quote

so when i use the formulas in first post to calculate the downward velocity and then the upvelocity to find the height of next bounce

you don’t need any of those formulas to calculate the height of the next bounce. they’re 22.5, 16.9, 12.7, 9.5, 7.1, 5.3 and so on.

This post has been edited by Dormilich: 07 September 2013 - 11:42 AM

### #12 bboyzeez

Reputation: 4
• Posts: 143
• Joined: 16-December 12

## Re: physics help needed , bouncing ball

Posted 07 September 2013 - 12:35 PM

ok deltatime is the time since last frame...which im thinking is not right one for this

here is my coding so if you throw that onto an object your see how it goes and ur see it just doesnt come to a stop at its next heights before it starts to fall down again

```function Start()
{
startingHeight = transform.position.y;
}
function Update ()
{

newTime =  Time.time - currentTime;							//reset the time eveytime it starts to fall?

velocity += gravity_g * Mathf.Pow(newTime/60,2);

if(isFalling && startingHeight >0.02)
{
if(transform.position.y > Floor)
transform.Translate(Vector3(0,-velocity,0));

else if(transform.position.y <= Floor)
{
if(startingHeight <=0.5 )
{
transform.position.y = Floor;
print("Finished");
isFalling = false;
bounce = false;
}
else
{
downVel = Mathf.Sqrt((2 * (gravity_g * startingHeight)));
newVel = (COR * downVel);
startingHeight = (Mathf.Pow(newVel,2)) / (2 * gravity_g);
isFalling = false;

bounce = true;
}
}
}

if(bounce)
{

velocity -= gravity_g * Mathf.Pow(Time.time/60,2);

if(transform.position.y <0)
transform.position.y = 0;

transform.Translate(Vector3(0,velocity,0));

if(transform.position.y >= startingHeight)
{

bounce = false;
isFalling = true;
newTime = Time.time - currentTime;

}

}

}

```

This post has been edited by bboyzeez: 07 September 2013 - 12:37 PM

### #13 Dormilich

• 痛覚残留

Reputation: 3742
• Posts: 11,320
• Joined: 08-June 10

## Re: physics help needed , bouncing ball

Posted 07 September 2013 - 12:56 PM

first question on the code, what is it that you’re supposed to output (or which object do you need to modify)?

### #14 bboyzeez

Reputation: 4
• Posts: 143
• Joined: 16-December 12

## Re: physics help needed , bouncing ball

Posted 07 September 2013 - 02:50 PM

oh since i put this up i think i have the code working now i just adjusting the logic but ill repost in a few minutes when i think it should work

### #15 bboyzeez

Reputation: 4
• Posts: 143
• Joined: 16-December 12

## Re: physics help needed , bouncing ball

Posted 07 September 2013 - 03:16 PM

ok the first fall and bounce speed seems to be working but if you look the next height should be 22.5 but when it bounces up its getting to roughly 15 and then losing velocity and starts falling back down....is this losing speed to fast if so how do i work it out?

```#pragma strict

var Floor : float = 0;
var CubeWeight : float = 5;
var velocity : float =0;

var acceleration : float;
var gravity : float = 9.81;

var isFalling : boolean = true;
var isBouncingUp : boolean = false;
var hasBounced : boolean = false;
var startingHeight : float = 30;
var nextisBouncingUpHeight : float;
var downVel : float=0;
var newVel : float = 0;
var COR : float =0.75;
var currentTime : float = 0;
var newTime : float;

function Start()
{
startingHeight = transform.position.y;
acceleration = gravity * CubeWeight;
}

function Update ()
{
newTime =  Time.time - currentTime;							//reset the time eveytime it starts to fall?
if(isFalling)
{

if(transform.position.y > Floor)						// if the ball is higher than the floor
{
velocity += acceleration * Mathf.Pow(newTime/60,2);
transform.Translate(Vector3(0,-velocity,0));
}

else if(transform.position.y <= Floor)					// if the ball has hit the floor
{
startingHeight = startingHeight * COR;
hasBounced= true;
isFalling = false;
isBouncingUp = true;

}

if(velocity <= 0 && hasBounced == true)					//if its come to a stop
{
transform.position.y = Floor;
print("Finished");
isFalling = false;
isBouncingUp = false;
}
}

if(isBouncingUp)
{
velocity -= acceleration * Mathf.Pow(Time.time/60,2);
transform.Translate(Vector3(0,velocity,0));

if(transform.position.y >= startingHeight)
{

isBouncingUp = false;
isFalling = true;
currentTime = Time.time;

}

}
}

```

• (2 Pages)
• 1
• 2

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }