We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hello,
I not to recently posted some quite problematic and disorganized code for my Asteroids game that I am working on for my Computer Programming class. I cleaned it up, and organized it a bit better. But I can't for the life of me figure out how to create and array of bullets for my ship, and in turn get them to fire when holding down say the, SHIFT or SPACE key. I am quite new to Processing and computer programming, any help would be enormously appreciated. Below I am posting the code for just my ship. I think my main confusion is that I used PVectors and I don't know how to incorporate the bullets properly with this technique. Thanks so much!
float shipX;//initial location
float shipY;//initial location
float shipAngle;
float direction;//ships's direction
PVector location;//ships's location
PVector velocity;//ship's speeds
PVector accel;//ship acceleration
void setup() {
size(500, 500);
location = new PVector(width/2, height/2, 0);
velocity = new PVector();
accel = new PVector();
shipX = width/2;
shipY = height/2;
shipAngle = 0.0;
}
void draw() {
checkKeys();
background(0);
stroke(0);
velocity.add(accel);
location.add(velocity);
drawShip();
accel.set(0, 0, 0);
if (velocity.mag() != 0) velocity.mult(0.99);
//wrap function
if (location.x<0) {
location.x = location.x+width;
}
if (location.x>width) {
location.x = 0;
}
if (location.y<0) {
location.y = location.y+height;
}
if (location.y>height) {
location.y = 0;
}
}
void drawShip() {
pushMatrix();
// Translate ship origin
translate(location.x, location.y);
// Rotate ship
rotate(direction);
// Display the ship
fill(105,95,95);
stroke(255,0,0);
triangle(-10, 20, 10, 20, 0, -20);
// if the ship is accelerating draw a thruster
if (accel.mag() != 0) {
float thrusterCol = random(0,255);//thuster feature that appears behind the ship when accelerating
fill(thrusterCol, thrusterCol/2, 0);
triangle(-5, 22, 5, 22, 0, 40);
}
popMatrix();
}
//moving the ship
void checkKeys() {
if (keyPressed && key == CODED) {
if (keyCode == LEFT) {
direction-=0.1;
}
else if (keyCode == RIGHT) {
direction+=0.1;
}
else if (keyCode == UP) {
float totalAccel = 0.2; // how much ship accelerates
accel.x = totalAccel * sin(direction); // total accel
accel.y = -totalAccel * cos(direction); // total accel
}
}
}
Answers
To do this you would probably need a class named Bullet. In that class you would need values like x, y, etc. You would also need a few methods such as bullet.move, bullet.display, etc. Then you create a global Array of bullets OR an ArrayList cast to bullets. then on each frame you update, display and do all that good stuff with the bullets. Consider my example:
void mousePressed()//add a new bullet if mouse is clicked { Bullet temp = new Bullet(mouseX,mouseY); bullets.add(temp); }
// How can I make the bullets come from an ship image instead of using mouseX and mouseY
replace mouseX,mouseY with the ship's pos
when the ship has a cannon attached, with a cannon pos, use this of course
//When I do this i get an error message "myShip.ship cannot be resolved or is not a field"
This is my ship class,I'm trying to get the bullets to fire from my ship Image by using KeyPressed function outside the class.
class Ship {
//Global Variables PVector position; PVector accel; PVector velocity; PVector rotation; float drag = .9; //Ship image PImage ship = loadImage("ship.png");
//How much the ship accelerates float acc = 0;
//rotation angle float a = 0;
//CONSTRUCTOR public Ship() {
}
//FUNCTIONS void update() {
}
void drawShip() { pushMatrix(); //method to use the new pos variable translate(position.x, position.x); rotate(a); stroke(255, 50, 0); fill(255, 50, 20);
}
void drawFinished() {
} }
well, when your ship's name is myShip outside the class (we don't know, because you don't tell), use it.
Since you use a PVector for position, you have to use that too:
Bullet temp = new Bullet(myShip.position.x, myShip.position.y);
Greetings, Chrisir ;-)
this is the idea
hit space or mouse to fire
Screen Shot 2014-03-13 at 13.52.42
Thank you, I got it working with my image. The only problem now is that I want the bullets to come out from the end of the ship not in the middle. When I rotate my ship the bullets stays in the middle of the Ship. How could I go about doing this?
can your ship look in every direction?
is this angle a?
Yes. a is the angle of the ship.
class Bullet//bullet class { float x; float y; float speed; float ang;
I have tried adding a float angle inside the class but when I do I get an error message "(float, float) is underfined"
can your ship look in every direction?
so like ---> and <-----
and | and / and \ etc. .................. ??
can bullets fly in every direction?
The ship can look in every direction but the bullets only fly out in one direction (middle of the ship). Is this what you mean?
this would be it in my sketch above...
you need to determine the position of the gun (where your bullet starts) by using sin and cos, since the bullet-position rotates on a circle with the ship or around the ship.
As you can see in the formula
the trick is that you know the radius of the circle (length of your ship / ship image / 70) and the position of the center of the ship (position.x,position.y) and the angle a
now you can calculate the bullets starting position positionbullet
sin and cos do all the hard work for you then
It is really this easy!
see https://forum.processing.org/tutorials/trig/
thread closed, cont'd elsewhere
http://forum.processing.org/two/discussion/3673/how-to-rotate-my-ship-bullets