Note that there are some explanatory texts on larger screens.

plurals
  1. POSimulate the gravitational pull of a star?
    primarykey
    data
    text
    <p>I'm making a game where the player will (<em>on release of mouseclick</em>) shoot a "star" in a certain direction at an initial speed determined by how far he dragged the mouse before releasing. I have a "planet" (<em>stationary circle</em>) on the canvas that I want to exert a gravitational pull on the moving planet. I believe I'm using the right formulas for gravitational force and such, and I have it partially working - the planet affects the planet's trajectory up until a certain point, when the star seems to endlessly speed up and stop changing direction based on it's angle to the star. <strong>Any advice</strong>? (<em>I know that stars aren't supposed to orbit planets, it's the other way around. I coded the whole thing with the names interchanged so forgive that</em>).</p> <p>main class:</p> <pre><code> import acm.graphics.GCompound; import acm.graphics.GImage; import acm.graphics.GLabel; import acm.graphics.GLine; import acm.graphics.GMath; import acm.graphics.GObject; import acm.graphics.GPen; import acm.graphics.GPoint; import acm.graphics.GRect; import acm.graphics.GOval; import acm.graphics.GRectangle; import acm.program.GraphicsProgram; import acm.util.RandomGenerator; import java.awt.Color; import java.awt.event.MouseEvent; import java.util.*; public class Space extends GraphicsProgram { public static int APPLICATION_WIDTH = 1000; public static int APPLICATION_HEIGHT = 1000; private int size = 15; public static double pMass = 1000; public static int sMass = 20; public static double G = 200; private RandomGenerator rand = new RandomGenerator(); GOval planet, tempstar; shootingStar star; GLine line; double accel, xAccel, yAccel, xspeed, yspeed, angle; public void init(){ planet = new GOval(APPLICATION_WIDTH/2, APPLICATION_HEIGHT/2, 30, 30); planet.setFilled(true); planet.setFillColor(rand.nextColor()); add(planet); } public void mousePressed(GPoint point) { // draw a line tempstar = new GOval(point.getX() - size/2, point.getY() - size/2, size, size); tempstar.setFilled(true); tempstar.setColor(rand.nextColor()); add(tempstar); line = new GLine(tempstar.getX() + size/2, tempstar.getY() + size/2, point.getX(), point.getY()); add(line); line.setVisible(true); } public void mouseDragged(GPoint point) { line.setEndPoint(point.getX(), point.getY()); } public void mouseReleased(GPoint point){ xspeed = -.05*GMath.cosDegrees(getAngle(line))*GMath.distance(line.getStartPoint().getX(), line.getStartPoint().getY(), line.getEndPoint().getX(), line.getEndPoint().getY()); yspeed = .05*GMath.sinDegrees(getAngle(line))*GMath.distance(line.getStartPoint().getX(), line.getStartPoint().getY(), line.getEndPoint().getX(), line.getEndPoint().getY()); System.out.println(xspeed + " " + yspeed); star = new shootingStar(xspeed, yspeed, this); if(xspeed != 0) add(star, tempstar.getX(), tempstar.getY()); new Thread(star).start(); remove(tempstar); remove(line); } private double getAngle(GLine line) { return GMath.angle(line.getStartPoint().getX(), line.getStartPoint().getY(), line.getEndPoint().getX(), line.getEndPoint().getY()); } public void checkPlanet(){ accel = .06*GMath.distance(star.getX(), star.getY(), planet.getX(), planet.getY()); angle = correctedAngle(GMath.angle(planet.getX(), planet.getY(), star.getX(), star.getY())); xAccel = accel*GMath.cosDegrees(GMath.angle(planet.getX(), planet.getY(), star.getX(), star.getY())); yAccel = accel*GMath.sinDegrees(GMath.angle(planet.getX(), planet.getY(), star.getX(), star.getY())); double newX = xspeed - xAccel*.01; double newY = yspeed + yAccel*.01; xspeed = newX + xAccel*Math.pow(.01, 2)/2; yspeed = newY + yAccel*Math.pow(.01, 2)/2; star.setSpeed(xspeed, yspeed); } public double correctedAngle(double x) { return (x%360.0+360.0+180.0)%360.0-180.0; } } </code></pre> <p>Pertinent parts of shootingStar class:</p> <pre><code> public void run() { // move the ball by a small interval while (alive) { oneTimeStep(); } } // a helper method, move the ball in each time step private void oneTimeStep() { game1.checkPlanet(); shootingStar.move(xSpeed, ySpeed); pause(20); } public void setSpeed (double xspeed, double yspeed){ xSpeed = xspeed;; ySpeed = yspeed; } } </code></pre> <hr> <p>EDIT:</p> <p>Current Main Class Method:</p> <pre><code> public void checkPlanet(){ double xDistance = star.getX() - planet.getX(); double yDistance = star.getY() - planet.getY(); double distance = Math.sqrt(Math.pow(xDistance, 2) + Math.pow(yDistance, 2)); accel = G*pMass/Math.pow(distance, 2); xAccel = accel * xDistance/distance; yAccel = accel * yDistance/distance; xspeed += xAccel; yspeed += yAccel; star.setSpeed(xspeed, yspeed); } </code></pre> <p>Current Star class Method:</p> <pre><code> public void run() { while (alive) { oneTimeStep(); } } private void oneTimeStep() { game1.checkPlanet(); shootingStar.move(xSpeed, ySpeed); pause(20); } public void setSpeed (double xspeed, double yspeed){ xSpeed = xspeed;; ySpeed = yspeed; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload