Equations of motions vs Verlet: Collision ignored? - simulation

I initially coded my simulation using the standard equations of motions, but as is known, it ended up being quite unstable, even if it technically worked.
If we take x(t) to be the position-function of the equations of motion, I calculated my "next" position in the simulation by re-factoring x(t+delta), as follows:
x(t+delta) = x(t) + (1/2)*a*delta^2 + a*t*delta
t = t + delta
Most importantly, a is calculated as NetForce/Mass. If my system/particle encounters a collision, a normal force influences the net force in such a way that it is 0 on the axis of the collision (ie if it falls on the ground, force of gravity is pulling down, normal force of floor cancels gravity). So far, this has worked splendidly with any kind of collision.
However, I decided to switch to Verlet integration as it is known to be more stable, and for some reason, it completely ignores collision. I use the following formulas:
x(t+delta) = x(t) + v(t) * delta + .5*a(t)*delta^2
v(t+delta) = v(t) + .5 * (a(t) + a(t+delta)) * delta
a(t+delta) = NetForce / Mass
t = t + delta
Where v(0)=0, a(0)=0. As such, in addition to old position, I also store old acceleration and velocity.
However, I get the aforementioned problem: It just doesn't work properly, as it ignores collision. Even though it should be factored in into the acceleration already...
How should I approach this?

