We are about to switch to a new forum software. Until then we have removed the registration on this forum.
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);
location.add(velocity);
velocity.limit(speedMax);
velocity.add(acceleration);
if (mousePressed) {
accelerationMax = 0.01;
speedMax = 0;
}
else {
accelerationMax = 0.5;
speedMax = 5;
}
pushMatrix();
rectMode(CENTER);
translate (location.x, location.y);
rotate (PI/2 + velocity.heading());
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();
}
Answers
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
@junkpot -- okay! If you are familiar with classes, then this is one approach (there are many) you might try:
Thanks a lot!
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:
https://forum.Processing.org/two/discussion/18736/inheritance#Item_46
http://studio.SketchPad.cc/sp/pad/view/ro.9K-kjhuONcJDZ/latest
http://studio.SketchPad.cc/sp/pad/view/ro.9lTJ6qSlmCidk/latest