How do I conserve energy in a physics simulation

Get help using Construct 2

Post » Wed Jan 13, 2016 2:06 pm

Hi:

I'm trying to develop a physics simulation for educational purposes, the game consist in a energy conservation demostration (spring, platform, ball) , the problem is when i have friction=0 ,it suposes that the ball will oscillate forever but at some time it stops, somebody knows how to fix it?,maybe is a physics problem between me and game platform but i was trying a lot of options and i'm not so experienced guy with C2.

Thanks
here's capx link
bit.ly/1OhXRIQ
B
4
Posts: 11
Reputation: 222

Post » Wed Jan 13, 2016 8:01 pm

Physics engines usually do no conserve energy, there is loss over time. The reason is everything is an approximation. You can however keep the energy close to being constant by correcting the velocity.

So the equation is this:
E=KE+PE
E=0.5*m*v^2 + m*g*h

solved for v
v = sqrt((E-mgh)*2/m)

So the events to correct the velocity so energy is conserved would be:

global number total_energy=0
global number initial_y=0
global number speed=0
global number angleOfMotion=0

Start of layout
--- set initial_y to ball.y
--- set total_energy to ball.physics.mass*10*(480-ball.y)

ball: y<initial_y
--- ball: set y to initial_y

every tick
--- set speed to sqrt((total_energy-ball.physics.mass*10*(480-ball.y))*2/ball.physics.mass)
--- set angleOfMotion to angle(0,0,ball.physics.velocityX,ball.physics.velocityY)
--- ball: physics: set velocity to ( speed*cos(angleOfMotion), speed*sin(angleOfMotion) )
B
88
S
30
G
89
Posts: 5,112
Reputation: 60,335

Post » Wed Jan 13, 2016 11:18 pm

Thanks R0J0, almost clear, the only part that i don't understand is the following:

ball: y<initial_y
--- ball: set y to initial_y

y<initial_y is a conditon without start of layout?, what do you mean with "ball:ball: y<initial_y"

Thanks again
B
4
Posts: 11
Reputation: 222

Post » Wed Jan 13, 2016 11:58 pm

Again thanks, i think i got it...
B
4
Posts: 11
Reputation: 222

Post » Fri Jan 15, 2016 6:57 am

The idea there is we need to keep the object's y position below the starting y. If the object goes above the sqrt in the equation will make the speed NaN because you can't get the square root of a negative number.
B
88
S
30
G
89
Posts: 5,112
Reputation: 60,335

Post » Sun Jan 17, 2016 10:02 pm

@R0J0hound, well thanks again, but now the challenge is doing the simulation but including friction how can i do that?
B
4
Posts: 11
Reputation: 222

Post » Sun Jan 17, 2016 11:12 pm

You mean in an exact way? The normal physics behavior should be a good approximation. You can turn up the iterations to make things more accurate with the "set stepping iterations" action of the physics behavior.

If you want an even more precise simulation, you'll basically need to make your own, but that would take lots of research and time.
B
88
S
30
G
89
Posts: 5,112
Reputation: 60,335


Return to How do I....?

Who is online

Users browsing this forum: Benjo11, dzert14, imhotep22, mdvgames, yodashii and 2 guests