We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Objects do not collide correctly.
Ship ship;
ArrayList<Bullet> bullets = new ArrayList<Bullet>();
ArrayList<Asteroid> asteroids = new ArrayList<Asteroid>();
int num = 10;
void setup()
{
size(displayWidth, displayHeight);
ship = new Ship(300, 300, 0, 0);
for(int i = 0; i < num; i++)
{
asteroids.add(new Asteroid(random(50, width - 50), random(50, height - 50),
random(5), random(5)));
}
}
void draw()
{
background(0);
ship.run();
Collision2();
for(int i = 0; i < bullets.size(); i++)
{
Bullet bullet = bullets.get(i);
bullet.run();
if(!bullet.active)
{
bullets.remove(i);
}
}
for(int a = 0; a < asteroids.size(); a++)
{
Asteroid asteroid = asteroids.get(a);
asteroid.run();
}
}
void mousePressed()
{
float angle = atan2(mouseY - ship.position.y, mouseX - ship.position.x);
bullets.add(new Bullet(angle, ship.position.x, ship.position.y));
}
class Asteroid
{
PVector position;
PVector velocity;
float radius;
Asteroid(float posX, float posY, float velX, float velY)
{
position = new PVector(posX, posY);
velocity = new PVector(velX, velY);
radius = 40;
}
void run()
{
draw();
update();
}
void draw()
{
pushMatrix();
translate(position.x, position.y);
ellipse(0, 0, radius, radius);
popMatrix();
}
void update()
{
position.add(velocity);
if(position.x - radius/2 < 0 || position.x + radius/2 > width)
{
velocity.x *= -1;
}
if(position.y - radius/2 < 0 || position.y + radius/2 > height)
{
velocity.y *= -1;
}
}
}
class Ship
{
float angle;
PVector position;
PVector velocity;
Ship(float posX, float posY, float velX, float velY)
{
position = new PVector(posX, posY);
velocity = new PVector(velX, velY);
}
void run()
{
draw();
}
void draw()
{
pushMatrix();
float angle = atan2(mouseY - position.y, mouseX - position.x);
translate(position.x, position.y);
rotate(angle);
triangle(20, 0, -20, -10, -20, 10);
popMatrix();
}
}
class Bullet
{
PVector position;
PVector init_position;
PVector velocity;
float angle;
float distance;
boolean active;
float radius;
Bullet(float _angle, float init_posX, float init_posY)
{
angle = _angle;
position = new PVector();
init_position = new PVector(init_posX, init_posY);
velocity = new PVector(10, 0);
distance = 0.0;
active = true;
radius = 5;
}
void run()
{
draw();
update();
}
void draw()
{
pushMatrix();
translate(init_position.x, init_position.y);
rotate(angle);
ellipse(position.x, position.y, radius, radius);
popMatrix();
}
void update()
{
position.add(velocity);
distance += velocity.mag();
if(distance > 700)
{
active = false;
}
}
}
void Collision2()
{
for(int i = asteroids.size() - 1; i >= 0; i--)
{
Asteroid asteroid = asteroids.get(i);
for(int j = bullets.size() - 1; j >= 0; j--)
{
Bullet bullet = bullets.get(j);
if(collision(asteroid.position.x, asteroid.position.y, asteroid.radius,
bullet.position.x, bullet.position.y, bullet.radius))
{
asteroids.remove(i);
bullets.remove(j);
}
}
}
}
boolean collision(float x1, float y1, float r1, float x2, float y2, float r2)
{
float d = dist(x1, y1, x2, y2);
if(d <= (r1 + r2)/2)
{
return true;
}
return false;
}
Answers
Hi Gismas--
First, this code doesn't work. Ton of errors, including "The function Collision2()" does not exist.
Could you please:
Line 152 has an obvious typo
I suspect the problem is that you are deleting both bullet and asteroid within a nested loop.
Specifically, when you delete an asteroid you continue to check the rest of the bullets against the modified asteroid list. I think you need a break in there somewhere.
jeremydouglass, I corrected code . When you shoot down asteroids that are at the top of the screen disappears . If you shoot the asteroid , it does not disappear .
koogs, well , I will think how to fix
https://forum.Processing.org/two/discussions/tagged/backwards
It's already backwards!
Not all of them! Anyways I've got this very old online sketch about bullets vs asteroids collision: :bz
http://studio.SketchPad.cc/sp/pad/view/Ehj6razBLn/latest
And its corresponding forum thread too: ;;)
https://forum.Processing.org/two/discussion/3890/collision-problem
It seems the problem is drawing the wrong bullets (translate (), rotate ()). And for bullets should be a direction vector (direction - > Speed - > position) . In another sketch , where bullet witch random velocity , collisions are correct .