I have been working on hitting a target with particle "bullets".
Since I have a circle/circle collision that works I thought I would try it. My problem is I am unsure how to get a usable x value for the collision mathematics. My print statement does not show a change in value. It remains 400. My method doesn't use vector math. It uses speed. I was hoping to use a vector just for the experience. Here is my code. My goal is to get the asteroid to change state(color).
#Hitting a target with bullets
1 messages · Page 1 of 1 (latest)
import java.util.Iterator;
ArrayList<Bullet>bullets;
Laser laser;
float x, y;
float x2,y2;
float cx, cy, cSize,speed;
float sx, sy;
void setup() {
size(800, 800);
background(0);
x = 400;
y = 400;
sx = width/2;
sy = height/2;
cx = 650;
cy = 400;
cSize = 100;
speed = 3;
laser = new Laser(x, y, cx, cy, cSize, sx, sy);
bullet = new Bullet(new PVector(x,y));
bullets = new ArrayList<Bullet>();
}
void draw() {
background(0);
bullet.input();
laser.drawAsteroid();
laser.moveAsteroid();
laser.bounceAsteroid();
laser.ship();
collision();
}
class Laser {
Laser(float x_, float y_, float cx_, float cy_, float cSize_, float sx_, float sy_) {
x = x_;
y = y_;
//x2 = x2_;
//y2 = y2_;
cx = cx_;
cy = cy_;
cSize = cSize_;
sx = sx_;
sy = sy_;
}
void ship() {
strokeWeight(0);
stroke(0, 0, 255);
fill(255);
triangle(sx-100, sy+10, sx, sy, sx-100, sy-10);
}
void drawAsteroid() {
noStroke();
fill(125);
ellipse(cx, cy, cSize, cSize);
}
void moveAsteroid(){
cy+=speed;
//println("cy = " + cy);
}
void bounceAsteroid(){
if(cy-(cSize-(cSize/2))<=0||cy+(cSize-(cSize/2))>=height){
speed*=-1;
}
}
}```
Bullet bullet;
class Bullet {
PVector location;
PVector velocity;
PVector acceleration;
float size;
Bullet(PVector bullet) {
location = bullet.copy();
acceleration = new PVector(2, 0);
velocity = new PVector(2, 0);
size = 8;
}
void run() {
update();
display();
}
void update() {
velocity.add(acceleration);
location.add(velocity);
}
void display() {
stroke(5);
fill(255, 0, 0);
ellipse(location.x, location.y, size, size);
}
boolean isDead() {
if (location.x>=width) {
return true;
} else {
return false;
}
}
void input() {
if (keyPressed) {
if (key == ' ') {
bullet.runBulletSystem();
println("location = " + location);
}
}
}
void runBulletSystem() {
bullets.add(new Bullet(new PVector(location.x, location.y)));
Iterator<Bullet> it = bullets.iterator();
while (it.hasNext()) {
Bullet bullet = it.next();
bullet.run();
if (bullet.isDead()) {
it.remove();
println("particle is dead");
}
}
}
}
boolean collision() {
//float lineLen = dist(sx,sy,width,sy);
//println("lineLen = " + lineLen);
return true;
}
Please ignore the laser code. I should have removed/changed it but I haven't gotten that far. Bugging me I cant figure out why my value wont change.
I was thinking each particle will have an x,y and size value I need for the pythagorean theorem to calculate when the radii of the particle is inside the asteroid.
The first particle to strike it will change its color hopefully "explode" someday.
Update: Been reading and dist() method should work. It accepts PVector as arguments.
Update: dist() method was the answer. Once I get this working with a single asteroid, I need to work on the distance to multiple asteroid objects.
I am plugging along.
I need to get the bullets x and y distance value into the equation. 🤔 . I have their starting position. I need to get their "moving" value. Their moving value is updated with vector math. Think, Micky, think.
I need at least two PVectors. one for bullet location and one for asteroid location. Someplace to store the locations and compare them. This is getting interesting now.
Which object test the collision?
@forest sun I have bullet testing for collision in the development.
I see bullet as a child of Particle in the game itself. Following the inheritance example the classes are too similar not to have a parent/child relationship.
I also have an ArrayList for bullets when I only need one "particle" ArrayList.
systems in systems example in Nature of code.
Why not let the asteroids test if a bullet hits them? I think the asteroids must make the most changes on a hit for itself.