How do I conserve energy in a physics simulation

0 favourites
  • 7 posts
From the Asset Store
Animated Energy Portals, Barriers, and Power-Ups great for all types of games.
  • 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.


    here's capx link

  • 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=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) )

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • 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

  • Again thanks, i think i got it...

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

  • R0J0hound, well thanks again, but now the challenge is doing the simulation but including friction how can i do that?

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

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)