Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (487)
Games in Android Showcase (112)
games submitted by our members
Games in WIP (553)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Gravitational Slingshot  (Read 2088 times)
0 Members and 1 Guest are viewing this topic.
Offline hansmoolman

Innocent Bystander





« Posted 2011-04-11 16:41:21 »

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
1  
2  
3  
4  
5  
6  
7  
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
1  
2  
3  
4  
5  
6  
7  
8  
9  
// 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;
   }
Offline pitbuller
« Reply #1 - Posted 2011-04-11 18:17:58 »

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

Junior Member


Medals: 1



« Reply #2 - Posted 2011-04-11 19:03:11 »

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.

It's not what you know, it's what other people think you know.
Just hope you don't get quizzed on it.
Game engine design tutorials
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline pitbuller
« Reply #3 - Posted 2011-04-11 19:31:06 »

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
      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.
Offline pitbuller
« Reply #4 - Posted 2011-04-12 19:58:36 »

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
      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?
Offline philfrei
« Reply #5 - Posted 2011-04-13 00:00:14 »

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.

"Greetings my friends! We are all interested in the future, for that is where you and I are going to spend the rest of our lives!" -- The Amazing Criswell
Offline pitbuller
« Reply #6 - Posted 2011-04-13 07:52:18 »

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.
Offline Nate

JGO Kernel


Medals: 145
Projects: 4
Exp: 14 years


Esoteric Software


« Reply #7 - Posted 2011-04-14 01:02:33 »

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".

Offline philfrei
« Reply #8 - Posted 2011-04-14 02:37:27 »

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?

"Greetings my friends! We are all interested in the future, for that is where you and I are going to spend the rest of our lives!" -- The Amazing Criswell
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

TehJavaDev (15 views)
2014-08-28 18:26:30

CopyableCougar4 (25 views)
2014-08-22 19:31:30

atombrot (38 views)
2014-08-19 09:29:53

Tekkerue (33 views)
2014-08-16 06:45:27

Tekkerue (32 views)
2014-08-16 06:22:17

Tekkerue (20 views)
2014-08-16 06:20:21

Tekkerue (29 views)
2014-08-16 06:12:11

Rayexar (66 views)
2014-08-11 02:49:23

BurntPizza (42 views)
2014-08-09 21:09:32

BurntPizza (34 views)
2014-08-08 02:01:56
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!