Java spaceshooter design

Hey all,

i've been making a spaceshooter in java, but now i'm having design problems. my code is apparantly inefficient and lags like crazy on certain comps, but works fine on good comps. i've experimented with threading, but dont know an efficient way to thread. So far i have a thread to update drawing, a thread to updte movement, and a thread to check 4 all collisions. i also have a bunch of random threads that certain objects create, but thats not that big of a deal. alsoi'm using particle effects to create a shot, but thats the one thats causing the greatest lag as it hsato loop through to draw each tiny piece. Anyway i'm confused right now on how to make an efficient design so i'm asking for ideas and help on how to create a design. plz help all u can and plz dont flame me =(

[811 byte] By [Astrofa] at [2007-10-3 10:57:13]
# 1
Have you tested your game without the particle effects? If there is a significant difference in speed, then post your particle code here, and we can see if we can make it more efficient.Also, I've made many games, and I've never needed a separate thread for collisions.
Nethera at 2007-7-15 6:23:16 > top of Java-index,Other Topics,Java Game Development...
# 2

well 4 particle effects all i do is make a new object with a certain size, and decrement the size every so often, so it makes it appear to move and have a tail. problem is is that each actual shot has like a bunch of particles, and it updates so frequently that if i had 2 enemies fiercely shooting it lags like crazy.

Astrofa at 2007-7-15 6:23:16 > top of Java-index,Other Topics,Java Game Development...
# 3
Well I'd say that's a major problem to have it lagging when just two ships are shooting. But, I can't help you because I have no clue what your code looks like. Maybe you could post some of it?
Nethera at 2007-7-15 6:23:16 > top of Java-index,Other Topics,Java Game Development...
# 4

hmmm aiight. my code is somewhat confusing...wud anyone mind telling me how they wud do it? neway here's my code:

//code from SimpleEnemy.java

EnemyShotPiece p=new EnemyShotPiece((int)Math.round(x), (int)Math.round(y));

pieces.add(p); //pieces is a vector of EnemyShotPiece

int length=pieces.size();

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

{

if(pieces.get(i).radius()><=0)

{

pieces.remove(i);

i-=1;

}

length=pieces.size();

}

//out.println((" "+pieces.size()));

for(EnemyShotPiece ep:pieces)

{

ep.move();

ep.draw(g);

}

public class EnemyShotPiece extends MovingObject

{

double radius;

int step=0;

public EnemyShotPiece(int row, int col)

{

super(row, col);

//this.direction=direction;

//this.speed=speed;

//out.println(direction*(180.0/Math.PI));

radius=10;

//player=p;

}

public void draw(Graphics g)

{

//move();

//out.println(row.get()+" "+col.get()+" "+radius);

//g.drawArc(row.get(), col.get(), (int)Math.round(radius), (int)Math.round(radius), 0, 360);

Graphics2D gg=(Graphics2D)g;

//Color coll=gg.getColor();

//gg.setColor(new Color(random.nextInt(256),random.nextInt(256),random.nextInt(256)));

Paint p=gg.getPaint();

final GradientPaint gp=new GradientPaint(3, 3, Color.orange, width()/2.0f, height()/2.0f, Color.red, true);

gg.setPaint(gp);

Ellipse2D e=new Ellipse2D.Double(row.get()+radius/2, col.get(), radius, radius);

gg.fill(e);

//gg.setPaint(Color.green);

//gg.setStroke(new BasicStroke());

//gg.draw(e);

//gg.setPaint(gp);

gg.draw(e);

//e.setFrame()

//gg.setColor(Color.ORANGE);

//gg.drawOval((int)Math.round(row.get()+radius/2), (int)Math.round(col.get()), (int)Math.ceil(radius), (int)Math.ceil(radius));

//gg.fillOval((int)Math.round(row.get()+radius/2), (int)Math.round(col.get()), (int)Math.ceil(radius), (int)Math.ceil(radius));

//gg.setColor(coll);

gg.setPaint(p);

//g.fillOval(row.get(), col.get(), (int)Math.round(radius), (int)Math.round(radius));

}

public void setImage()

{

}

public int radius()

{

return (int)Math.round(radius);

}

public void move() //object doesnt move itself, only radius gets decremented

{

radius-=.5;

}

}

sorry if the code is knda funky, i had a lot of test statements in there.

Astrofa at 2007-7-15 6:23:16 > top of Java-index,Other Topics,Java Game Development...
# 5

Ok, calls to "new

" are extremely expensive. You should try to avoid them as much as possible in methods that are being called many many times in your program.

The draw(Graphics g)

method in your EnemyShotPiece class makes two new objects.

Lets try to remove both of those.

1. GradientPaint- You have to consider if you want to sacrifice the speed of your program, just for the sake of making it a little prettier. If I were you, I would not do a gradient OR i would make the gradient a static final variable. This way, you would only have to create it once.

2. Ellipse2D- Can't you just use g.drawOval(x,y,width,height)

?

Tell me how it goes.

Nethera at 2007-7-15 6:23:16 > top of Java-index,Other Topics,Java Game Development...
# 6
the eclipse was needed to do the gradient. thx though, i'll c what i can do to fix it
Astrofa at 2007-7-15 6:23:16 > top of Java-index,Other Topics,Java Game Development...
# 7

woah turns out my method was horribly off.....i used an eclipse because i saw on an online gradient tut that it used eclipse....but i didnt know u cud just fill oval and it wud do the same thing.....

here's my simplified code:

public void draw(Graphics g)

{

(int)Math.round(radius), 0, 360);

Graphics2D gg=(Graphics2D)g;

Paint p=gg.getPaint();

gg.setPaint(gp);

gg.drawOval((int)Math.round(row.get()+radius/2), (int)Math.round(col.get()), (int)Math.ceil(radius), (int)Math.ceil(radius));

gg.fillOval((int)Math.round(row.get()+radius/2), (int)Math.round(col.get()), (int)Math.ceil(radius), (int)Math.ceil(radius));

}

Astrofa at 2007-7-15 6:23:16 > top of Java-index,Other Topics,Java Game Development...
# 8
To completely fix it i had to not use the gradients, as it caused an uberlag. i instead used diffeerent colors to make it look interesting, but it still kinda lags if i have too many shots coming on the screen from multiple enemies.
Astrofa at 2007-7-15 6:23:16 > top of Java-index,Other Topics,Java Game Development...