Java-Gaming.org
 Featured games (90) games approved by the League of Dukes Games in Showcase (556) games submitted by our members Games in WIP (476) 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
 Gravity hole: Objects moving near a hole, shifts its projection towards the hole  (Read 3088 times) 0 Members and 1 Guest are viewing this topic.
tom_mai78101
 « Posted 2012-07-31 12:09:50 »

I'm just curious.

You know that golf holes in any games that "attracts, or sucks, balls into/towards the hole" game mechanic?

I find games in space, such as Angry Birds Space on Android produces some zones in which objects spins around the planet when interacting with gravity, intriguing with gravity involved. Like how black holes interact with objects, how gravity distorts the original velocity vector of an object towards the center, etc.

• What is the name for the "gravity hole" game mechanic? (Depicted as such in the picture.) I would like to know the most generic name, so I can find it easily on Google.
• How it was done exactly? Does one rotate the dot product of (the normalized vector from center of hole to point of collision) and (speed vector of any object within range of the gravity hole) towards the center of the hole?
UprightPath
 « Reply #1 - Posted 2012-07-31 12:40:30 »

It's Gravitational Physics.

The effect? Nope, not rotating for the most part. It's more a build up of velocities based on the acceleration imparted by objects on objects. You're not really 'rotating' anything (Of course, due to the way that most objects function in these circumstances a rotation does occur, however that's a side-effect, not a cause).

Basically, you do something like: dv = g * mass / distance^2. Then you can figure out the 'components' of it by doing dx = sin(angle) * dv, dy = cos(angle) * dv. These are then added to the object's velocity vector. Rotation happens, to a small extent due to the fact that gravity doesn't affect the entire object at the same amount. This tends to be very negligible, instead more of it comes from the forces of drag/other atmospheric/friction based considerations.

In your image the 'Yellow' zone is really just the point at which the dv formula actually starts to apply a change in velocity that is noticeable (Due to the distance^2, far away objects are relatively unaffected), however the objects still feel them.

jonjava
 « Reply #2 - Posted 2012-07-31 12:48:49 »

Increase the speed of the object toward the whole. The magnitude of the speed increases as the object gets closer.

If you've got vertical and horizontal speeds, find out their components and magnitude with sin and cos based on the distance between the hole and the object (magnitude of speed).

I don't know if this phenomenon has a specific name for games. Ideally you don't have to rotate anything since it's a centripetal force, i.e, it happens automatically.

 Games published by our own members! Check 'em out!
tom_mai78101
 « Reply #3 - Posted 2012-08-01 10:55:58 »

I thought the entire process can be done by vector calculations, instead of using sines, cosines, and angles.

It's trippy, but I guess my curiosity is fulfilled.

The only problem left is the search term that allows me to search around on Google. I see that, other than using physics to describe it, there aren't anything on Google that points me to a few Java examples.
jonjava
 « Reply #4 - Posted 2012-08-01 11:41:16 »

You can do it with vectors too. Just add the gravity vector to the objects displacement vector.

The gravity vector which direction is towards the "gravity hole" and the magnitude is based on the distance between the "gravity hole" and the object.

Axeman

Senior Member

Medals: 7

 « Reply #5 - Posted 2012-08-01 17:52:13 »

What you are looking for can be done with springs. Google spring physics and Hooke's law and it should take you in the right direction. It shouldn't be too hard to inplement if you've got a good physics engine as a base.
krasse
 « Reply #6 - Posted 2012-08-01 20:59:55 »

What you are looking for can be done with springs. Google spring physics and Hooke's law and it should take you in the right direction. It shouldn't be too hard to inplement if you've got a good physics engine as a base.

Spring force (approximately) increases linearly with distance while gravity decreases quadratically with distance.

Axeman

Senior Member

Medals: 7

 « Reply #7 - Posted 2012-08-01 22:20:19 »

Yes, but the example tom_mai78101 is using is a golf game, as he states is "´attracts, or sucks, balls into/towards the hole´ game mechanic". When an object is in the yellow zone you let a spring force work on the object. It the force of the object is weaker than the spring it will "magnetise" itself to the center, else it will just change the objects course as it enters and leaves the yellow zone. I might not be gravity, but I belive it´s an relatively easy solution to the problem. Why wouldn´t this work?
tom_mai78101
 « Reply #8 - Posted 2012-08-02 16:23:14 »

Note that it's not just any golf games. It could be any space themed games.

By continuing this discussion further, I would like to try taking such a challenge and be able to get this mock Java code up on here:

 1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16  17  18  19 `public void suckIntoHole(Ball b, Hole h){      //Note that (x,y) for Ball and Hole are given at the top left corner.      float ballRadius = b.diameter /2;      float holeRadius = 16;      //Give the correct difference of the two objects' position.      float dx = b.position[0] - h.x + (ballRadius + holeRadius);      float dy = b.position[1] - h.y + (ballRadius + holeRadius);      //Find the angle of the line of collision      double angle = Math.atan2(dx, dy);      //Change the angle of the speed vector to the line of collision.      double lx = b.speed[0]*Math.cos(angle)-b.speed[1]*Math.sin(angle);      double ly = b.speed[1]*Math.cos(angle)+b.speed[0]*Math.sin(angle);      //Set the new speeds to the current speed.      b.speed[0] = (float) lx;      b.speed[1] = (float) ly;      //And then set the new position on the line of collision to the object's position.      b.position[0] = (float)(lx*Math.cos(-angle)-ly*Math.sin(-angle));      b.position[1] = (float)(ly*Math.cos(-angle)+lx*Math.sin(-angle));   }`

My hypothesis is like this:

But in practice, the ball went awkward... And didn't quite bend around the hole like I expected.
jonjava
 « Reply #9 - Posted 2012-08-02 23:52:46 »

I don't really understand that graph

I made a little sample applet. All I do is add the speed vector to the position vector.

The "hard" part is figuring out the x and y components of the pull. Which you can do with sin and cos on the magnitude of the pull. You get the angle needed by the angle between the objects. I wasn't bothered I simply took the delta of the objects positions and added a fraction of that to the speed.

Warning! Untrusted content: jonjava submitted an applet to JGO.

JGO cannot be held responsible for the contents and/or behavior of the hosted applets.

left and right click to add particles, middle click (wheel) to make a massive immovable.

Hmm.. the [applet] tag doesn't work.

 Games published by our own members! Check 'em out!
tom_mai78101
 « Reply #10 - Posted 2012-08-03 03:49:06 »

It is an interesting insight. I guess I went ahead and thought things out differently than I expected.
matheus23

JGO Wizard

Medals: 87
Projects: 3

You think about my Avatar right now!

 « Reply #11 - Posted 2012-08-03 11:28:03 »

I don't really understand that graph

I made a little sample applet. All I do is add the speed vector to the position vector.

The "hard" part is figuring out the x and y components of the pull. Which you can do with sin and cos on the magnitude of the pull. You get the angle needed by the angle between the objects. I wasn't bothered I simply took the delta of the objects positions and added a fraction of that to the speed.

Warning! Untrusted content: matheus23 submitted an applet to JGO.

JGO cannot be held responsible for the contents and/or behavior of the hosted applets.

http://heartpirates.com/games/gravity.html

Hmm.. the [applet] tag doesn't work.

Very intresting applet.
The problem with your implementation is (I fist didn't know you are able to right-click on it to make things appear), that if you create a lot of object on one position, then wait for it to drive off, the more far away you put new "particles" the faster they move to the gravitation center.

So to put it in an easier sentence: The farer away your particles are from each other, the stronger the gravity is between them.

That's totally logical. You used the fraction of the delta vector and added that one to the position. So when does this vector get larger? Of course when the total delta is getting bigger.

See my:
My development Blog:     | Or look at my RPG | Or simply my coding
http://matheusdev.tumblr.comRuins of Revenge  |      On Github
jonjava
 « Reply #12 - Posted 2012-08-03 11:38:20 »

I increase the speed with the difference in distance towards every other particle. Actually, yeah, the further away the faster the speed is increased

tom_mai78101
 « Reply #13 - Posted 2012-08-03 11:52:47 »

I noticed something as soon as I try the implementation. The balls will eventually go towards the hole, but very slowly, while it's bouncing around and around the hole.

I tried tweaking it, so that if the ball gets closer and closer to the hole, the speed increases. I realized that if I were to hit the balls affected by the gravity pull with my cue ball, the balls ignore their current position and continues to move around, despite my cue balls changing their positions directly.

This behavior isn't what I wanted, and they seemed to be off by many factors. I've been tweaking it until I fell asleep on the keyboard, so I must've been thinking too hard on this problem.

Here's the current tweaked code:

 1  2  3  4  5  6  7  8 `public void gravityPull(Hole h){      double dx = (this.position[0] - h.x);      double dy = (this.position[1] - h.y);      dx *= 0.00000001;      dy *= 0.00000001;      this.speed[0] += (float) 0.9 * dx - 1;      this.speed[1] += (float) 0.9 * dy - 1;   }`

EDIT:

Actually, I'm adding buffers to this. If we look at this one-dimensionally, for example, on the X axis, we could see that the values tend to increase/decrease around the targeted X value (where the hole is located on the X axis). The values go up and down, as show here.

What I'm doing is to try to make it so that when the ball goes past the targeted X value, and slow down, just like the red graph shown above.

I thought that if I give the Dx and Dy values a bit more intensity, it will work. But that is not the case.
jonjava
 « Reply #14 - Posted 2012-08-03 12:56:13 »

Well the "correct" way of calculating the pull's strength is from Newtons:

Where r is the distance between them ( this is not the same as taking the difference between the objects x and y position )

This is where you use pythagoras famous "a^2 + b^2 = c^2" and conclude that the distance is

Once you have the distance you can insert it to the for r.

G is the gravitational constant ( change this to determine how fast/slow you want the gravitation to be ).

m1 and m2 are the masses of the objects. The larger the mass, the bigger it pulls.

Once you have the magnitude/strength of the pull (using ) you then have to split it to its horizontal ( x ) and vertical ( y ) components.  Then add it to the speed vectors.

[EDIT]: Actually I'll update my applet to incorporate this. My lazy delta x and y positions just confuses everyone.

[Updated]: controls: left and right click to add particles, middle click (wheel) to make a massive immovable.

tom_mai78101
 « Reply #15 - Posted 2012-08-03 17:25:02 »

The last problem I noticed immediately is my inability to split the gravity push/pull force into X and Y vectors on the Cartesian coordinates.

Line of pseudo-code process:

 1  2  3  4  5  6  7  8  9  10  11  12  13  14  15 `final double G;//......deltaX = a.position[0] - b.position[0];deltaY = a.position[1] - b.position[1];distance = Math.hypot(deltaX, deltaY);angle = Math.atan2(deltaX, deltaY);//We set both the mass to 1force = G * a.mass * b.mass / (distance * distance);//I suck at separating a singularity vector into 2 perpendicular vectors.x = force * Math.cos(angle);y = force * Math.sin(angle);`

God forbids me to get a good job. *sigh*

Anyway, I'll check your source codes and will update my own version of integration tomorrow. 11:25PM, signing out.
Bahill

Junior Newbie

Medals: 2

 « Reply #16 - Posted 2012-08-06 23:57:00 »

Sounds like you're wanting to simulate something like a gravity well, where an object is caught in the gravity field of a larger more massive object and is pulled inwards. Jonjava above does a good job of explaining the physics involved, i'd like to try and take it a tiny bit further.

Once you calculate the force due to gravity on the smaller object, F, i think its useful to use this to calculate the acceleration on the object, since F = mass x acceleration, and work with acceleration instead of F alone. This way you can just use the acceleration value to update the velocity, then use the new velocity value to update the x and y position co-ordinates of the object.

If you call m1 the mass of the larger more massive object, and m2 as the mass of the smaller orbiting object, then the acceleration of m2 is found by the following...

If m1 is much larger than m2, then the above shows that you can effectively drop the value of m2 from the calculation of acceleration due to gravity on m2.

Once you have the acceleration, you find it's horizontal and vertical components. You've already nailed that in your code above using trig and the angle.

In each frame just keep adding the horizontal and vertical components of the acceleration to the horizontal and vertical components of the object's velocity, and in turn add these velocity components to the x and y position co-ordinates of the object to simulate the effect of gravity, though rather roughly. A more accurate simulation would include higher order integration factors to accommodate the effects of a variable acceleration rate.

If you're having bother splitting a vector into its horizontal and vertical components, check out the applet I knocked up to help visualise this at the link below. Click the mouse left button and drag it around the screen to see a vector and its horizontal and vertical components.

http://www.bahill.net/myApplet.html

Hope all that makes sense and is of some help to you,

cheers,

Bahill
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.
 digdugdiggy (17 views) 2014-03-14 23:54:44 Rayvolution (26 views) 2014-03-13 00:02:00 The Lion King (51 views) 2014-03-11 04:35:53 Screem (70 views) 2014-03-08 05:04:17 Screem (73 views) 2014-03-08 05:01:59 Gibbo3771 (84 views) 2014-03-05 15:13:19 Conzar (84 views) 2014-03-05 09:59:23 Conzar (78 views) 2014-03-05 09:57:20 GamerIDGoesHere (111 views) 2014-03-02 10:31:15 GamerIDGoesHere (98 views) 2014-03-02 10:30:27
 opiop65 29x kpars 23x Rayvolution 21x HeroesGraveDev 20x BurntPizza 20x Grunnt 17x theagentd 15x jonjava 15x saucymeatman 15x Riven 14x LiquidNitrogen 13x revers 13x reymantha 13x Gibbo3771 11x princec 10x Troubleshoots 10x
 Anonymous/Local/Inner class gotchasby Roquen2014-03-11 15:22:30Anonymous/Local/Inner class gotchasby Roquen2014-03-11 15:05:20Anonymous/Local/Inner class gotchasby Roquen2014-03-10 14:19:36HotSpot Optionsby Roquen2014-02-26 17:00:23HotSpot Optionsby Roquen2014-02-26 11:27:03HotSpot Optionsby Dxu19942014-02-18 01:05:35HotSpot Options2014-02-16 03:47:31Mainby Roquen2014-02-10 11:17:06
 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