Calculating laser trajectory under influence of gravity

Started by
17 comments, last by MidLevelGameDev 4 years, 5 months ago

In this game certain entities emit lasers (called beams). These beams advance forward a little bit, and then are checked for collisions (I used to use raycasting, but the need for black holes kind of ruined that approach). Beams are basically a list of points where it advanced. This list is later used to create the mesh for the visuals.

Once a collision is found something happens to the lasers position and direction, but with black holes I have no clue how to go about this.

So far, the function looks like this:


while(!beamsToSimulate.empty())
  for(beam in beamsToSimulate)
  	nextPoint = beam.points[beam.points.size() - 1].position + beam.points[beam.points.size() - 1].direction * 0.25; // Small distance to advance.
	
	// check if it has collided, or is already inside, or has exited anything
  	

How should I change the direction of a beam under the influence of gravity? I thought about lerping the current direction with the normalized difference vector between it and the black hole, but what interpolant to use I don't know either.

Advertisement

If you want realism ... i am only a hobbyist regarding programming and astronomy but i know this one needs deep knowledge of analysis. Search for "geodesic equations general relativity" to get an overview of how light bends around a gravity well. The outcome depends on the reference systems of the rays and the observer and the type of the black hole, if it rotates for example.

The interstellar movie team and physicist Kip Thorne published a paper about a raytracer in conjunction with the movie Interstellar. But this is more of an artist's impression created for the black hole "Gargantua", the one we know from the film. It ignores much of general relativity, for example spacetime curvature and its effects on wavelengths.

There is much more information on this in the papers on the recently presented first image of a real black hole. The papers - which are surprinsingly comprehensible for a normal mortal who is used to the "standard newtonian world" of low masses, speed and gravity - can be found here, number 4 might be the interesting one.

For a game, one would probably have to go a long way to simplify things, and the outcome will probably be bland and boring. In the end, the raytracer described in the paper may be a compromise ...

Maybe one of the more experienced guys here knows more about realizing such things ... i admit realizing it would be over my head.

 

I just want a simple formula to determine the ray's new direction given it's current position, black hole position and the beam's direction, that's all.

Well, then, how about the usual trivial 2 body problem, a photon of 0 mass representing the ray around a body with very high mass in a 2 dimensional plane of movement on a keplerian orbit. Orbital elements can easily be obtained from position and firing direction of the ship, gravity and angular change from the distance of the photon to the center of the mass. This is probably as simple as it can get, the formulae are in the links. You'll then have a parabola or hyperbola that can be drawn easily and checked if it hits something or someone else.

Just a suggestion. But this ignores the physics in the mentioned environment.

 

2 hours ago, midn said:

I just want a simple formula to determine the ray's new direction given it's current position, black hole position and the beam's direction, that's all.

Newtons formula defines gravitational force between two positions of bodies based on distance and mass, from there on, you can compute new acceleration for your beam (and the blackohole) and thus compute new position ahead for it.

Force=newton law of atrraction

accelaration= Force/mass of beam particle

speedupdate=acceleration*timeupdate

position=position+speedupdate

I don't compute acceleration nor speed, as I said above I compute only position and direction and then have it advance a few steps (Which is why I'm asking the question, that formula doesn't seem to work with my provided variables).

Of course, I may be just dumb. Algebra isn't my strong suit.

That's why i suggested a simplistic approach to calculate an orbital path from the variables you have (position, direction, distance, Edit: forgot speed, central mass, gravity). You can then look if the enemy's path crossed your hyperbola between two frames to have them eliminated from time, space, spacetime and the buffet table.

Edit: for more, like when and where exactly does a hit happen, you need to do more than that, calculate and apply forces, advance in discrete steps, do more thorough collision resolve.

And again, this does not take into account any physics in the given environment, or if the ray missed barely between two frames, and that your ship's path and that of the aliens are orbital paths as well. All guarantees void in the vicinity of black holes ?

If someone knows a simpler method ...

If you want to be realistic, you need to follow geodesics not only for the laser, but for every photon that ends up composing your image. In other words, you have to rethink the whole graphics pipeline. I have no idea how one would go about that, although it does sound like an interesting hobby project...

If you don't want to be realistic, make lasers bend however you think would contribute to the game mechanics.

 

Quote

make lasers bend however you think would contribute to the game mechanics.

That's exactly what I'm trying to figure out.

Quote

That's why i suggested a simplistic approach to calculate an orbital path from the variables you have (position, direction, distance, Edit: forgot speed, central mass, gravity). You can then look if the enemy's path crossed your hyperbola between two frames to have them eliminated from time, space, spacetime and the buffet table.

Edit: for more, like when and where exactly does a hit happen, you need to do more than that, calculate and apply forces, advance in discrete steps, do more thorough collision resolve.

And again, this does not take into account any physics in the given environment, or if the ray missed barely between two frames, and that your ship's path and that of the aliens are orbital paths as well. All guarantees void in the vicinity of black holes ?

If someone knows a simpler method ...

I seem to have badly explained how my current approach works. Basically, every frame:

  1. Remove all laser beams.
  2. For every entity which emits a beam:
    1. Start beam at entity position
    2. While the beam's power is above 0:
      1. Calculate new direction, position and power depending on whether the current/next/previous point is intersecting with any other entity.

This system effectively makes the delta time zero, so I can't figure out how to only curve it's direction and by how much depending on the distance to the black hole (as Newton's formulas kinda seem to break).

Well, not sure if i understand but i think it doesn't work that way.

Basically, during one frame, the ray advances c * deltatime(*) in the direction in which it was initially fired or it had last time(!), + the part it was deflected by gravity (acceleration towards the center * deltatime * (1/sqrt(height above the center))) of your massive object. That further simplifies the story to linear propagation between frames. You then check two lines, the enemy ship's travel vector and the resulting phaser beam propagation, for intersection. If there was an intersection, you must roll back the propagation to the intersection point and do what must be done, like reflection on a surface, damage calculation, etc. Count down the power of a beam over its lifetime by subtracting something from a start value every frame.

(*) You definitely do need deltatime for all of these kinds of calculation, i can't imagine a way without if you don't want to take the suggested orbit approach. Time is crucial here. And elsewhere of course ?

This topic is closed to new replies.

Advertisement