Gravitational Slingshot

Im looking for some help please.

Im doing a simple Java game (well for the Android smart phone actually), and Id like to implement some gravitational slingshot effect in my game. The way it will work is as follows:
An emitter emitts a steady stream of particles in random directions and at a constant velocity. At a random position on the screen, there is a “Gate”. A Gate is simply a immovable stationary object that does nothing except register when a particle should collide with it.

But I want my particles to be “attracted” to the Gate when they come within a certain radius from the gate (before the main collision occurs). This attraction should be like a satellite geting sling shot past a planet (NASA calls it gravity assist). The particles can either be sling shot past, or if they collide with the gate, the score will be incremented.

Does anyone know of a good tutorial/ suggestion for this?
Here is some code that I have tried so far but there is an error somewhere as nothing happens when a particle gets close to the Gate. They simply continue on their original path.

Here I detect a collision, and then call the Particle class’ calcGravPull() method


if (getDistanceBetweenObjects(gate.getX(), particle[i].getX(), gate.getY(), particle[i].getY()) <= 
				sumOfRadii(particle[i].getRadius(), barrier.getRadius()) + barrier.getOuterCollisionRadius())
			{	
				
				particle[i].calcGravPull(particle[i].getMass(), barrier.getMass(), 
						getDistanceBetweenObjects(gate.getX(), particle[i].getX(), gate.getY(), particle[i].getY()));
			}

And here is the method to do the movement


// Calculate the gravitational pull between objects
	public void calcGravPull(int mass1, int mass2, double distBetweenObjects)
	{	
		double gravityPull;
		gravityPull = GRAV_CONSTANT * ((mass1 * mass2) / (distBetweenObjects * distBetweenObjects));
		
		x += gravityPull;
		y += gravityPull;
	}

You add the gravity effect to position and you should add it to velocity. Gravity only give acceleration not teleporting.

Also, you are adding the same value to both the x and y. You should be scaling a unit length (1 unit long) vector that points from your object to the gate. Then add that to its velocity.


		public void gravity(double xG, double yG, int rG, int mG, double dxG,
				double dyG, Ball ballG) {

			double ddx = (xG - x);
			double ddy = (yG - y);
			double d2 = ddx * ddx + ddy * ddy;
			double d = Math.sqrt(d2);

			if ((2 * d) > (radius + rG)) {
				ddx /= d;
				ddy /= d;
				dx += (ddx * (gra * mG) / d2);
				dy += (ddy * (gra * mG) / d2);
				ballG.setDx(ballG.getDx() - (ddx * (gra * mass) / d2));
				ballG.setDy(ballG.getDy() - (ddy * (gra * mass) / d2));
			} else if (d <  (radius + rG)) {
//if collison then I add masses together and smaller one will be se to zero and deleted later
				if (radius > rG) {
					dx = (mass * dx + mG * dxG) / (mass + mG);
					dy = (mass * dy + mG * dyG) / (mass + mG);
					this.setRadius((int) Math.sqrt(radius * radius + rG * rG));
					ballG.setRadius(0);

				} else {

					ballG.setDx((mass * dx + mG * dxG) / (mass + mG));
					ballG.setDy((mass * dy + mG * dyG) / (mass + mG));
					ballG.setRadius((int) Math.sqrt(radius * radius + rG * rG));
					radius = 0;
					mass = 0;
				}
			}
		}

I have made this long time ago with some basic variant and quickly ported it for java but it still works and you may benefit it.
That works for when you want lots of gravity objects. It’s lot easier to do if you just want one single static gravity well. Like you don’t need calculate masses. You just can multiply your gravity object mass with gravity_constant. So only distance is variable.


		public final static long staticGravityForce = 1000; //just test what is good
		public final static long staticX = 400;
		public final static long staticY = 400;
		public final static long staticRadius = 50;
		public void gravity(double x, double y, int radius, double dx,double dy) {
			 
			double ddx = (x - staticX);
			double ddy = (y - staticY);
			double distanceSquared = ddx * ddx + ddy * ddy;
			double distance = Math.sqrt(distanceSquared);

			if ((distance) > (staticRadius + radius)) {
				
				ddx /= distance;
				ddy /= distance;
				dx += (ddx * staticGravityForce / distanceSquared);
				dy += (ddy * staticGravityForce / distanceSquared);				
			} else {
				//COLLAPSE
				//JUST make some cool effects here
			}
			}
		}


Make my code simpler just for this task. Can anyone tell me how to get rid at squareroot?

You can get rid of the square root function in the line of code by using Point2D objects and their distance() method.
Not sure if that is what you were asking, though.

How its iplemented. I doubt it will be any faster.
I found http://www.flipcode.com/archives/Fast_Approximate_Distance_Functions.shtml
But it’s look like mess but I bet it would beat math.sqrt() any time.

Point2D.distance uses Math.sqrt and the same math, so you don’t get rid of anything. Math.sqrt is quite fast. Even on Android I couldn’t beat it with a few different “shortcuts”.

Yeah, I didn’t mean to imply that the distance() function didn’t use a square root to do its thing. But it does eliminate the need to code it by hand. I’ve not had problems with it’s speed either. If there is a performance problem, maybe something else?