Gravitations-Simulator

player337

Mitglied
Hi,
ich programmiere derzeit ein "Sonnensystem".
Die Anziehungskraft funktioniert auch schon,
doch ich hab das Problem dass die Anziehungskraft mit zunehmendem Abstand nicht geringer wird,
sondern stärker. ???:L

Hier die Main Klasse:

Java:
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.image.BufferedImage;


public class Main
{
	private static GameFrame frame;
	private static BufferedImage screen = new BufferedImage(720, 720, BufferedImage.TYPE_INT_RGB);
	
	private static Point sun = new Point(400, 400);
	private static Point velocity = new Point(0, 10);
	private static Point gforce = new Point(0, 0);
	private static Point pos = new Point(300, 300);
	
	private static int stop = 0;
	
	public static void main(String[] args)
	{
		frame = new GameFrame("Gravity", 720, 720);
		frame.startGame();
	}

	public static void update()
	{	
		if(sun.x-pos.x<300 && sun.y-pos.y<300)
		{
			gforce.x = (int) ((sun.x-pos.x)/30*0.5);
			gforce.y = (int) ((sun.y-pos.y)/30*0.5);
			
			System.out.println(gforce.x*gforce.y+"G");
		}
		
		velocity.x += gforce.x;
		velocity.y += gforce.y;
		
		pos.x += velocity.x;
		pos.y += velocity.y;
		

	}

	public static void draw()
	{
		screen = new BufferedImage(720, 720, BufferedImage.TYPE_INT_RGB);
		Graphics g = screen.getGraphics(); //Get graphics
		
		g.setColor(Color.CYAN);
		g.fillRect(pos.x, pos.y, 10, 10);
		g.setColor(Color.ORANGE);
		g.fillOval(sun.x, sun.y, 20, 20);
		
		frame.getGraphics().drawImage(screen, 0, 0, null);
	}

}

GameFrame:

Java:
import java.awt.Graphics;
import javax.swing.JFrame;

public class GameFrame implements Runnable
{
	private JFrame frame;
	private	String FrameTitle;
	private Thread t;
	private	int width;
	private	int height;
	private static boolean runFlag = true;

	private final int TARGET_FPS = 20;  
	private int updates = 0;
	private int frames = 0;
	private int FPS = 0;
	
	public GameFrame(String FrameTitle, int width, int height)
	{
		this.FrameTitle = FrameTitle;
		this.width = width;
		this. height = height;
		
		frame = new JFrame();
		frame.setTitle(FrameTitle);
		frame.setSize(width, height);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setResizable(false);
		frame.setVisible(false);
		
		t = new Thread(this);
	}
	
	public void startGame()
	{
		frame.setVisible(true);
		t.start();
	}
	
	public static void pauseGame()
	{
		runFlag = false;
	}

	public String getTitle()
	{
		return this.FrameTitle;
	}
	
	public int getWidth()
	{
		return this.width;
	}
	
	public int getHeight()
	{
		return this.height;
	}
	
	public Graphics getGraphics()
	{
		return frame.getGraphics();
	}
	
	public int getFPS()
	{
		return FPS;
	}

	public void run()
	{
		long lastTime = System.nanoTime();
		double ns = 1000000000 / TARGET_FPS;
		double delta = 0;
		long timer = System.currentTimeMillis();
		int updates = 0;
		int frames = 0;
		while(runFlag){
			long now = System.nanoTime();
			delta += (now - lastTime) / ns;
			lastTime = now;
			while(delta >= 1){
				Main.update();
				updates++;
				delta--;
			}
			Main.draw();
			frames++;
					
			if(System.currentTimeMillis() - timer > 1000){
				timer += 1000;
				System.out.println("FPS: " + frames + " TICKS: " + updates);
				FPS = frames;
				frames = 0;
				updates = 0;
			}
		} 
		
	}
}

Ich hoffe ihr könnt mir helfen.

Gruss Tobi
 

Deros

Bekanntes Mitglied
Moin,

Java:
gforce.x = (int) ((sun.x-pos.x)/30*0.5);
gforce.y = (int) ((sun.y-pos.y)/30*0.5);

die stelle hast du doch scheinbar schon gefunden. Der Rest ist Mathematik.
Desto weiter Weg desto größer ist der Betrag von sun.x-pos.x.
Wenn der Punkt in der Sonne liegt heben sich die Werte (400-400=0) auf und die gforce liegt bei 0.

Ist der cast auf int eigentlich wirklich absicht oder nur damit du Point nutzen kannst?
 

player337

