Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 19 of 19

Thread: Java Math/Physics Help

  1. #1
    Junior Member
    Join Date
    Jul 2014
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Java Math/Physics Help

    As I am very new to this forum (i.e. 10 minutes ago) please forgive any formatting errors I may create.

    I am attempting to create a 2d game that has a realistic space simulation. The map is supposed to be set so that you are looking from the top down. The problem I am having exists in my interaction between the sun and a planet. Simply put, the planet will only move in a single diagonal direction without ever changing direction. The formulas I am using are F=(G*M1*M2)/D^2 and A=F/M. If there are better formulas for my purpose please tell me. The code below is simplified so that I do not have to copy over the entire class for it to make sense.

    (inside planet Class)
    double x;
    double y;
    double mass;
    double xForce;
    double yForce;
    double xAccel;
    double yAccel;
    double xVel;
    double yVel;
     
    (Inside SolarSystem Class)
    double sunX;
    double sunY;
    double sunMass;
    ArrayList<Planet> planets;
     
    public void update()
    xAccel=getAccelX();
    yAccel=getAccelY();
    xVel+=xAccel;
    yVel+=yAccel;
    x+=xVel;
    y+=yVel;
     
    public double getAccelX()
    double G=6.67*Math.pow(10,-11);
    double D=Math.pow(sunX-x,2);
    xForce=(G*sunMass*mass)/D;
    return xForce/mass;
     
    public double getAccelY()
    double G=6.67*Math.pow(10,-11);
    double D=Math.pow(sunY-y,2);
    xForce=(G*sunMass*mass)/D;
    return yForce/mass;

    Things I have already tried:
    1. Setting G to negative
    2. changing the order of the subtraction in determining D
    3. some weird pythagorean theorem thing someone suggested


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    6,097
    My Mood
    Mellow
    Thanks
    244
    Thanked 754 Times in 740 Posts

    Default Re: Java Math/Physics Help

    Welcome to the forum! Thanks for taking the time to learn how to post code correctly. If you haven't already, please read this topic to learn other useful info for new members.

    What does "top down" mean in space? Describe the player's perspective as a relationship between the planet and the sun. Describe what the player currently sees (the current undesired operation) and what causes the change to the view, then describe the desired effect.

    Even better, post a short piece of code that is compilable and runnable that demonstrates the problem. We could look at equations all day long and not know how they relate to your game space.

  3. The Following User Says Thank You to GregBrannon For This Useful Post:

    Strauscon (August 1st, 2014)

  4. #3
    Junior Member
    Join Date
    Jul 2014
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Java Math/Physics Help

    By "top down" I mean that if you consider a standard graph with X and Y axis then add another axis (I'll call this the Z axis) so that the Z axis is perpendicular to both other axis at the same time you would only see the X and Z axis from a "top down" view. A better way of phrasing it is probably "from top, facing down".

    Because I organized my code so that it is easy to expand upon and that I am not very good when it comes to graphics, my game is currently spread across 5+ classes and I am not sure how to compress that into a short, runnable code segment. I am currently using a core class that draws a black screen and uses fillOval for the sun and planet.

    Below I have copied all of the interaction between the sun and planet in currently in my code just in case it helps. It may help if I point out that both the SolarSystem and Planet classes have a superclass called Body. This superclass only contains doubles (x, y, and mass), a String (name), a constructor, and getters and setters.

    From SolarSystem Class:

    @Override
    	public void update(long ticks) {
    		// run interactions between planets and solar systems
    		for(int i=0; i<planets.size(); i++)
    		{
    			planets.get(i).setxAccel(genAccelX(i));
    			planets.get(i).setyAccel(genAccelY(i));
    		}
     
    		// update using acceleration and velocity
    		for(int i=0; i<planets.size(); i++)
    		{
    			planets.get(i).update(ticks);
    		}
     
    	}
    public double genAccelX(int i)
    	{
    		double x1=0;
    		double x2=0;
    		/*if(getX()>planets.get(i).getX())
    		{*/
    			x1=getX();
    			x2=planets.get(i).getX();
    		/*}
    		else
    		{
    			x1=planets.get(i).getX();
    			x2=getX();
    		}*/
    	    double G=6.67*Math.pow(10, -11);
    	    double D=x1-x2;
    	    planets.get(i).setxForce((G*getMass()*planets.get(i).getMass())/D);
    	    return planets.get(i).getxForce()/planets.get(i).getMass();
    	}
    	public double genAccelY(int i)
    	{
    		double y1=0;
    		double y2=0;
    		/*if(getY()<planets.get(i).getY())
    		{*/
    			y1=getY();
    			y2=planets.get(i).getY();
    		/*}
    		else
    		{
    			y1=planets.get(i).getY();
    			y2=getY();
    		}*/
    	    double G=6.67*Math.pow(10, -11);
    	    double D=y1-y2;
    	    planets.get(i).setyForce((G*getMass()*planets.get(i).getMass())/D);
    	    return planets.get(i).getyForce()/planets.get(i).getMass();
    	}

    From Planet Class:

    @Override
    	public void update(long ticks) {
    		xVel+=xAccel;
    		yVel+=yAccel;
    		x+=xVel;
    		y+=yVel;
    		System.out.println("X: "+(int)x+"\t\t"+"Y: "+(int)y);
    		System.out.println("XVel: "+xVel+"\t\t"+"YVel: "+yVel);
    		System.out.println("XAccel: "+xAccel+"\t"+"YAccel: "+yAccel);
     
    	}

    Aside from these methods the class have basic draw methods and the usual getters and setters for each variable.

  5. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    6,097
    My Mood
    Mellow
    Thanks
    244
    Thanked 754 Times in 740 Posts

    Default Re: Java Math/Physics Help

    So if x, y, and z are used to define the coordinates of your solar system: The sun is at (0, 0, 0) and planets rotate about the sun in the x-z plane (for now). Is that correct? If so, your planet should move in an orbit about (0, 0, 0). For a simple case, that orbit might be a circle or an ellipse centered at (0, 0, 0) that varies according to an equation in x and z, because y will be 0. Start with that. If you decide the orbital equation should also include y, then add that later. Start easy and grow from there.

    With that in mind, what is the equation that currently describes the orbit of the planet around the sun?

  6. #5
    Junior Member
    Join Date
    Jul 2014
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Java Math/Physics Help

    That is the idea, yes. In my code Y kind of replaces Z for two reasons. The first is that computer screens are in terms of X and Y, and the second is that I simply didn't think of the Z thing until this morning.

    I don't really have a great grasp of physics when it comes to space, but what I was going for is not that the equation necessarily describes the orbit. It is closer to the equation(s) changing the acceleration, which in turn moves the planets. Due to the fact that when I start the only information I have is the distance between objects and their mass I used the F=(G*M1*M2)/D^2 formula first and then used that force and the planets mass to determine the planets acceleration with the A=F/M formula.

  7. #6
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    6,097
    My Mood
    Mellow
    Thanks
    244
    Thanked 754 Times in 740 Posts

    Default Re: Java Math/Physics Help

    And why do you care about the planet's acceleration? Or force? How does that help you program the motion of the planet? I understand F = MA, but I don't understand why you care. How is that important in your game?

  8. #7
    Junior Member
    Join Date
    Jul 2014
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Java Math/Physics Help

    I want to make how the planets interact with the sun (and maybe things like asteroids and moons in the future) as realistic as possible. I partly care just because I am extremely OCD as well.

  9. #8
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    6,097
    My Mood
    Mellow
    Thanks
    244
    Thanked 754 Times in 740 Posts

    Default Re: Java Math/Physics Help

    Okay, but I'm not sure being extremely OCD is good for a programmer. Software always has bugs.

    But to your current question, then the equations of motion of a planet around the sun will only change when acted upon by another body - another significant body. That's what you need to model. Before you spend time doing that, you might consider that our own solar system has reached a steady state condition. Except for the expansion of the Universe occurring as a result of the Big Bang, the motions of the planets around their suns among all of the solar systems within the Universe change very little, if at all, Within the time frame of your game, how much is your solar system going to change, and what's going to cause that change?

    I think you're over thinking it, but I'm not OCD.

  10. #9
    Junior Member
    Join Date
    Jul 2014
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Java Math/Physics Help

    After the initial set up of the map the idea was that the only events that could change the solar system were collisions between planets or asteroids (which would change mass or remove an object) and maybe player terraforming later in the game. Outside of that there were really no other ideas that could contribute to change.

    Another plan was for the sun(s) to be set to a fixed point so that none of the suns could ever collide.

  11. #10
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    6,097
    My Mood
    Mellow
    Thanks
    244
    Thanked 754 Times in 740 Posts

    Default Re: Java Math/Physics Help

    But collisions between planets suggests something caused the planets to get in each other's way - their orbits intersected somehow. What caused that? And collisions between asteroids impacts the asteroids but not the planets.

    Colliding suns? How would that happen? How do solar systems get in each other's way? What would happen IF that happened? Game over?

  12. #11
    Junior Member
    Join Date
    Jul 2014
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Java Math/Physics Help

    The idea eventually is that the planets all affect each other's orbit, similar to our solar system. There are numerous ways that could happen. For example: say a player terraforms a planet and lowers its mass enough that it moves into another planet's orbit. When I said asteroids I also meant asteroid x planet collision.

    The idea is that the suns could never collide, simply because I have no idea what happens when two suns collide, but I assume it is destructive.

    It may help you understand if I say this: The idea is that it is similar to the Civilization games, but entirely in space. I haven't really decided what would happen when objects collide because I haven't gotten past the realistic gravity problem.

  13. #12
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    6,097
    My Mood
    Mellow
    Thanks
    244
    Thanked 754 Times in 740 Posts

    Default Re: Java Math/Physics Help

    There are numerous ways that could happen. For example: say a player terraforms a planet and lowers its mass enough that it moves into another planet's orbit. When I said asteroids I also meant asteroid x planet collision.
    So then you're imagining a pretty unstable solar system with its own rules of physics that you'll have to create. By its own rules, I'm referring to your statement, ". . . a player terraforms a planet and lowers its mass . . ." First, I'm surprised the rules of physics in your system would reduce mass by adding something, but more importantly, that the rules of physics in your system allow changes in the mass of closed systems at all.

    I've tried to gently point out that your ideas about the rules of physics in your world are not consistent with the rules of Physics we currently understand by observing the Universe in which we live. Solar systems are not that fragile, orbits are stable, predictable, and resistant to change, and the mass of planets or closed systems does not change significantly enough to alter that. Since the universe you're imagining is significantly different than the one in which you live, there aren't equations that describe how bodies will react to each other in your universe. You'll have to create them.

    For example, asteroids (meteors) impact planets in our Solar System all the time. We know this by observing the craters on the moon and on other planets. How much of an impact have those collisions had?

    You'll have to create your own rules where meteors impacting planets have a disproportionate effect. I suggest that you start by creating the equations that define your system in its stable state, and then define the variations to those equations that will occur due to the changes you've described and have yet to imagine. F = MA is different in your universe. The Law of Conservation of Mass doesn't exist in your system. Maybe the speed of light is different and e != Mc^2. Once you change any one of those, the equations we currently have don't apply to your system.

    And then somehow bring the topic back to Java.

  14. #13
    Junior Member
    Join Date
    Jul 2014
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Java Math/Physics Help

    By players lowering mass through terraforming I somewhat meant removing materials from the planet. I understand that through terraforming the player could also add mass. The removing of mass was just one specific example.

    I did manage to completely forget the conservation of mass when thinking about impacts, so I guess for it to be realistic the masses of colliding objects end results would have to be equal to their total mass before the collision. Also the momentum would probably have to be conserved the same way in a collision.

    This is very helpful in getting an understanding of how I should be coding the game in the future, but is it okay with you if we shift to one specific problem that I currently have?

    If so, that problem is that the gravity interaction doesn't even slightly work. I understand that if my view of the universe is slightly skewed the same formula may not apply, but I haven't implemented any physics except for the currently broken gravity. I followed the the best I could, but I have no idea where to go from here.

  15. #14
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    6,097
    My Mood
    Mellow
    Thanks
    244
    Thanked 754 Times in 740 Posts

    Default Re: Java Math/Physics Help

    Explain how gravity is broken in your game's universe. What is gravity currently doing, what code defines its behavior, and how would you like that behavior to change? If possible, post runnable code that demonstrates the problem. If not possible, then I'm not sure how we'll find a common perspective from which to address the behavior your perceive to be a problem.

  16. #15
    Junior Member
    Join Date
    Jul 2014
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Java Math/Physics Help

    Currently the planet moves away from the sun towards the bottom right without ever changing direction. I can post a zip file of the code if you want, but I don't know how.

    If it is easier for me to copy all of the classes than to upload a file please say so.

  17. #16
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    6,097
    My Mood
    Mellow
    Thanks
    244
    Thanked 754 Times in 740 Posts

    Default Re: Java Math/Physics Help

    Does the planet follow an orbit defined by an equation? If not, why not?

  18. #17
    Junior Member
    Join Date
    Jul 2014
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Java Math/Physics Help

    The planet follows the equation, but only moves towards the bottom right. I believe this to be because I misused the equation when I moved it to java.

  19. #18
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    6,097
    My Mood
    Mellow
    Thanks
    244
    Thanked 754 Times in 740 Posts

    Default Re: Java Math/Physics Help

    Here's another approach you may find acceptable and helpful: There are a number of Applets on the web that demonstrate Physics, including Astronomy. Unfortunately, most of the Applets are broken because of the recent security concerns with Applets and the changes made to Java and browser plugins to protect us from them. However, there are some sites with broken Applets that share the code behind them. Here's an example of one of those, and I think you'll be able to find others if you look.

  20. #19
    Junior Member
    Join Date
    Jul 2014
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Java Math/Physics Help

    I looked at a bunch of simulations before coming here, for example: My Solar System 2.04. Many of the ones I found either did not work or did not show how they were coded. The one you linked does not work on my computer for some reason, I just get an applet blocked error.

Similar Threads

  1. Question about math.pow and math.sqrt
    By ggx7 in forum Java Theory & Questions
    Replies: 7
    Last Post: March 7th, 2014, 12:51 PM
  2. Need help with my code (physics pendulum)
    By geezlouise in forum What's Wrong With My Code?
    Replies: 11
    Last Post: March 30th, 2013, 08:23 PM
  3. May Term project: Physics-based games
    By NcAdams in forum Member Introductions
    Replies: 0
    Last Post: May 22nd, 2012, 03:11 PM
  4. Confusion with Math.toDegrees() and Math.toRadians(). Please help.
    By marksquall in forum Java Theory & Questions
    Replies: 3
    Last Post: June 23rd, 2011, 02:28 AM
  5. [SOLVED] Physics Program
    By pwngrammer in forum Algorithms & Recursion
    Replies: 36
    Last Post: June 15th, 2009, 09:32 AM

Tags for this Thread