How to resolve this physical collision with impulses? - algorithm

I've started to write a physics engine but became stuck on some physics of resolving collisions. Let's say I have this situation:
I.e. body B is going towards body A at the speed of 1 space units/time unit. Both A and B have the same mass of 1 unit. Let's consider a completely elastic collision.
I've read in a book (Game Physics Engine Development) that an impulse-based approach can be used to resolve the collision (i.e. find out the linear and angular velocities of both bodies after the collision). As I understand it, it should work like this:
When the bodies collide, I get the point of the collision and the collision normal.
At the point of the collision I consider only two points colliding in the direction of the normal (the points at which the bodies are touching, i.e. I ignore the shapes of both bodies) and I compute the new velocities of these two colliding points (this is easy to do, there is a simple formula found e.g. on Wikipedia).
I find an impulse such that when applied to both bodies at this point it achieves the computed velocities for these two points.
Now the problem arises when I consider that from a physical point of view both momentum and kinetic energy need to be conserved. With these constraints in mind there is seemingly no solution, because:
When B collides with A, B should come to complete stop and transfer all its momentum and kinetic energy to A, according to elastic collision formula. In order for linear momentum to stay conserved, A then has to start linearly moving left at the same speed as B was before the collision (as they have the same mass). So now A has the same kinetic energy as B had, which however means that A cannot come into rotation because that would add additional kinetic energy to it (as rotating adds kinetic energy as well as linear motion), breaking the conservation of kinetic energy. Nevertheless, the physically correct solution IS for A to both move linearly to the left AND rotate as B colliding at this location exerts torque (and I've also checked real life object behave this way). Note that we cannot take away some energy of A's linear motion and add it to the rotation as that breaks the conservation of linear momentum.
The only "real" solution is that B doesn't come to complete stop and keeps some momentum while A will be both moving left and rotating. But this doesn't seem to be doable with the impulse-based approach that only takes into account the two colliding points, the elastic collision formula simply say the point at B should come to stop and as B cannot receive any torque (the collision happens in its middle), the only way to fulfill this is for B to stop moving.
So is there something I missed? Is the impulse-based approach just not physically correct? I appreciate any insight and suggestions on how to correctly resolve the collision. Thanks!

The formulas that you're looking at are for the collision of two point masses. Point masses can't have angular momentum, and so the formulas have no room for that term.
You have to go back to first principles.
Suppose that an edge collides with another body at a point (think corner hitting an edge). Then a specific impulse is imparted at that point, in a direction normal to the edge. (Any other direction would have required friction, which would make this a non-elastic collision.) The opposite impulse is imparted to the other body, along the same vector. Imparting opposite impulses to both bodies is sufficient to guarantee both conservation of momentum and angular momentum. But conservation of energy is going to take some work.
Next, what happens when we impart that momentum? As this physics answer says, we impart momentum as if the impulse happened to the center of mass. We impart angular momentum equal to the cross product of the impulse and the moment arm (the vector describing how much the impulse misses the center of mass). This will cause the body to start rotating at a rate of the impulse divided by the moment of inertia.
You get kinetic energy from the motion of the center of mass, but also kinetic energy from its rotation.
So in your 2-D collision you now have the following facts:
The mass of each body.
The velocities of each body.
The moment of inertia of each velocity.
The angular velocity of each body.
The moment arm of the line of force for each body.
You can now calculate the kinetic energy of the whole system, as a function of the magnitude of the specific impulse. Unlike the point mass, ALL of these factors play into it, making the equation complicated. However, like the point mass, you'll get a quadratic equation with 2 solutions. One solution is 0 impulse imparted (representing the system before the collision), and the other is your answer afterwards. Complete with changes to the momentum and angular momentum of both systems.

Related

Collision Detection algorithm for Selection advice

I don't know if this is proper platform for the question.
I'm trying to develop a collision detection algorithm.
In my system the primary host (a vehicle) is either stationary or is in motion (in xy, yz and xz plane).
Incoming projectile can either follow a strait path or non-linear trajectory (guided projectile).
Can anyone advice me on proper Collision Detection algorithm!
Initially I thought in terms of equation of a straight line in two-point and further extrapolating (using linear regression) to check if the projectile is impacting the host. This approach might work for unguided or high speed projectile but however on guided projectile this approach will not work due non-linearity and stochastic nature of real-world.
I wish to know if the projectile follows a ballistic trajectory or curved trajectory how can I detect collision. Is their any recommended algorithm.
I have created a Minimum bounding box (i.e. vehicle boundary - a cube) around my vehicle, I am having difficulty selecting/deciding on algorithm on how to track a projectile (say 400 meters away), if the projectile is converging/impacting on my vehicle boundary.

Deformation of a sphere on collision with the wall

I have made a sphere with squares(tilled with squares). For this I have taken 3 bands and each band consists of 8 points. So total 24 points. Now I have to deform the sphere on collision with wall. For this I have stored all the points in a tree data structure. Now I will traverse the tree as the ball translate and will increase the coordinate of the points (by say, 1 unit). When the collision point gets matched with any of the node in the tree, then collision has been detected. But in order to deform the sphere, I need to check its 8 neighbors and find how much area has been compressed. But how to find by how much distance the points in the sphere will get reduced(compressed) and by how much the neighbor points will be compressed. How I will know which points in the node is to be considered as their neighbors? I think the neighbors should be the children of the collided node. But how to find how much area is compressed? What are the points in that area?. How many children a node will have in my tree? Please give me some mathematical formula or procedure for doing this.
Hope you are writing about solid materials ...
you need to know the math/physics background behind this
you will need to know the integral and vector math so Google for mechanics of materials something like this ... first hit in my language. Sorry I am not confident to do the search in English but if you find materials with similar drawings ...
most Important things you need to look for are:
Hook's law
modulus of elasticity
tension
compression
look here vocabulary of terminology for this matter
may be a Surface/Voxel map of the sphere-oid would be a simpler choice
at least during computation otherwise you can pack/unpack it by your surface points tree. You will need the surface normals and integrate the spheroid by its slices
you need to compile all the data needed
material constants
handle isotropy/anisotropy
during contact
handle each time frame as static state. Compute the actual contact forces (transform actual kinetic energy into reaction force) and then compute the new shape for contact slice. Add the deformation distribution to non contact slices. Remove energy dissipation from kinetic energy and do the next time frame
Do not forget that the wall will deform too !!! so the contact zone is not always flat !!!
[Notes]
Sorry for long and not exact answer but this can not be solved by simple formula. You need to do this by area integration + slid state conditions fitting

Finding time and position of intersection of two moving, rotating bounding boxes

With an emphasis on finding the time (when the intersection starts), although the position is also important. The bounding boxes (not axis aligned) have a position, rotation, velocity, and angular velocity (rate of rotation). NO accelerations, which should really simplify things... And I could probably remove the angular velocity component as well if necessary. Either a continuous or iterative function would work, but unless the iterative function actively converges toward a solution (or lack thereof), it probably would be too slow.
I looked at the SAT, but it doesn't seem to be built to find the actual time of collision of moving objects. It seems to only work with non-moving snapshots and is designed to work with more complicated objects than rectangles, so it actually seems ill-suited to this problem.
I've considered possibly drawing the trajectory out of each of the 8 points then somehow having a function for if a point is in or out of the other shape and getting a time range of that occurring, but I'm pretty lost on how to go about that. One nice feature would be that it operates entirely with time and ignores the idea of discrete "steps", but it also strikes me as an inefficient approach.
No worries about broad phase (determining if it's worth seeing if these two bounding boxes may overlap), I already have that tackled.
Finding an exact collision time is essentially a nonlinear root-finding problem. This means that you will ultimately need an iterative approach to determine the final collision time -- but the clever bit in designing a collision solver is to avoid the root-solving when it isn't actually necessary...
The SAT is a theorem, not an algorithm: it can be used to guide the design of a collision solver, but it is not one itself. Briefly, it says that, if you can demonstrate a separating axis exists, the objects have not collided. Conversely, if you can show that there is no such axis, then the objects currently do overlap. As you point out, you can use this principle more-or-less directly, to design a binary "yes/no" query as to whether two objects in given positions overlap or not.
The difference with a collision solver is that the problem is animated, or kinetic: object position is a function of time. One way to solve this problem is to start with a valid "yes/no" collision test, treat all the inequalities as functions of time, and use root-finding methods to look for the actual collision times on that basis.
There is a variety of existing methods in published academic literature. I recommend some library research: the best option probably depends on the details of your application.
First of all, instead of thinking of two rectangles moving with speed (x1, y1) and (x2, y2) respectively, you may fix one of them (set its speed to (0, 0) ) and think of another one moving with speed (x2 - x1, y2 - y1).
This way, the situation looks like one rectangle is immovable, and another one is passing by, possibly hitting the first.
Assuming you don't have any angular velocity
Not hard to see, that you can then intersect 4 trajectories of a second rectangle (they are rays starting from different corners of a bounding box in (x2 - x1, y2 - y1) direction) with 4 sides of the first rectangle, standing still. Then you'll have to do the same vice versa - find the intersection of a first rectangle moving in reverse direction - (-(x2 - x1), -(y2 - y1)) with 4 sides of a second rectangle. Choose the minimum distance between the all intersection points you've found (there might be 0-8 of them) and you're done.
Don't forget to consider many special cases - when the sides of both rectangles are parallel, when there's no intersection at all etc.
Note, this all is done in O(1) time, though the calculations are quite complex - 32 intersections of a ray and a segment.
If you really wish your rectangles to rotate with some speed, I would suggest considering what #comingstorm has said: this is a problem of finding roots of a non-linear equation, however, even in such case, if you have a limited angular speed of your rectangles, you may split the task into a series of ternary search subtasks, though I suppose this is just one of possible methods of solving non-linear problems.

What "boundary conditions" can make a rectangle "look" like a circle?

I am solving a fourth order non-linear partial differential equation in time and space (t, x) on a square domain with periodic or free boundary conditions with MATHEMATICA.
WITHOUT using conformal mapping, what boundary conditions at the edge or corner could I use to make the square domain "seem" like a circular domain for my non-linear partial differential equation which is cartesian?
The options I would NOT like to use are:
Conformal mapping
changing my equation to polar/cylindrical coordinates?
This is something I am pursuing purely out of interest just in case someone screams bloody murder if misconstrued as a homework problem! :P
That question was asked on the time people found out that the world was spherical. They wanted to make rectangular maps of the surface of the world...
It is not possible.
The reason why is not possible is because the sphere has an intrinsic curvature, while the cube/parallelepiped has not. It can be shown that for two elements with different intrinsic curvatures, their surfaces cannot be mapped while either keeping constant infinitesimal distances, either the distance between two points is given by the euclidean distance.
The easiest way to understand this problem is to pick some rectangular piece of paper and try to make a sphere of it without locally stretch it or compress it (you can fold). You can't. On the other hand, you can make a cylinder surface, because the cylinder has also no intrinsic curvature.
In maps, normally people use one of the two options:
approximate the local surface of the sphere by a tangent plane and make a rectangle out of it. (a local map of some region)
make world maps but implement some curved lines everywhere identifying that the measuring distances must be made according to those lines.
This is also the main reason why when traveling from Europe to North America the airplanes seems to make a curve always trying to pass near canada. If we measured the distance from the rectangular map, we see that they should go on a strait line to minimize the distance. However, because we are mapping two different intrinsic curvatures, the real distance must be measured in a different way (and not via a strait line).
For 2D (in fact for nD) the same reasoning applies.

Detect when 2 moving objects in 2d plane are close

Imagine we have a 2D sky (10000x10000 coordinates). Anywhere on this sky we can have an aircraft, identified by its position (x, y). Any aircraft can start moving to another coordinates (in straight line).
There is a single component that manages all this positioning and movement. When a aircraft wants to move, it send it a message in the form of (start_pos, speed, end_pos). How can I tell in the component, when one aircraft will move in the line of sight of another (each aircraft has this as a property as radius of sight) in order to notify it. Note that many aircrafts can be moving at the same time. Also, this algorithm is good to be effective sa it can handle ~1000 planes.
If there is some constraint, that is limiting your solution - it can probably be removed. The problem is not fixed.
Use a line to represent the flight path.
Convert each line to a rectangle embracing it. The width of the rectangle is determined by your definition of "close" (The bigger the safety distance is, the wider the rectangle should be).
For each new flight plan:
Check if the new rectangle intersects with another rectangle.
If so, calculate when will each plane reach the collision point. If the time difference is too small (and you should define too small according to the scenario), refuse the new flight plan.
If you want to deal with the temporal aspect (i.e. dealing with the fact that the aircraft move), then I think a potentially simplification is lifting the problem by the time dimension (adding one more dimension - hence, the original problem, being 2D, becomes a 3D problem).
Then, the problem becomes a matter of finding the point where a line intersects a (tilted) cylinder. Finding all possible intersections would then be n^2; not too sure if that is efficient enough.
See Wikipedia:Quadtree for a data structure that will make it easy to find which airplanes are close to a given airplane. It will save you from doing O(N^2) tests for closeness.
You have good answers, I'll comment only on one aspect and probably not correctly
you say that you aircrafts move in form (start_pos, speed, end_pos)
if all aircrafts have such, let's call them, flightplans then you should be able to calculate directly when and where they will be within certain distance from each other, or when will they be at closest point from each other or if the will collide/get too near
So, if they indeed move according to the flightplans and do not deviate from them your problem is deterministic - it boils down to solving a set of equations, which for ~1000 planes is not such a big task.
If you do need to solve these equations faster you can employ the techniques described in other answers
using efficient structures that can speedup calculating distances (quadtree, octree, kd-trees),
splitting the problem to solve the equations only for some relevant future timeslice
prioritize solving equations for pairs for which the distance changes most rapidly
Of course converting time to a third dimension turns the aircrafts from points into lines and you end up searching for the closest points between two 3d lines (here's some math)
I actually found an answer to this question.
It is in the book Real-Time Collision Detection, p. 223. It's better named, as well: Intersecting Moving Sphere Against Sphere, where a 2D sphere is a circle. It's not so simple (and I may also violate some rights) to explain it here, but the basic idea is to fix one of the circles as a point, adding its radius to the radius of the moving one. The new direction for the moving one is the sum of the two original vectors.

Resources