Mitglied
Habe es nur gemacht damit Point nutzen kann.
Ich habe ein neue Methode, aber immer noch das gleiche Problem. ;(
 
Zuletzt bearbeitet:

player337

Mitglied
Hi,
hab die Lösung!!! :D
Sie ist das C++-Programm von https://www.youtube.com/user/GuessGen,
wessen ich in Java umgeschrieben habe.

Code der Main-Klasse:
Java:
import java.util.LinkedList;
import org.lwjgl.LWJGLException;
import org.lwjgl.input.Keyboard;
import org.lwjgl.input.Mouse;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GL11;


public class Main
{
	public final static float CIRCLE_PRECISION = 0.1f;
	
	public static LinkedList<Object> objects = new LinkedList<Object>();
	public static float Lx1, Ly1, Lx2, Ly2, Mx, My;

	private static boolean CLICKED;

	private static boolean RELEASED;
	
	public static void main(String[] args)
	{
		//addObject(10000, 20, true, 0, 0, 0, 0);
		
		Mouse.setGrabbed(true);
		
		try
		{
			Display.setDisplayMode(new DisplayMode(720, 720));
			Display.setTitle("");
			Display.create();
			Keyboard.create();
			
			GL11.glClearColor(0, 0, 0, 1);
			GL11.glLineWidth(2.5f);
			GL11.glMatrixMode(GL11.GL_PROJECTION);
			GL11.glLoadIdentity();
			GL11.glOrtho(-310, 310.0, 310.0, -310.0, 0, 1);

		}
		catch (LWJGLException e)
		{
			e.printStackTrace();
		}
		
		while(!Display.isCloseRequested())
		{
			Display.update();
			Display.sync(60);
			mouse();
			update();
			draw();
		}
		System.exit(0);
	}
		
	private static void mouse()
	{
		Mouse.poll();
		Mx = -(310-Mouse.getX());
		My = (310-Mouse.getY());
		
		
		
		
		if(CLICKED)
		{
			Lx1 = Mx;
			Ly1 = My;
			CLICKED = false;
		}
		if(RELEASED)
		{
			addObject(10.0f, 10, false, Mx, My, Lx1-Mx, Ly1-My);
			RELEASED = false;
		}
		
		while (Mouse.next())
		{
		    if (Mouse.getEventButtonState()) 
		    {
		        if (Mouse.getEventButton() == 0) 
		            CLICKED = true;
		    }
		    else 
		    {
		        if (Mouse.getEventButton() == 0) 
		        	RELEASED = true;
		        else
		        	RELEASED = false;
		    }
		}
		
		System.out.println(CLICKED);
	}

	public static void update()
	{
		for(int i = 0; i < objects.size(); i++)
		{
			Object o = objects.get(i);
			boolean not_fall = true;
			for(int j = 0; j < objects.size(); j++)
			{
				if(j == i || o.M >= 10000) // we consider the 10000 as infinit (big mass) so this particles won't move
					continue; 

				Object o1 = objects.get(j);

				float d = (float) Math.sqrt((o1.x - o.x)*(o1.x - o.x) + (o1.y - o.y)*(o1.y - o.y));

				if(d > o1.r)
				{
					o.vx += 0.03 * o1.M / (d*d) * (o1.x - o.x)/d; //f = ma => a = f/m
					o.vy += 0.03 * o1.M / (d*d) * (o1.y - o.y)/d;
				}
				else
					not_fall = false;
			}

			if(not_fall)	
			{
				o.x += o.vx;
				o.y += o.vy;
			}
			else
				objects.remove(i);
			
			if(o.x < -(Display.getWidth()/2) || o.x > Display.getWidth()/2 || o.y < -(Display.getHeight()/2) || o.y > Display.getHeight()/2)
				objects.remove(i);
		}

	}

	public static void draw()
	{
		GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);
		
		//Draw Objects
		for(int o = 0; o < objects.size(); o++)
		{
			Object obj = objects.get(o);
			GL11.glColor3f(obj.c[0], obj.c[1], obj.c[2]);
			GL11.glBegin(GL11.GL_POLYGON);
				for(float a = 0; a < 2*Math.PI; a+=CIRCLE_PRECISION)
					GL11.glVertex2d(obj.r*Math.cos(a) + obj.x, obj.r*Math.sin(a) + obj.y);
			GL11.glEnd();
		}
		
		//Draw Mouse
		GL11.glColor3f(255, 255, 255);
		GL11.glRectf(Mx-2, My-2, Mx+2, My+2);
		//Draw Speed Vector
		if(Mouse.isButtonDown(0))
		{
			GL11.glColor3f(255, 0, 0);
			GL11.glBegin(GL11.GL_LINE_STRIP);
			GL11.glVertex2f(Lx1, Ly1);
			GL11.glVertex2f(Mx, My);
			GL11.glEnd();
		}
		GL11.glFlush();
 
	}
	
	public static void addObject(float m, float r, boolean staticO, float x, float y, float vx, float vy)
	{
		Object o;
		if(staticO)
			o = new Object(x, y, m, r, 255, 255, 0);
		else
			o = new Object(x, y, m, r, 0, 0, 255);
		
		o.vx = vx/30;
		o.vy = vy/30;
		
		objects.add(o);
	}
	
	public static void remove()
	{
		objects = new LinkedList<Object>();
	}

}
Object-Klasse:
Java:
public class Object
{
	public float x;
	public float y;
	public float vx;
	public float vy;
	public float M;
	public float r; 
	public int[] c = new int[3];
	
	public Object(float x, float y, float M, float r, int cR, int cG, int cB)
	{
		this.x = x;
		this.y = y;
		this.M = M;
		this.r = r;
		this.c[0] = cR;
		this.c[1] = cG;
		this.c[2] = cB;
	}
}
 
Ähnliche Java Themen

Ähnliche Java Themen


Oben