javascript - Game physics simulation: accelerate based on energy -


Countless times I have implemented the simulation of physics using the Euler integration for small toy games: press the arrow keys And it sets an acceleration, then integrate acceleration and add to velocity, then integrate velocity and add to the position to get the last position. I see it everywhere.

But when I thought of kinetic energy, I realized that it is not "realistic". If something is given like a car, then the engine produces power (kilowatt), does not force / acceleration, with constant acceleration, increases the power velocity of the virtual engine increases. In my case this is a space shuttle, but I want to use continuous power in one direction instead of constant motion.

Is my assessment right that tracking energy is more realistic than speed? If so, does it really make sense to keep track of the energy vector rather than the velocity vector? But then I have to change in velocity to integrate. So I did this for 1D simulation and it works, of course i can use e = 0.5 * m * v * v and solve for v. But while using the vector, where I can not take the "square root", the vector of energy can get the vector whose direction is the same, but the magnitude is the square root of the original, but is it physically correct?

I thought someone should do this, but I searched and searched the web and did not see it, so maybe I'm away from the base.

Basically, what I want to do, the conventional "push" arrow and spacecraft increase the speed on a steady acceleration, and it is "kinetic kinetic energy to achieve the up arrow and spacecraft at a constant rate" but

Update: Javascript code based on the accepted answer of Xepeptian:

  

Code> Function class (x) {return x * x; } // Square Cosine, for the protection of the signature function cos2 (x) {var ret = Math.cos (x); If (rate> = 0) {return square (ret); } Other {return-sq (ret); }} // square sign, for the protection of sin sin2 (x) {var ret = Math.sin (x); If (rate> = 0) {return square (ret); } Other {return-sq (ret); }} Ceremony vessel () {this.x = 20; // m this.y = 40; // m this.dx = 0; // m / s this.dy = 0; // m / s this.ex = 0; // J this.ey = 0; // J this pangle = Math PI / 2; // this digit of angle = ms = 1; // kg this.power = 200; // w UPDATE = Function (DT) {/*...*/} // Update based on X / Y DX / DI / ** * Direct expression of expressions of expression, but conservation of sin in COS ^ 2 ^ 2 and sqrt * Operations * @ Param DT Delta Time in Seconds / / Speedup2 = Function (DT) {this.ex + = this.power * dt * cos2 (this.pangle); This.ey + = this.power * dt * -sin2 (this.pangle); Var signx = this.ex & gt; 0? 1 1; Var signy = this.ey & gt; 0? 1 1; This.dx = Math.sqrt (2 * Math.abs (this.ex) / this.mass) * signx; This.dy = Math.sqrt (2 * Math.abs (this.ey) / this.mass) * Signature; }; / ** * Modified variety of the exception post, where I change the energy "vector" in velocity, which I believe is equal. * / This.speedup = function (dt) {this.ex + = Math.cos (this.pangle) * this.power * dt; This.ey + = -Math.sin (this.pangle) * this.power * dt; Var totalEnergy = Math.sqrt (this.ex * this.ex + this.ey * this.ey); Var speed = Math.sqrt (2 * total energy / this.mass); Var ratio = speed / total energy; This.dx = this.ex * ratio; This.dy = this.ey * ratio; This.speed = Math.sqrt (this.dx * this.dx + this.dy * this.dy); }; }

You can do this if you want to be just Kinetic Keep track of energy with X and Y (and if you are moving in 3D) Kinetic energy with x = axis = kinetic energy with i = axis = ect = x + i / / dt = 1 / fps P> ORC If you do not like Cartesian then you can keep Ex, Ey you Etot, direction , this will be a completely equal conversion:

  Ex = Etot * cos ^ 2 (direction) i = atot * sin ^ 2 (direction)   

and invert:

  Etot = Ex + Ey direction = atan2 (sqrt (i), sqrt (east))   

On a given frame, your engine will give you Power * DT Energy, that is

  \ delta {Ex} = power * dt * cos ^ 2 (direction) \ delta {I} = power * dt * sin ^ 2 (direction)   

And from there, you should know that every frame:

  // etot = E_x + E_y // vtot = sqrt (2 * eutout / m) v_x = Sqrt (2 * ex / m) // vtot * cos (direction) v_y = sqrt (2 * i / m) // vtot * sin (direction)    

Comments

Popular posts from this blog

ios - Adding an SKSpriteNode to SKScene from a child SKSpriteNode -

Matlab transpose a table vector -

c# - Textbox not clickable but editable -