## mathematics – Predict future position of a moving body in Phaser arcade physics

I am looking for an equation for predicting the future position of a moving arcade physics body in Phaser 3. The body has drag applied and isDamping set to true. Phaser applies the drag using the following run on each update (showing x axis only but same applies to y).

``````//  Damping based deceleration
dragX = Math.pow(dragX, delta);

velocityX *= dragX;
``````

Given the above, how would I write a kinetic equation to predict the future position?

I am currently using the method below, where I am iterating over frames to calculate the position accumulatively. But this is inefficient and inelegant so would prefer a solution which estimates the position without any looping.

``````public futurePosition(timeInSeconds: number): Phaser.Math.Vector2 {
const DELTA = 1 / 60; // Assume we are running at 60fps
const DRAG = 0.3; // Drag value
const position = this.position.clone(); // Current position of body
const velocity = this.body.velocity.clone(); // Current velocity

// Inefficiently looping through frames
for (let i = 0; i < timeInSeconds / (DELTA * 1000); i++) {
velocity.x *= Math.pow(DRAG, DELTA);
velocity.y *= Math.pow(DRAG, DELTA);
position.x += velocity.x * DELTA;
position.y += velocity.y * DELTA;
}

return position;
}
``````

Any help appreciated. Thanks.

Posted on

## mathematics – What is closing and separating velocity in Ian Millington’s game physics book?

I’m working on a physics engine in `c++` and chose Ian Millington’s Physics Engine Development book as a guide and reference to help me out understand physics concepts and how to implement them.

Yesterday I was reading through the Hard constraints chapter particularly the simple collision resolution section and came across an explanation of a physics formula that I couldn’t fully comprehend. Millington says The closing velocity is the total speed at which two objects are moving together and it is calculated with the following formula: Where `Vc` is the closing velocity, `Pa` and `Pb` are object a and b respectively. Also he further discusses another formula called the separating velocity which is written as follows: Where `Vs` is the separating velocity. Now I don’t really understand what these formulas actually are or what they represent in the physics world. I also have trouble wrapping my head around why we need these formulas to perform collision resolution since he used the separating velocity formula to resolve the velocity upon collision and why we need the dot product. It would be really helpful if someone can provide visual explanation that would significantly help me understand what’s going on.

Posted on

## physics – Simulate spring wire through fixed points

Suppose we have a wire made of an ideal spring steel. If we bend it and then release any external force it will straighten itself into a perfect line.

Suppose also that the wire is attached to some set of points in the plane. The wire can rotate in these points slide through the points but can not escape out of these points.

How to simulate this?

I made a code when we have only two fixed points – (large points in image). The small points are ends of the wire. The length of wire is constant all the time.

``````Manipulate(
Plot({x^2, 1/a x^2 + 1 - 1/a}, {x, -5, 5},
Epilog -> {Point({{b /.
FindRoot(
b Sqrt(1 + (4 b^2)/a^2) + 1/2 a ArcSinh((2 b)/a) -
1/2 (4 Sqrt(17) + ArcSinh(4)), {b, 1}),
1/a b^2 + 1 - 1/a /.
FindRoot(
b Sqrt(1 + (4 b^2)/a^2) + 1/2 a ArcSinh((2 b)/a) -
1/2 (4 Sqrt(17) + ArcSinh(4)), {b, 1})}, {-b /.
FindRoot(
b Sqrt(1 + (4 b^2)/a^2) + 1/2 a ArcSinh((2 b)/a) -
1/2 (4 Sqrt(17) + ArcSinh(4)), {b, 1}),
1/a b^2 + 1 - 1/a /.
FindRoot(
b Sqrt(1 + (4 b^2)/a^2) + 1/2 a ArcSinh((2 b)/a) -
1/2 (4 Sqrt(17) + ArcSinh(4)), {b, 1})}}),
Point({{-2, 4}, {2, 4}}), PointSize -> Large,
Point({{-1, 1}, {1, 1}})}, AspectRatio -> Automatic,
PlotRange -> {-1, 5}), {a, 1, 100})
`````` This is only a pseudo simulation and it has nothing to do with reality, I only wanted to demonstrate how it might look.

Instead of two fixed points we can have 3, 4 or more points. I have no idea how to simulate it.

## physics – How to use a Rigidbody on a game object after animation in Unity?

How to use a Rigidbody on a game object after animation in Unity?

I want to play an animation fully controlling the positioning of the elements, i.e. ignoring physics. And after an animation plays till the end I want physics to resume affecting the element.

Right now to get the expected behavior I have to remove the Animator after an animation. I thought that maybe there is a neater way to do what I want, but I did not find it, so decided to ask here. Posted on

## physics – How to avoid entities flying far away when tabbing out and then tabbing back in?

I’m developing a game which uses a standard Entity-Component-System.

Every frame, the `PhysicsSystem` runs, which, among other things, updates the position of entities based on their velocity. Something like this:

``````execute(delta: number, elapsed: number) {
for (let entity of this.scene.query(CT.Transform, CT.Physics)) {
let transform = entity.get(CT.Transform);
let physics = entity.get(CT.Physics);

let movementIncrement = physics.direction.multiply(delta * physics.friction * physics.speed);

physics.velocity = physics.velocity.multiply(1 - delta * physics.friction).add(movementIncrement);
transform.pos = transform.pos.add(physics.velocity.multiply(delta));
}
}
``````

