#### Howdy, Stranger!

We are about to switch to a new forum software. Until then we have removed the registration on this forum.

# How do I make my 'spaceship' shoot projectiles?

edited November 2016

I want to make a game about space battles and I need your help. Basically, all I need is a part of code, that makes my object shoot projectiles that fly for ~500 pixels when pressing spacebar. Here's the code.

```PVector location = new PVector (400,400);
PVector velocity = new PVector (0,0);
PVector acceleration = new PVector (0,0);
PVector mouse = new PVector (0,0);
float accelerationMax = 0.5;
int speedMax = 5;
int a = 40;
int b = 80;

void setup() {
size (800,800);
noCursor();
}
void draw() {
background (40);
fill(255,0,0);
ellipse(mouseX,mouseY,10,10);
line(mouseX,mouseY-15,mouseX,mouseY+15);
line(mouseX+15,mouseY,mouseX-15,mouseY);
mouse.set(mouseX, mouseY);
acceleration = PVector.sub(mouse,location);
acceleration.limit(accelerationMax);
velocity.limit(speedMax);
if (mousePressed) {
accelerationMax = 0.01;
speedMax = 0;
}
else {
accelerationMax = 0.5;
speedMax = 5;
}
pushMatrix();
rectMode(CENTER);
translate (location.x, location.y);
fill(0,0,0);
beginShape();
vertex(30,10);
vertex(45,35);
vertex(35,30);
vertex(38,40);
vertex(45,45);
vertex(15,45);
vertex(22,40);
vertex(25,30);
vertex(15,35);
endShape();
popMatrix();
}
```
Tagged:

• Not good at trigonometry. But I've got these 2 online sketches which maybe can be of some use: :-\"

• Many particles at a time, or just one?

Always the same speed, or does the speed depend on the ship speed?

To start out with, you might want to implement a single particle the way you did a ship. In general, this problem calls for classes -- eg class Ship, class Particle. Are you familiar with classes / have you read the classes tutorial?

• for now, I'll be fine with one particle, the particle speed is independent. Yes, I'm familiar with classes

• edited November 2016 Answer ✓

@junkpot -- okay! If you are familiar with classes, then this is one approach (there are many) you might try:

1. create a class Particle
2. the particle constructor takes a PVector location and a PVector velocity
3. the particle saves and keeps its starting location
4. the particle can .update() its current location separately
5. the particle can .display() its position
6. the particle has a .distance() which calculates the dist() between the starting and current location.
7. create a global list ArrayList<Particle> pList
8. when you press the key space (" "), then create a new particle and add it to pList. When creating, pass it the location of your ship, and the velocity of your ship -- with the velocity vector .setMag() to whatever speed you want.
9. In your draw loop, loop over pList if it is not empty, .update(), check if the .distance() is too big -- if so, remove from the ArrayList, otherwise .display() Edit: loop backwards if you plan on deleting, as TfGuy44 mentions.
• Thanks a lot!

• edited November 2016

I like to loop over my ArrayList of Particles backwards, and have my Particle's draw function return true if a particle should be removed from the ArrayList. Thus your loop for them can be:

``````for( int i = particles.size()-1; i >= 0 ; i-- ) { if ( particles.get(i).draw() ) particles.remove(i); }
``````