Gå til innhold

Anbefalte innlegg

Driver å lager begynnelsen på en slags simulator i 2d og har selvfølgelig støtt på problemer allerede--men her er ett jeg ikke skjønner bæret av...

 

Jeg vil lagre objekter (Sphere) i et array--ArrayList--så jeg kan loope gjennom de senere, men hver gang jeg prøver å compile det får jeg feilmeldinger.

 

import java.awt.*;
import java.applet.*;
import java.util.*;

public class PhysX extends Applet implements Runnable
{
Thread aniThr;

Arena arena = new Arena(800, 600);
ArrayList<Sphere> obj = new ArrayList<Sphere>();
obj.add(new Sphere("Blue Ball", new Position(0, 0), new Vector(Math.toRadians(70), 3), 5, 7, new Color(0, 0, 255))); // < her får jeg problemer
obj.add(new Sphere("Red Ball", new Position(-130, -90), new Vector(Math.toRadians(76), 2), 7, 9, new Color(255, 0, 0))); // < her får jeg problemer
obj.add(new Sphere("Green Ball", new Position(120, 50), new Vector(Math.toRadians(-45), 2), 12, 15, new Color(0, 255, 0))); // < her får jeg problemer

public void init()
{
 setBackground(new Color(230, 230, 230));
}

public void start()
{
 if (aniThr == null)
 {
 	aniThr = new Thread(this);
 	aniThr.start();
 }
}

public void stop()
{
 aniThr = null;
}

public void run()
{
 while (aniThr != null)
 {
 	for (int i = 0; i < obj.size(); i++)
 	{
   obj.get(i).arenaCollision(arena);
   obj.get(i).move();
 	}
 	
 	repaint();
 	try
 	{
   Thread.sleep(40);
 	}
 	catch (InterruptedException e)
 	{
   return;
 	}
 }
}

public void paint(Graphics g)
{
 for (int i = 0; i < obj.size(); i++)
 {
 	g.setColor(obj.get(i).color);
 	double xPos = arena.width / 2 + (obj.get(i).pos.x - obj.get(i).radius) / Global.METERS_PER_PIXEL;
 	double yPos = arena.height / 2 + (obj.get(i).pos.y - obj.get(i).radius) / Global.METERS_PER_PIXEL;
 	double diameter = (obj.get(i).radius * 2) / Global.METERS_PER_PIXEL;
 	g.fillOval((int)xPos, (int)yPos, (int)diameter, (int)diameter);
 }
}
}

class Global
{
public static double METERS_PER_PIXEL = 1;
public static double GAMMA = 6.67 * Math.pow(10, -11);
}

class Arena
{
int width;
int height;

public Arena(int width, int height)
{
 this.width = width;
 this.height = height;
}
}

class Position
{
double x;
double y;

public Position(double x, double y)
{
 this.x = x;
 this.y = y;
}
}

class Vector
{
double angle;
double mag;

public Vector(double angle, double mag)
{
 this.angle = angle;
 this.mag = mag;
}
}

