somewhat of a Black hole simulation
Say I have X amount of balls on the screen, I want at any opint to have a "black hole" (quote unquote) appear at a single location in space. This is for a 2D game, so that''s just pixel location.
Now given the X balls moving at some speed in some direction, when the "Black hole" pops up, I want the balls to spiral into it until they all end up at the center of the singularity. I''ve tried various things, getting the vector from the ball to the singularity, using that as some ratio of gravity in the direction of x and y and adding that into the current x/y velocities, but I got various strange results and I couldnt figure it out properly.
Anynoe have any good ideas?
ByteMe95::~ByteMe95()
My S(h)ite
ByteMe95::~ByteMe95()My S(h)ite
The simplest thing to do is treat the black hole as a gravity source. That is, the black hole causes the objects to accelerate towards the hole. The acceleration represents a change in velocity. So, it'd go something like this:
float blackhole_location[2]; // x,y coordinates of black hole
float object_location[2]; // x,y coordinates of object
float object_velocity[2]; // velocity of the object in x and y
float object_accel[2]; // acceleration of the object in x and y
float black_hole_g = 1.; // gravitational acceleration of bh
float direction_of_g[2]; // vector that points from object to bh
For each frame, do this:
direction_of_g[0] = blackhole_location[0] - object_location[0];
direction_of_g[1] = blackhole_location[1] - object_location[1];
// normalize direction_of_g to length = 1;
// I won't provide code for this
// now calculate acceleration. This is basically just applying
// black_hole_g along the vector defined by direction_of_g.
object_accel[0] = black_hole_g * direction_of_g[0];
object_accel[1] = black_hole_g * direction_of_g[1];
// now update velocity
object_velocity[0] += object_accel[0] * time_step;
object_velocity[1] += object_accel[1] * time_step;
// finally, update position
object_location[0] += object_velocity[0] * time_step;
object_location[1] += object_velocity[1] * time_step;
time_step is just the time that passes between frames (e.g, 1./60. seconds if you are running at 60 frames per second). There are different ways to choose time_step, that have been discussed in other threads. I won't go into it here. Just pick something simple for now, and it'll probably work okay for this simple animation.
That's basically very simple particle dynamics, using simple Euler integration to update velocity and location based on acceleration due to a center mass---the black hole.
In reality, black_hole_g is not constant---it varies as the objects get closer to the black hole. But for a basic effect as you describe, for a game, this should give you what you want. You may have to play around to get a good value for black_hole_g.
Graham Rhodes
Senior Scientist
Applied Research Associates, Inc.
[edited by - grhodes_at_work on April 26, 2002 5:14:39 PM]
float blackhole_location[2]; // x,y coordinates of black hole
float object_location[2]; // x,y coordinates of object
float object_velocity[2]; // velocity of the object in x and y
float object_accel[2]; // acceleration of the object in x and y
float black_hole_g = 1.; // gravitational acceleration of bh
float direction_of_g[2]; // vector that points from object to bh
For each frame, do this:
direction_of_g[0] = blackhole_location[0] - object_location[0];
direction_of_g[1] = blackhole_location[1] - object_location[1];
// normalize direction_of_g to length = 1;
// I won't provide code for this
// now calculate acceleration. This is basically just applying
// black_hole_g along the vector defined by direction_of_g.
object_accel[0] = black_hole_g * direction_of_g[0];
object_accel[1] = black_hole_g * direction_of_g[1];
// now update velocity
object_velocity[0] += object_accel[0] * time_step;
object_velocity[1] += object_accel[1] * time_step;
// finally, update position
object_location[0] += object_velocity[0] * time_step;
object_location[1] += object_velocity[1] * time_step;
time_step is just the time that passes between frames (e.g, 1./60. seconds if you are running at 60 frames per second). There are different ways to choose time_step, that have been discussed in other threads. I won't go into it here. Just pick something simple for now, and it'll probably work okay for this simple animation.
That's basically very simple particle dynamics, using simple Euler integration to update velocity and location based on acceleration due to a center mass---the black hole.
In reality, black_hole_g is not constant---it varies as the objects get closer to the black hole. But for a basic effect as you describe, for a game, this should give you what you want. You may have to play around to get a good value for black_hole_g.
Graham Rhodes
Senior Scientist
Applied Research Associates, Inc.
[edited by - grhodes_at_work on April 26, 2002 5:14:39 PM]
Graham Rhodes Moderator, Math & Physics forum @ gamedev.net
quote:
direction_of_g[0] = blackhole_location[0];
direction_of_g[1] = blackhole_location[1];
Should that actually be:
direction_of_g[0] = blackhole_location[0] - objectlocation[1];
direction_of_g[1] = blackhole_location[1] - objectlocation[1];
?
ByteMe95::~ByteMe95()
My S(h)ite
ByteMe95::~ByteMe95()My S(h)ite
Ok, that pretty muh worked thanks. The only addition I had to make was to add some sort of friction pointing in the opposite direction of the new trajectory so that the balls actually end up spiraling inwards. Without the friction they pretty much found a nice orbit at some point and never got closer to the singularity.
But looks good now
Now I just have to find out how to render a wormhole
ByteMe95::~ByteMe95()
My S(h)ite
But looks good now

Now I just have to find out how to render a wormhole

ByteMe95::~ByteMe95()
My S(h)ite
ByteMe95::~ByteMe95()My S(h)ite
quote:
Original post by ByteMe95
Should that actually be:
direction_of_g[0] = blackhole_location[0] - objectlocation[1];
direction_of_g[1] = blackhole_location[1] - objectlocation[1];
Yup! Thanks for the correction. I''ll update my original post.
Graham Rhodes
Senior Scientist
Applied Research Associates, Inc.
Graham Rhodes Moderator, Math & Physics forum @ gamedev.net
quote:
Original post by ByteMe95
Without the friction they pretty much found a nice orbit at some point and never got closer to the singularity.
That makes sense. In reality, the black_hole_g would be increasing as the objects get closer, and so they never find that equilibrium. But with constant *g, they do find that orbit. Your friction is a good less-computationally-expensive to get a good looking effect. (Actually calculating the new g would require a bit more floating point operations.)
quote:
Original post by ByteMe95
But looks good now
Now I just have to find out how to render a wormhole
Cool!
Graham Rhodes
Senior Scientist
Applied Research Associates, Inc.
Graham Rhodes Moderator, Math & Physics forum @ gamedev.net
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement