help with collision detection using angles
in
Programming Questions
•
6 months ago
I've spent the last few hours looking for help on collision while using angles and I've come up dry. Any help or suggestions for tutorials would be greatly appreciated.
//----------------------------------------------------------------------------------------------------------------------------------------------------------------------
Lasers lasers;
SpaceShip spaceShip;
SpaceShip spaceShip2;
void setup() {
size (displayWidth/2, displayHeight/2);
spaceShip = new SpaceShip();
spaceShip2 =new SpaceShip();
rectMode(CENTER);
}
void draw() {
background(30);
drawSpaceShip();
collision();
}
void drawSpaceShip() {
spaceShip.drawShip(40, 150, 40, 30, 255, 30);
spaceShip2.drawShip(150, 40, 40, 255, 30, 30);
}
void keyPressed() {
if (keyCode==81) {
spaceShip2.fireLeft=true;
}
if (keyCode==69) {
spaceShip2.fireRight=true;
}
if (keyCode==87) {
spaceShip2.switchSpeed = -1;
}
else if (keyCode==83) {
spaceShip2.switchSpeed = 1;
}
else if (keyCode==68) {
spaceShip2.switchAngle = 1;
}
else if (keyCode== 65) {
spaceShip2.switchAngle = -1;
}
if (keyCode==85) {
spaceShip.fireLeft=true;
}
if (keyCode==79) {
spaceShip.fireRight=true;
}
if (keyCode==73) {
spaceShip.switchSpeed = -1;
}
else if (keyCode==75) {
spaceShip.switchSpeed = 1;
}
else if (keyCode==76) {
spaceShip.switchAngle = 1;
}
else if (keyCode== 74) {
spaceShip.switchAngle = -1;
}
}
void keyReleased() {
if (keyCode==81) {
spaceShip2.fireLeft = false;
}
if (keyCode==69) {
spaceShip2.fireRight = false;
}
if (keyCode==87) {
spaceShip2.switchSpeed = 0;
}
else if (keyCode==83) {
spaceShip2.switchSpeed = 0;
}
else if (keyCode==68) {
spaceShip2.switchAngle = 0;
}
else if (keyCode== 65) {
spaceShip2.switchAngle = 0;
}
if (keyCode==85) {
spaceShip.fireLeft=false;
}
if (keyCode==79) {
spaceShip.fireRight=false;
}
if (keyCode==73) {
spaceShip.switchSpeed = 0;
}
else if (keyCode==75) {
spaceShip.switchSpeed = 0;
}
else if (keyCode==76) {
spaceShip.switchAngle = 0;
}
else if (keyCode== 74) {
spaceShip.switchAngle = 0;
}
}
void collision() {
float hypotenuse = sqrt((spaceShip.posX - spaceShip2.posX)*(spaceShip.posX - spaceShip2.posX) + (spaceShip.posY - spaceShip2.posY)*(spaceShip.posY - spaceShip2.posY));
}
class SpaceShip {
float range =1.3, fire=1;
float posX, posY, angle = 0, switchSpeed, switchAngle = 0;
float hypoSquared = (posX*posX)+(posY*posY), hypo = sqrt(hypoSquared);
PVector location;
boolean fireLeft = false, fireRight = false;
void drawShip(int c1, int c2, int c3, int s1, int s2, int s3) {
location = new PVector (255, 255);
pushMatrix();
translate(posX, posY);
posY += hypo*.05 *sin(angle);
posX += hypo*.05 *cos(angle);
posY = constrain(posY, 10, height - 10);
posX = constrain(posX, 10, width - 10);
angle += radians(switchAngle);
hypo += switchSpeed;
fire = constrain (fire, 0, 1000);
rotate(angle);
strokeWeight(3);
stroke(s1, s2, s3);
fill(c1, c2, c3);
rect(0, 0, 60, 30, 30, 0, 0, 30);
if (fireLeft == true) {
stroke(255, 0, 0);
fill(255, 100, 100);
fire *= range;
rectMode(CORNER);
rect(-15, 25, 30, fire);
fireRight = false;
}
else if (fireRight == true) {
stroke(255, 0, 0);
fill(255, 100, 100);
fire *= range ;
rectMode(CORNER);
fireLeft = false;
rect(-15, -25, 30, fire * -1);
}
else if (fireRight == false) {
fire = 10;
}
else if (fireLeft == false) {
fire = 10;
}
rectMode(CENTER);
popMatrix();
}
SpaceShip() {
rect(posX, posY, 30, 60);
posX = 255;
posY = 255;
}
}
1