We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hi There, I can either get all objects to collide properly but not control my "hero" or I can control my "hero" but not collide (case with code below). How can I control an object from an ArrayList AND have it collide with everything else from the list? Thanks! EDIT: just fixed a mistake so the hero displays now.
Hero heros;
ArrayList<Actor> actors=new ArrayList<Actor>();
void setup() {
size(1600, 800);
addActors();
}
void draw() {
background(120, 120, 255);
for (int i=0; i<actors.size(); i++) {
Actor currActor=actors.get(i);
for (int j = i + 1; j < actors.size(); j++) {
Actor otherActor = actors.get(j);
if (currActor.hitActor(otherActor)) {
currActor.resolveHitActor(otherActor);
}
}
currActor.update();
}
heros.update();
heros.handleCollision();
heros.force();
}
void keyPressed () {
heros.keyPressed();
}
void keyReleased () {
heros.keyReleased();
}
void addActors () {
for (int i=0; i<10; i++) {
actors.add(new Baddy(new PVector(random(50, width-50), random(50, width-50)), new PVector(random(2, 4), random(2, 4)), random(20, 50)));
actors.add(new BossBaddy(new PVector(random(50, width-50), random(50, width-50)), new PVector(random(2, 4), random(2, 4)), random(20, 50)));
}
heros=new Hero(new PVector(random(50, width-50), random(50, width-50)), new PVector(random(2, 4), random(2, 4)), 100);
}
//ACTOR CLASS
class Actor {
PVector pos;
PVector vel;
PVector grav;
float diameter;
Actor(PVector pos, PVector vel, float diameter) {
this.pos = pos;
this.vel = vel;
// size = s;
this.diameter = diameter;
grav = new PVector(0, 0.2);
}
// Check for collisiond between actors
boolean hitActor (Actor other) {
if (dist(pos.x, pos.y, other.pos.x, other.pos.y) < diameter/2 + other.diameter/2) {
return true;
}
return false;
}
//resolve collision
void resolveHitActor(Actor other) {
float angle = atan2(pos.y - other.pos.y, pos.x - other.pos.x);
float avgSpeed = (vel.mag() + other.vel.mag())*0.49;
vel.x = avgSpeed * cos(angle);
vel.y = avgSpeed * sin(angle);
other.vel.x = avgSpeed * cos(angle - PI);
other.vel.y = avgSpeed * sin(angle - PI);
}
void update() {
move();
wallCollision();
drawMe();
}
void move() {
pos.add(vel);
vel.add(grav);
}
void wallCollision() {
if ((pos.x > width - diameter/2) || (pos.x < 0 + diameter/2)) {
vel.x = vel.x * -1;
}
if (pos.y > height - 100) {
vel.y = vel.y *-0.95;
pos.y = height- 100;
}
}
void drawMe() {
pushMatrix ();
fill(0);
translate(pos.x, pos.y);
ellipse(0, 0, diameter, diameter);
popMatrix ();
}
}
//BADDY CLASS
class Baddy extends Actor {
Baddy(PVector pos, PVector vel, float diameter) {
super (pos, vel, diameter);
this.diameter = diameter;
}
void drawMe() {
pushMatrix();
strokeWeight(5);
stroke(255, 255, 0);
fill(0, 255, 0);
translate(pos.x, pos.y);
ellipse(0, 0, diameter, diameter);
popMatrix();
}
}
//BOSSBADDY CLASS
class BossBaddy extends Baddy {
BossBaddy(PVector pos, PVector vel, float diameter) {
super (pos, vel, diameter);
this.diameter = diameter;
}
void drawMe (){
pushMatrix ();
translate (pos.x, pos.y);
noStroke();
fill(255, 0, 0);
ellipse(0, 0, diameter, diameter);
popMatrix ();
}
}
//HERO CLASS
class Hero extends Actor {
float damp = 0.9;
float angle;
boolean up = false;
boolean down = false;
boolean left = false;
boolean right = false;
Hero(PVector pos, PVector vel, float diameter) {
super (pos, vel, diameter);
this.diameter = diameter;
}
void force(){
PVector upForce = new PVector (0, -2);
PVector downForce = new PVector (0, 2);
PVector leftForce = new PVector (-2, 0);
PVector rightForce = new PVector (2, 0);
if (up) accelerate(upForce);
if (down) accelerate(downForce);
if (left) accelerate(leftForce);
if (right) accelerate(rightForce);
}
void keyPressed () {
if (key == 'w') up = true;
if (key == 's') down = true;
if (key == 'a') left = true;
if (key == 'd') right = true;
}
void keyReleased () {
if (key == 'w') up = false;
if (key == 's') down = false;
if (key == 'a') left = false;
if (key == 'd') right = false;
}
void update() {
move();
drawMe();
}
void move() {
pos.add(vel);
vel.mult(damp);
}
void accelerate(PVector force) {
vel.add(force);
}
void handleCollision() {
if ((pos.x > width - diameter) || (pos.x < 0 + diameter)) {
vel.x = vel.x * -1.8;
}
if (pos.y < 0 + diameter/2 + 20) {
vel.y = vel.y * -1;
pos.y = 0 + diameter/2 + 20;
}
if (pos.y > height - 100) {
vel.y = vel.y *-0.05;
pos.y = height- 100;
}
}
void drawMe() {
translate (pos.x, pos.y);
fill(255 );
ellipse(0,0, diameter, diameter);
}
}