Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8725567
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T08:04:07+00:00 2026-06-13T08:04:07+00:00

I am trying to tackle the Nbody example in my java class (this is

  • 0

I am trying to tackle the Nbody example in my java class (this is my first semester in java).

The Details of the program are as follows:

Write a program Nbody.java that reads in the universe from standard input using Scanner, simulates its dynamics using the leapfrog scheme described above, and animates it using our StdDraw. Maintain several arrays to store the data. To make the computer simulation, write an infinite loop that repeatedly updates the position and velocity of the particles. When plotting, consider using StdDraw.setXscale(-R, +R) and StdDraw.setYscale(-R, +R) to scale the physics coordinates to the screen coordinates.

I’ve written the following code which compiles & loads the background image, the song, & the planets with correct placement. However, I cannot get the plantes to rotate as they should.

Here is my code:

import java.util.Scanner;

public class Nbody {

// method dist calculates distance between two points
// it accepts four double values (x1, y1, x2, y2)
// it returns a double value
private static double dist(double x1, double y1, double x2, double y2)

{

     double r;
     r = Math.sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2));
     return r;
} 


public static void main(String[] args) {

    final double G = 6.67e-11; // constant value G

    final double deltaT = 25000.0; // constant value delta T, which determines animation interval

    // ax and ay are accerlations on x axis and y axis
    double ax;
    double ay;


    Scanner scan = new Scanner (System.in); // define a keyboard object: scan

    int bodyNum = scan.nextInt(); // read in the first integer, which specify how many particles
    double radius = scan.nextDouble(); // read in radius of universe


    // define arrays to save x values and y values of each particle
    double[] x = new double[bodyNum];
    double[] y = new double[bodyNum];

    // define arrays to save velocity of each particle
    double[] vx = new double[bodyNum];
    double[] vy = new double[bodyNum];

    double[] m = new double[bodyNum];  // array stores mass of each particle
    String[] name = new String[bodyNum]; // array stores file name of each particles
    double[] Fx = new double[bodyNum]; // force on X axis
    double[] Fy = new double[bodyNum]; // force on Y axis

    StdDraw.setXscale(-radius, radius); // Set the X scale
    StdDraw.setYscale(-radius, radius); // Set the Y scale
    StdDraw.picture(0, 0, "starfield.jpg"); // Display background pic centered at (0,0)

    // read in inital location values and velocity values for each particle

    for (int i = 0; i < bodyNum; i++)

    {
       x[i] = scan.nextDouble();
       y[i] = scan.nextDouble();
       vx[i] = scan.nextDouble();
       vy[i] = scan.nextDouble();
       m[i] = scan.nextDouble();
       name[i] = scan.next();
       StdDraw.picture(x[i], y[i], name[i]); // display particle pic on screen
    }

    // This following line play background music, uncomment it in lab
    // If you work from a remote location via VNC, comment it
    StdAudio.play("2001.mid");

    // main animation loop
    while(true){

        // array Fx and Fy store net force acting on each body
        // initialize these two arrays to zeros

        for (int n = 0; n<bodyNum; n++)

        {
           Fx[n] = 0.0;
           Fy[n] = 0.0;
        }

         //  for loop to process all bodies
         for(int body = 0; body<bodyNum; body++)

            {

            // calculate the gravitational attraction between current body
            // and all other bodies

            Fx[body] = (m[body] * m[body+1])  / (x[body+1] -x[body])*G;

            Fy[body] = (m[body] * m[body+1]) / (y[body+1] -y[body])*G;

                for (int j = 0; j < bodyNum; j++)

                {

                 // calculate only when two bodies are different
                 // Please fill out the following if statement body                 

                 if(body != j)

                 {

                 Fx[j] = (m[j] * m[j+1]) / (y[j+1] -y[j])*G;
                 Fy[j] = (m[j] * m[j+1]) / (y[j+1] -y[j])*G;


                 }


                }
            }

         // update vleocity value and location value for each particle
         // please fill out the for loop
         for (int j = 0; j < bodyNum; j++)

            {

             // calculate accleration rate
             ax = Fx[j] / m[j];
             ay = Fy[j] / m[j];

             // update vleocity value 

            vx[j] = vx[j] + deltaT * ax;
            vy[j] = vy[j] + deltaT * ay;

            // update location value

            x[j] = x[j] + deltaT * vx[j];
            y[j] = y[j] + deltaT * vy[j];


            } 

         // redraw background
         StdDraw.setXscale(-radius, radius);
         StdDraw.setYscale(-radius, radius);
         StdDraw.picture(0, 0, "starfield.jpg");

         for (int i = 0; i < bodyNum; i++){
             // display the particle           
             StdDraw.picture(x[i], y[i], name[i]);
         }

         // display and pause for 30ms      
         StdDraw.show(30);        
    }
}
}

Any thoughts?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T08:04:08+00:00Added an answer on June 13, 2026 at 8:04 am

    Your force calculations seem quite a bit off to me. I’d do it like this:

    Arrays.fill(Fx, 0.0);
    Arrays.fill(Fy, 0.0);
    for (int body = 0; body < bodyNum - 1; body++) {
        for (int body2 = body + 1; body2 < bodyNum; body2++) {
            double dx = x[body2] - x[body];
            double dy = y[body2] - y[body];
            double d2 = dx * dx + dy * dy;
            double d = Math.sqrt(d2);
            double f = G * m[body] * m[body2] / d2;
            double fx = f * dx / d;
            double fy = f * dy / d;
            Fx[body] += fx;
            Fy[body] += fy;
            Fx[body2] -= fx;
            Fy[body2] -= fy;
        }
    }
    // then update positions and velocities based on Fx and Fy arrays
    

    This computes the x and y components of the force acting on body due to body2 and then adds that to the x and y force components for body and subtracts them from body2. By indexing from body + 1, we can account for equal-and-opposite forces on two bodies in the same inner loop pass, reducing the number of calculations by half.

    For updating positions, you are using the velocity at the end of the time delta, as if the bodies were moving for the entire delta at the velocity at the end of the delta. It might be more accurate to do some sort of interpolation, but that should be a second order effect.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Trying to make this jQuery filter that uses .find case-insensitive. For example, when the
I have this problem I've been trying to tackle for a while. I have
I was wondering about the best way to tackle this. I'm trying to save
I've been trying to tackle this for hours. Ive even looked at other similar
I am trying to figure out how to tackle this problem. I have to
I'm trying to figure out what the best way to tackle this would be.
Hi there guys trying to gather some ideas to tackle this problem. i am
I am trying to determine how I want to tackle this... this is all
I am trying to tackle a program take the given input file is in.properties
I've been trying to tackle this crash and just don't know where to begin.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.