This works fine, except there’s an issue. When the user switches to another tab, waits some time (let’s say 5 seconds), and then tabs back in, sometimes the entities instantly fly away so far that they fly off the map and out of bounds.

I’m not sure why this would happen, as it never happens during normal gameplay. Something to do with the tab becoming inactive for a time, and then active, seems to cause this issue.

I suppose a possible solution is to “pause” the game if the tab loses focus, but I rather the game keep running in the background unless the user manually pauses by hitting escape.

Does anyone know why this is happening? The rest of the game continues functioning normally, it’s just the entity positions fly away for some reason.

Posted on

## physics – What is the science behind 3d collision relative to the ground?

To my understanding you have the model of the character and the model of the map.

It seems extremely redundant and process intensive to have to calculate that the ground exerts a force on the player model.

So to my understanding if y coord was less than a certain value then the physics would behave differently.

but what if the map becomes an incline instead of linear? the model would just walk through the ramp into nothingness….

EDIT: i remember a commercial game where you could “sink” if your speed was high enough. So it seems to me like the ground is somehow coded to exert a force on the player without putting too much work on th cpu

Posted on

## matrix – Need help in setting up the code for a physics problem (wick theorem)

I want to compute following object , it’s exact form is given in the follow (I will try to avoid talking any physics in this question) you can treat as time (but it is not important) and <.> is the expectation value. One can use following trick to do the computation For example, i = 1 and j = 2 or it also be written as the following by using above tricks How do I turn this into mathematica code? Could someone give me a little bit of hint ?

Thank you very much !!!

Posted on

## physics – Calculate angular velocity from rotation matrix difference

I am working on something for a game. I need to calculate the angular velocity, however in my situation I only have access to the previous rotation matrix and the current rotation matrix (and some other variables like dt or age of the object).

My angular velocity is hereby constant between t0 and t1.

The normal formula for the angular velocity `W(t) = dR(t)/dt * R(t)^-1` can’t be used here, since I have discrete values. Is there a possibility to calculate the angular velocity between timestep t0 and t1 with the given R0 and R1 rotation matrix of an object?

I am not very advanced with matrix math, so please go easy on me.

Posted on

## physics – Equation describing an elliptical orbit using time and angle

I’m working on a project where I’m trying to describe the orbits of the planets in the solar system using the polar equation of an ellipse.

Below is an equation I got from: https://www.hilarispublisher.com/open-access/mathematically-describing-planetary-orbits-in-two-dimensions-2168-9679-1000414.pdf

$$r=frac{b^{2}}{a-c cos theta}$$

Where
a is the semi-major axis,
b is the semi-minor axis,
c is the distance between the center of the orbit and a focus point of the orbit (c^2 = a^2 – b^2). The sun is at one of these foci, so c is the distance between the center of the orbit/ellipse and the sun, the origin of the graph. This form of the equation also assumes no displacement of the ellipse in the y direction from the origin.

This equation does relate the radius r of a point on the ellipse as a function of the angle θ it makes with the origin.

However, I am trying to look for an equation that models the angle θ as a function of time t. For example, if T was the period of one full orbit, then after T seconds, the change in angle should be 2π radians. In other words, where if f(t) = θ, f(T) = 2π or 0.

Considering that I have the values for a, b, and c, the orbital period T, and the linear speed v of each planet, and since I cannot use the radius directly (because it is not constant across the ellipse), how could I derive the polar angle θ as a function of time t without r?

Posted on

## physics – Seeming hard-limit on image resolution?

I am trying to generate some very large images of the basins of attraction of a dynamical system. However, the resolution of the images seems to cap out at 10001 x 10001 pixels, and no arrangement of ImageResolution or ImageSize seems to fix this. As far as I can tell, Mathematica is encountering no errors as it runs this, and is able to accurately spit out a result, but for whatever reason, the image simply cannot exceed 10001 x 10001 pixels. I have combed the documentation and have not found any clues about any software or hardware limitations which may be present here. Any ideas?

Here’s the code I’m using; on my machine, it spits out a 10001 x 10001 image, despite the resolution being set at 50000 x 50000:

``````(*2 magnets*)
n = 50000; (*number of pixels on each side of the image*)
{tmax, dt} = {25,
0.05}; (*Number of time steps and length of each time step*)
{k, c, h} = {0.1, 0.2,
0.1}; (*Damping coeffecient, Gravitational force coefficient,
resting distance from surface*)
{z1, z2} =
2 N@Exp(I 2 Pi {1, 2}/
2); (*Locations in the complex plane of the magnets. Magnets
can't be at the origin*)
l = 5.0;

z = Developer`ToPackedArray@
Table(x + I y, {y, -l, l, 2 l/n}, {x, -l, l, 2 l/n});
v = a = aold = 0 z;
Do(z += v dt + (4 a - aold) dt^2/6;
vpredict = v + (3 a - aold) dt/2;
anew = (z1 - z)/(h^2 + Abs(z1 - z)^2)^1.5 + (z2 -
z)/(h^2 + Abs(z2 - z)^2)^1.5 - c z - k vpredict;
v += (5 anew + 8 a - aold) dt/12;
aold = a; a = anew, {t, 0, tmax, dt});
res = Abs({z - z1, z - z2});
Image(.7/res, Interleaving -> False, ColorSpace -> "GrayScale", ImageSize -> Full)
``````

Posted on