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:
GameFrame:
Ich hoffe ihr könnt mir helfen.
Gruss Tobi
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