class Sphere
{
String name;
Position pos;
Vector velocity;
double mass;
double radius;
Color color;

public Sphere(String name, Position pos, Vector velocity, double mass, double radius, Color color)
{
 this.name = name;
 this.pos = pos;
 this.velocity = velocity;
 this.mass = mass;
 this.radius = radius;
 this.color = color;
}

public double getDistance(Sphere other)
{
 double distanceX = other.pos.x - this.pos.x;
 double distanceY = this.pos.y - other.pos.y;
 return Math.sqrt(Math.pow(distanceX, 2) + Math.pow(distanceY, 2));
}

public double getAngle(Sphere other)
{
 double distanceX = other.pos.x - this.pos.x;
 double distanceY = this.pos.y - other.pos.y;
 return Math.atan2(distanceX, distanceY);
}

public void arenaCollision(Arena arena)
{
    if (this.pos.x - this.radius < (-arena.width / 2) * Global.METERS_PER_PIXEL)
    {
     this.pos.x = (-arena.width / 2) * Global.METERS_PER_PIXEL + this.radius;
     this.velocity.angle = 2 * Math.PI - this.velocity.angle;
    }
    else if (this.pos.x + this.radius > (arena.width / 2) * Global.METERS_PER_PIXEL)
    {
     this.pos.x = (arena.width / 2) * Global.METERS_PER_PIXEL - this.radius;
     this.velocity.angle = -this.velocity.angle;
    }
    if (this.pos.y - this.radius < (-arena.height / 2) * Global.METERS_PER_PIXEL)
    {
     this.pos.y = (-arena.height / 2) * Global.METERS_PER_PIXEL + this.radius;
     this.velocity.angle = -Math.PI - this.velocity.angle;
    }
    else if (this.pos.y + this.radius > (arena.height / 2) * Global.METERS_PER_PIXEL)
    {
     this.pos.y = (arena.height / 2) * Global.METERS_PER_PIXEL - this.radius;
     this.velocity.angle = Math.PI - this.velocity.angle;
    }
}

public void objectCollision(Sphere subobj)
{
 double distance = this.getDistance(subobj);
 if (distance - this.radius - subobj.radius < 0)
 {
 	// get angle to b from a
 	double objAngle = this.getAngle(subobj);
 	// get angle to a from b
 	double subobjAngle = subobj.getAngle(this);
 	
 	// decompose speeds on collision plane (collision plane: line from center this to center subobj where x is along plane and y is normal to plane)
 	double objPlaneXSpeed = this.velocity.mag * Math.cos(this.velocity.angle - objAngle);
 	double objPlaneYSpeed = this.velocity.mag * Math.sin(this.velocity.angle - objAngle);
 	double subobjPlaneXSpeed = subobj.velocity.mag * Math.cos(subobj.velocity.angle - objAngle);
 	double subobjPlaneYSpeed = subobj.velocity.mag * Math.sin(subobj.velocity.angle - objAngle);
 	
 	// calculate new speeds in x-direction
 	double objPlaneNewXSpeed = (objPlaneXSpeed * (this.mass - subobj.mass) + 2 * subobj.mass * subobjPlaneXSpeed) / (this.mass + subobj.mass);
 	double objPlaneNewYSpeed = objPlaneYSpeed; // leave unchanged in y-direction
 	double subobjPlaneNewXSpeed = (subobjPlaneXSpeed * (subobj.mass - this.mass) + 2 * this.mass * objPlaneXSpeed) / (this.mass + subobj.mass);
 	double subobjPlaneNewYSpeed = subobjPlaneYSpeed; // leave unchanged in y-direction
 	
 	// get new polar speed
 	this.velocity.mag = Math.sqrt(Math.pow(objPlaneNewXSpeed, 2) + Math.pow(objPlaneNewYSpeed, 2));
 	subobj.velocity.mag = Math.sqrt(Math.pow(subobjPlaneNewXSpeed, 2) + Math.pow(subobjPlaneNewYSpeed, 2));
 	
 	// get new polar angle (first part is angle from plane, then second part adds the angle arena-plane)
 	this.velocity.angle = Math.atan2(objPlaneNewYSpeed, objPlaneNewXSpeed) + objAngle;
 	subobj.velocity.angle = Math.atan2(subobjPlaneNewYSpeed, subobjPlaneNewXSpeed) + objAngle;
 	
 	// move object out of collision zone
 	this.pos.x = subobj.pos.x + ((this.radius + subobj.radius) * Math.sin(subobjAngle));
 	this.pos.y = subobj.pos.y + ((this.radius + subobj.radius) * Math.cos(subobjAngle) * -1);
 }
}

public void move()
{
 this.pos.x += this.velocity.mag * Math.sin(this.velocity.angle);
 this.pos.y += this.velocity.mag * Math.cos(this.velocity.angle) * -1;
}
}

 

---

 

Spørsmål 2. Som dere sikkert ser her, har jeg laget en egen abstrakt klasse bare for å holde globale variabler. Er det ikke noen måte i Java å gjøre dette på en litt mer logisk måte? F eks deklarere dem utenfor en klasse på toppen og la de være tilgjengelige for alle klassene? Jeg veit at klassene er koden i Java så det går jo ikke, men noen forslag? Denne måten er jo dum hvertfall... Global.KONSTANT_VARIABEL hver gang jeg skal ha tak i den. Bedre med KONSTANT_VARIABEL...

 

Lost

Lenke til kommentar
Videoannonse
Annonse

Hvis du flytter de problematiske linjene inn i init() så er det en god start. Hva angår bruken din av globale variabler så vil jeg heller kalle dem konstanter, og måten du gjør det på er ganske vanlig.

 

Hilsen Werner

Lenke til kommentar

Opprett en konto eller logg inn for å kommentere

Du må være et medlem for å kunne skrive en kommentar

Opprett konto

Det er enkelt å melde seg inn for å starte en ny konto!

Start en konto

Logg inn

Har du allerede en konto? Logg inn her.

Logg inn nå
×
×
  • Opprett ny...