GML Need Help With Collisions Between Two Moving Objects

D

Dylanator0123

Guest
I've been trying to make a pong style game for a while now but I can't seem to get any of the collisions right. Whenever I check for collisions, the ball either gets caught in the players or the wall, usually by one of the players pushing the ball into the wall (the players can move in 8 directions). I've been trying everything I know and can't seem to get it to work no matter what. If anyone knows how I could avoid this issue, or could point me in the right direction of a solution to my problem, I would greatly appreciate it.
 
D

Dylanator0123

Guest
I have a bunch of code that I was trying that I couldn't get to work that I could post, but most of it is commented out since I was trying multiple different methods.

Here's all of it

Code:
//place_meeting(x+hspeed,y,obj_player1){
if !place_free(x+hspeed,y){
    hspeed = -hspeed;
}else if (!place_empty(x+hspeed-obj_player1.hsp,y)) && (distance_to_object(obj_player1)<= abs(hspeed-obj_player1.hsp)) {
    if sign(obj_player1.hsp) ==  sign(hspeed){
        if obj_player1.hsp > hspeed{
            hspeed = obj_player1.hsp + sign(obj_player1.hsp);
        }
        if hspeed == obj_player1.hsp{
            hspeed = -hspeed;
        }
    }
    if sign(obj_player1.hsp) !=  sign(hspeed){
        if abs(obj_player1.hsp) > abs(hspeed){
            hspeed = obj_player1.hsp + sign(obj_player1.hsp);
        }
        else
        hspeed = -hspeed;
    }
}
else if place_meeting(x,y,obj_player1) {
    if x < obj_player1{
        if sign(obj_player1.hsp) = -1{
            hspeed = ballspeed*2;
        }
        else if sign(obj_player1.hsp) = 1{
            hspeed = -ballspeed*2;
        }
    }
    if x > obj_player1{
        if abs(obj_player1.hsp) = -1{
            hspeed = -ballspeed*2;
        }
        else if abs(obj_player1.hsp) = 1{
            hspeed = ballspeed*2;
        }
    }
}

//if !place_empty(x+hspeed,y){ // && (distance_to_object(obj_wall)<= abs(hspeed)){
//    hspeed = -hspeed;
//};

//if place_meeting(x,y,obj_wall){
//    if x<room_width/2{
//        x = sprite_width/2;
//    }
//        if x>room_width/2{
//        x = room_width/sprite_width/2;
//    }
//};

//if distance_to_object(obj_wall)<= abs(hspeed * 2) && distance_to_object(obj_player1)<= abs(hspeed * 2){
//    if (obj_player1.x > x && obj_wall.x < x){
//        hspeed = -ballspeed * 2;
//    }
//    if (obj_player1.x < x && obj_wall.x > x){
//        hspeed = ballspeed * 2;
//    }
//};

if !place_empty(x,y+vspeed) {
    vspeed = -vspeed;
};

//Horizontal collisions
if (place_meeting(x + hspeed, y, obj_wall)) {
    while (!place_meeting(x + sign(hspeed), y, obj_wall)) {
        x += sign(hspeed);
    }
    hspeed = -hspeed;// / 2;
    if (tick != 0){
        //ballspeed = -ballspeed * 2;
        tick = tickt;
    }
}

//Horizontal movement
//x = hspeed;

//Vertical collisions
if (place_meeting(x, y + vspeed, obj_wall)) {
    while (!place_meeting(x, y + sign(vspeed), obj_wall)) {
        y += sign(vspeed);
    }
    vspeed = -vspeed;// / 1.5;
}

//Vertical movement
//if (grounded) && (abs(0 - vspeed) < 1) vspeed = 0;
//y = vspeed;

//Rotating
//dir += -hspeed * 2;

//Horizontal collisions w/ the player1
if (place_meeting(x + hspeed, y, obj_player1)) {
    while (!place_meeting(x + sign(hspeed), y, obj_player1)) {
        x += sign(hspeed);
    }
    hspeed = -hspeed;// / 2;
    vspeed = (y-obj_player1.y)/ballspeed;
    tick = tickt;
}

//Horizontal collisions w/ the player2
if (place_meeting(x + hspeed, y, obj_player2)) {
    while (!place_meeting(x + sign(hspeed), y, obj_player2)) {
        x += sign(hspeed);
    }
    hspeed = -hspeed;// / 2;
    vspeed = (y-obj_player2.y)/ballspeed;
    tick = tickt;
}

//Vertical collisions w/ the player
if (place_meeting(x, y + vspeed, obj_player)) {
   while (!place_meeting(x, y + sign(vspeed), obj_player)) {
        y += sign(vspeed);
    }
    //vspeed = -vspeed;// / 1.5;
    vspeed = -vspeed;
}

//move_bounce_solid(true);

//More unused code

//if !place_meeting(x+hspeed,y,obj_player1) || !place_meeting(x+hspeed,y,obj_player2){
//    if !place_empty(x+hspeed,y) {
//        hspeed = -hspeed;
//    };
//};

//if place_meeting(x+hspeed,y,obj_player1){// and !place_empty(x+hspeed + abs(hspeed),y){
//   if abs(obj_player1.hsp) > abs(hspeed){
//        hspeed = obj_player1.hsp + sign(obj_player1.hsp);
//    }
//    else if place_meeting(x+hspeed,y,obj_player2){
//        hspeed = 0;
//        vspeed = 5;
//    }
//    else{hspeed = -hspeed;};
//    vspeed = (y-obj_player1.y)/ballspeed;
//};

//if place_meeting(x+hspeed,y,obj_player2){// and !place_empty(x+hspeed + abs(hspeed),y){
//    if abs(obj_player2.hsp) > abs(hspeed){
//        hspeed = obj_player2.hsp + sign(obj_player2.hsp);
//    }
//    else if place_meeting(x+hspeed,y,obj_player1){
//        speed = 0;
//        vspeed = 5;
//    }
//    else{hspeed = -hspeed;};
//    vspeed = (y-obj_player2.y)/ballspeed;
//};

//if !place_meeting(x+hspeed,y,obj_player1) && !place_meeting(x+hspeed,y,obj_player2){
//    if !place_empty(x+hspeed,y) {
//        hspeed = -hspeed;
//    };
//};

//if place_meeting(x+hspeed,y,obj_player1) && place_meeting(x+hspeed,y,obj_player2){
//    hspeed = 0;
//    vspeed = ballspeed*1.25;
//};

//if place_meeting(x,y+vspeed,obj_player1) && !place_empty(x,y+vspeed){
//    if abs(obj_player1.vsp) > abs(vspeed){
//        hspeed = obj_player1.vsp + 1;
//    }
//    else {vspeed = -vspeed;};
//};

//if place_meeting(x,y+vspeed,obj_player2) && !place_empty(x,y+vspeed){
//    if abs(obj_player1.vsp) > abs(vspeed){
//        hspeed = obj_player1.vsp + 1;
//    }
//    else {vspeed = -vspeed;};
//};
 
S

Sursion

Guest
Wow, that is a lot of commenting. Just for future reference, you can block-comment by putting

/*
and
*/
 
Top