Isn't it suppose to be:
if x(t) is a position of collision:
v(t+delta) = v(t) - 2*( dot(normal_at(x(t)), v(t)) / norm(normal_at(x(t))^2 )*normal_at(x(t))
x(t+delta) = x(t)
else:
x(t+delta) = x(t) + v(t) * delta + .5*a(t)*delta^2
a(t+delta) = NetForce( x(t+delta) ) / Mass
v(t+delta) = v(t) + .5 * (a(t) + a(t+delta)) * delta
t = t + delta

Related

How to get some objects to move randomly in a space lets say a grid of [-5,5]

I need to move some objects lets say 50 in a space (i.e a grid of [-5,5]) and making sure that if the grid is divided into 100 portions most of the portions (90% or more) are once visited by any object
constraints :
object should move in random directions in the grid changing their velocities frequently (change speed and direction in each iteration)
I was thinking of bouncing balls ( BUT moving in random directions even if not hit by anything in space, not they way a real ball moves) , if we could leave them into space in different positions with different forces and each time they hit each other (or getting closer to a specific distance ) they move to different directions with different speed and could give us a result near to 90% hit of portions in the grid .
I also need to make sure objects are not getting out of grid ( could make lb and ub limits and get them back in case they try to leave the grid)
My code is different from the idea I have written above ...
ux = 1;
uy = 15;
g = 9.81;
t = 0; x(1) = 0;
y(1) = 0;
tf = 2.0 * uy / g; % time of flight back to the ground
dt = tf / 20; % time increment - taking 20 steps
while t < tf
t = t + dt;
if((uy - 0.5 * g * t) * t >= 0)
x(end + 1) = ux * t;
y(end + 1) = (uy - 0.5 * g * t) * t;
end
end
plot(x,y)
this code makes the ball to go with Newton's law which is not the case
Bottom line i just need to be able to visit many portions of grid in a short time so this is why i want the objects to moves in a chaotic way in the space in a random manner (each time running the code i need different result so it needs to be random path) and to get a better result i could make the objects bounce to different directions if they hit or visit each other in the same portions , this probably give me a better result .

Point of intersection between two vectors when the direction of one vector is not known

Problem: I have two vectors. I know the starting point of one vector, its direction, its magnitude. I know the starting point of the other vector and its magnitude. I need to find the direction of second vector as well as the position of intersection.
Vector A: Vector B:
Position = Known Position = Known
Direction= Known Direction= UNKNOWN
Magnitude= Known Magnitude= Known
To Find: Point of intersection.
Is it possible to find the point of intersection, with the given parameters? If so then how?
Application: I want to find the position where a player would be found based on the velocity he is moving, and shoot a bullet at him at the moment he would be found, taking into account the time taken for the bullet to reach that virtual target position.
Following on from the comments I'm going to take a leap here and answer your ultimate question directly.
Say the player is, at the initial time, at a point p and travelling with velocity v; your gun is at position q and shoots a bullet in any direction at speed s:
The length of OP is vΔt and that of Q sΔt. The angle a is given by the dot product:
We can then use the cosine rule to solve for Δt:
Written in this form, we can easily see that it is a quadratic equation, and thus directly solve for Δt using the Quadratic formula:
There are a few cases we need to consider here:
v < s: need to take the positive root only as otherwise we would get negative time.
v > s and dot(PQ, s) < 0: the bullet will never catch the player.
v > s and dot(PQ, s) > 0: take the negative root this time, as the positive root is for a backwards travelling player (longer time; this is also the case presented in the diagram above).
Having the correct value for Δt from above will then enable us to find the intersection point o, and therefore the intended direction d:
Note that d is not normalized. Also, this solution works for 3D too, unlike an approach with angles.
Let subscript 1 mark the player, and subscript 2 mark the AI:
initial: position (x_i, y_i)
angle: alpha_i
speed: u_i
The positions as a function of time t are :
player: (x_1 + u_1 * t * cos(alpha_1), y_1 + u_1 * t * sin(alpha_1))
AI's bullet: (x_2 + u_2 * t * cos(alpha_2), y_2 + u_2 * t * sin(alpha_2))
You have 2 uknowns:
t - the time of collision
alpha_2 - the angle the AI should shoot
The collision happens when Xs and Ys match. i.e.:
x_1 + u_1 * t * cos(alpha_1) = x_2 + u_2 * t * cos(alpha_2)
y_1 + u_1 * t * sin(alpha_1) = y_2 + u_2 * t * sin(alpha_2)
So,
alpha_2 = arcos( (x_1 + u_1 * t * cos(alpha_1) - x_2) / u_2 * t )
and also
alpha_2 = arcsin( (y_1 + u_1 * t * sin(alpha_1) - y_2) / u_1 * t )
substitue your values and equate these to expressions of alpha_2 to obtain t, then you can substitue t in either expression to obtain the angle alpha_2.

Unity predict endpoint from current velocity

My rocket's rigidbody velocity is Vector2(0,100) when I call a function. How can I calculate the world coordinate (enpoint) when the velocity reaches 0?
Gravity should be included in the formula.
Thanks!
It sounds like you want the integral of the velocity function, which should provide the total distance respective to time.
Your velocity is going to be v = (100 - ('t'ime * 'g'ravity)). We can solve for time like t = (-v + 100)/g -> t = (0 + 100)/g = 100/g. So you should reach zero velocity at t = 100/g (assuming all the same units).
The integral of your velocity will give you distance traveled. An integral calculator is here: http://www.integral-calculator.com/
The integral function of your velocity is 100t - (g*t^2)/2
From zero to a particular time t, you can just plug and play. So for example, if for a particular gravity you reach zero velocity at t = 10 seconds, you will have traveled (100 * 10) - ((g * 10^2)/2) distance. (so for gravity 9, you would get 1000 - (9 * 100)/2 = 550 units
Edit: To be clear - first you want to calculate how long it takes to get to velocity zero at a particular starting velocity and gravity:
t = vStart/g
Then plug that time value into the integral function above:
distance = (vStart * t) - ((g * t^2)/2)
(or clearly you could turn it into one function by replacing t with vStart/g in the second function, but if I were coding I would definitely calculate them in two steps to provide a sanity check in case my units were wrong)

Earth goes out of orbit in java simulation

Firstly I'm very new to java, apologies. I'm trying to simulate the Earth going around the sun. After plotting the results, its apparent the Earth spirals out of orbit just after one revolution!
I've checked and double checked the constants such as the mass of the sun and the Earth as well as the initial velocity and position. I'm not sure where I'm going wrong the equations are also correct confirmed by colleagues and the lecturer.
Code consists of 4 classes:
find.
Bad:
y += yVelocity * timeStep;
x += xVelocity * timeStep;
As you are using discrete timesteps, you must not only add the velocity to your position, but also the effect that acceleration will have on your velocity during that time.
Better:
yAccel = Sun.componentY();
xAccel = Sun.componentX();
y += (yVelocity + yAccel * timeStep * 0.5) * timeStep;
x += (xVelocity + xAccel * timeStep * 0.5) * timeStep;
yVelocity += yAccel * timeStep;
xVelocity += xAccel * timeStep;
This assumes the acceleration to remain constant throughout the duration of the timestep, which in reality it doesn't. Still, it should get much closer to the goal you want to achieve.
Best:
Using integrals you should be able to model the real behaviour perfectly (ignoring floating point issues).
Acceleration, velocity and position can all be expressed using integrals, which then you should be able to solve for the per-frame simulation range of t to t+dt. I've found that a trial license of Mathematica can be very helpful in such situations.

how to get linear acceleration from accelerometer data

I'm using an IMU (3 axis accelerometer, 3 axis gyro, 3 axis magnetometer), and I want to get the linear acceleration from the accelerometer data. I knew about sensor fusion and the ability to use the gyroscope data (and get orientations) to get the gravity vector, and hence removing its effect from the corresponding axes.
Am I on the right path, and could you help if you can?
after that I'll integrate the acceleration twice to get the position as in following
CurrentAcceleration[0] = e.Accelerometer[0];
CurrentAcceleration[1] = e.Accelerometer[1];
CurrentAcceleration[2] = e.Accelerometer[2];
//we need to get the linear acceleration instead of the read data !!
CurrentVelocity[0] += (CurrentAcceleration[0] + PreviousAcceleration[0]) / 2;
CurrentVelocity[1] += (CurrentAcceleration[1] + PreviousAcceleration[1]) / 2;
CurrentVelocity[2] += (CurrentAcceleration[2] + PreviousAcceleration[2]) / 2;
Position[0] += (CurrentVelocity[0] + PreviousVelocity[0]) / 2 ;
Position[1] += (CurrentVelocity[1] + PreviousVelocity[1]) / 2 ;
Position[2] += (CurrentVelocity[2] + PreviousVelocity[2]) / 2 ;
PreviousAcceleration[0] = CurrentAcceleration[0];
PreviousAcceleration[1] = CurrentAcceleration[1];
PreviousAcceleration[2] = CurrentAcceleration[2];
PreviousVelocity[0] = CurrentVelocity[0];
PreviousVelocity[1] = CurrentVelocity[1];
PreviousVelocity[2] = CurrentVelocity[2];
Won't work.
You cannot get accurate location or even velocity. On the above link you find tips what you actually could do instead.
By the way, this question pops up surprisingly often.