Collisions issue
in
Programming Questions
•
5 months ago
I am fairly new to programming so I don't know the finer points unfortunately. I am working on creating a game with targets and arrows. Very simply put, it decides the speed with how far you pull the mouse after your mousePress. With collisions on the targets, this unfortunately leads to some issues. The arrows are just simple lines but I am using the front point of the arrow to determine collisions and if the speed is greater than the window for hitting the target, it simply jumps over the target and does doesn't activate the breaking of the target. Is there any way to say if any part of the line touches it, to break the target?
I have included the code below in case you feel it would help.
Arrow[]arrows = new Arrow[1];
Target[]targets = new Target[1];
Timer timer;
float mX1=-1;
float mX2=-1;
float mY1=-1;
float mY2=-1;
int t=2000;
int timercount=0;
int rounds=5;
int score=0;
void setup(){
size(800,800);
smooth();
arrows[0] = new Arrow(mX1,mY1,mX2,mY2);
targets[0] = new Target();
timer = new Timer(t);
timer.start(); //begins timer
}
void draw(){
if(timercount<rounds){ //sets a max number of targets that will spawn before game over
background(255);
if(timer.finished()){
Target t = new Target();
targets = (Target[]) append(targets,t);
timer.start();
timercount+=1;
}
for(int i = 0; i<targets.length; i++){ //displays targets
targets[i].display();
for(int i2 = 0; i2<arrows.length; i2++){
if(arrows[i2].collision(targets[i].x,targets[i].y)==true){
targets[i].bullseye();
score+=1;
}
}
}
for(int i = 0; i<arrows.length; i++){ //displays arrows
arrows[i].shoot();
arrows[i].display();
}
}
else{
background(255,0,0); //ends game and prints score
print("Your score was - ");
print(score);
noLoop();
}
}
void mouseDragged(){
line(mX1,mY1,mouseX,mouseY); //shows what your arrow will look like
}
void mousePressed(){ //saves first coords
mX1=mouseX;
mY1=mouseY;
}
void mouseReleased(){ //saves second coords
mX2=mouseX;
mY2=mouseY;
Arrow a = new Arrow(mX1,mY1,mX2,mY2); //creates a new arrow
arrows = (Arrow[]) append(arrows,a); //expands array and adds new arrow to it.
}
class Target{
float x;
float y;
int radi1;
Target(){
x=random(200,750);
y=random(50,600);
radi1=45;
}
void display(){
strokeWeight(1);
fill(255);
ellipse(x,y,radi1*1.5,radi1*1.5);
ellipse(x,y,radi1,radi1); //big circle
fill(200,0,0);
ellipse(x,y,radi1/3,radi1/3); //bullseye
}
void bullseye(){
x=x+800;
}
}
class Timer{
int savedTime;
int totalTime;
Timer(int totalTime_) {
totalTime=totalTime_;
}
void start(){
savedTime = millis();
}
boolean finished(){
int passedTime = millis()-savedTime;
if(passedTime > totalTime) {
return true; }
else{ return false; }
}
}
class Arrow{
float x1;
float y1;
float x2;
float y2;
float xSpeed;
float ySpeed;
float gravity=.1;
Arrow(float x1_, float y1_, float x2_, float y2_){
x1=x1_;
y1=y1_;
x2=x2_;
y2=y2_;
xSpeed=(x1-x2)/4; //speed based on how far mouse is dragged
ySpeed=(y1-y2)/4;
}
void display(){
fill(0);
strokeWeight(3);
line(x1,y1,x2,y2);
}
void shoot(){ //sticks arrows to the wall
if((x1<=0) || (x1>=width) || (y1<=0) || (y1>=height) || (x2<=0) || (x2>=width) || (y2<=0) || (y2>=height)){
xSpeed=0;
ySpeed=0;
}
x1+=xSpeed; //assigns speed to the arrow
x2+=xSpeed;
y1+=ySpeed;
y2+=ySpeed;
ySpeed=ySpeed+gravity;
}
boolean collision(float xCoord, float yCoord){
if((yCoord+25>=y1)&&(yCoord-25<=y1)&&(xCoord+25>=x1)&&(xCoord-25<=x1)){
return true;
}
else{return false;}
}
}
1