So, I know there's been a lot of questions on this in the past, but yet to find a fix that'd help on mine. As for the topic, it gives it all. I can't make it detect a collission, and I have a theory as to why, but no fix I've attempted has seemed to work.
I'm a super rookie in JS.
checkPlayer2(Player2) {
//checker kollision
if (this.y - this.r < Player2.y + Player2.h/2 &&
this.y + this.r > Player2.y - Player2.h/2 &&
this.x - this.r < Player2.x + Player2.w/2) {
print("hit")
//checker vinkel
if (this.x > Player2.x) {
let forskel = this.y - (Player2.y - Player2.h/2);
let rad = radians(45);
let angle = map(forskel, 0, Player2.h, -rad, rad);
this.xspeed = 5 * cos(angle);
this.yspeed = 5 * sin(angle);
this.x = Player2.x + Player2.w/2 + this.r;
}
}
}
This is supposed to be where it detects the collission and angle. It's a part of a group project, and my groupmate has no clue either. This is how the paddle is designed:
class player{
constructor(U,D,nr) {
rectMode(CENTER);
this.x=width*(nr-1);
if(nr>1){
this.x=this.x-35
}else{
this.x=this.x+35
}
this.y=height/2
this.ctrlU=U
this.ctrlD=D
this.h = 100;
this.w = 20;
}
show(){
rect(this.x, this.y, this.w, this.h);
}
update(){
if (keyIsDown(this.ctrlU)){
if(this.y>=51){this.y=this.y-10;}
}
if (keyIsDown(this.ctrlD)) {
if(this.y<=height-51){this.y=this.y+10;}
}
}
}
As you see, in this "version" I'm trying to check for a direct collission with a second instance of the "player" - since there'll be 2. There's just no collission. Everything else bounces as it should.