Using Rectangle.Intersects for Collision detection causes objects to “stick” to surfaces (Java)










1















I am trying to develop a top - down shooter game. Currently I am doing everything from scratch because I want to learn Java better. My problem right now is with collision detection. I am using Rectangle.Intersects(as seen in code) to determine whether the player is intersecting with walls. It almost perfectly, except that when the player is colliding on just one axis (ie running into a vertical wall), they are locked in both directions. I want to implement sliding for more smooth gameplay.



I have a slight idea on how to do this, which would involve checking the x and y planes individually. However, I don't know if this is possible using my current method.



I am a little new to Java so I don't know much about different kinds of libraries I could use instead of Rectangles but they have been working great and I would like to stick to them.



If there is a solution using Rectangles, that would be great. If there isn't, I'm open to new ideas.



Thanks.



Here is the code I am using



//x and y are position of player and vx and vy are the velocities in respective directions
public void tick()
x+=vx;
y+=vy;

collision();
//movement
//...

private void collision() {
for(int i = 0; i< handler.object.size(); i++) {
GameObject tempObject = handler.object.get(i);

if(tempObject.getId() == ID.Block)
if(getBounds().intersects(tempObject.getBounds()))
x +=-vx;
y +=-vy;


//...
public Rectangle getBounds()
return new Rectangle((int)x,(int)y,32,32);










share|improve this question






















  • You reset the position of the object, based on the amount it's move, but don't seem to change the delta values, so the collision will continue to occur, as an observation

    – MadProgrammer
    Nov 14 '18 at 3:24











  • thanks, this helped me solve my problem

    – play minecraft
    Nov 15 '18 at 1:55















1















I am trying to develop a top - down shooter game. Currently I am doing everything from scratch because I want to learn Java better. My problem right now is with collision detection. I am using Rectangle.Intersects(as seen in code) to determine whether the player is intersecting with walls. It almost perfectly, except that when the player is colliding on just one axis (ie running into a vertical wall), they are locked in both directions. I want to implement sliding for more smooth gameplay.



I have a slight idea on how to do this, which would involve checking the x and y planes individually. However, I don't know if this is possible using my current method.



I am a little new to Java so I don't know much about different kinds of libraries I could use instead of Rectangles but they have been working great and I would like to stick to them.



If there is a solution using Rectangles, that would be great. If there isn't, I'm open to new ideas.



Thanks.



Here is the code I am using



//x and y are position of player and vx and vy are the velocities in respective directions
public void tick()
x+=vx;
y+=vy;

collision();
//movement
//...

private void collision() {
for(int i = 0; i< handler.object.size(); i++) {
GameObject tempObject = handler.object.get(i);

if(tempObject.getId() == ID.Block)
if(getBounds().intersects(tempObject.getBounds()))
x +=-vx;
y +=-vy;


//...
public Rectangle getBounds()
return new Rectangle((int)x,(int)y,32,32);










share|improve this question






















  • You reset the position of the object, based on the amount it's move, but don't seem to change the delta values, so the collision will continue to occur, as an observation

    – MadProgrammer
    Nov 14 '18 at 3:24











  • thanks, this helped me solve my problem

    – play minecraft
    Nov 15 '18 at 1:55













1












1








1


1






I am trying to develop a top - down shooter game. Currently I am doing everything from scratch because I want to learn Java better. My problem right now is with collision detection. I am using Rectangle.Intersects(as seen in code) to determine whether the player is intersecting with walls. It almost perfectly, except that when the player is colliding on just one axis (ie running into a vertical wall), they are locked in both directions. I want to implement sliding for more smooth gameplay.



I have a slight idea on how to do this, which would involve checking the x and y planes individually. However, I don't know if this is possible using my current method.



I am a little new to Java so I don't know much about different kinds of libraries I could use instead of Rectangles but they have been working great and I would like to stick to them.



If there is a solution using Rectangles, that would be great. If there isn't, I'm open to new ideas.



Thanks.



Here is the code I am using



//x and y are position of player and vx and vy are the velocities in respective directions
public void tick()
x+=vx;
y+=vy;

collision();
//movement
//...

private void collision() {
for(int i = 0; i< handler.object.size(); i++) {
GameObject tempObject = handler.object.get(i);

if(tempObject.getId() == ID.Block)
if(getBounds().intersects(tempObject.getBounds()))
x +=-vx;
y +=-vy;


//...
public Rectangle getBounds()
return new Rectangle((int)x,(int)y,32,32);










share|improve this question














I am trying to develop a top - down shooter game. Currently I am doing everything from scratch because I want to learn Java better. My problem right now is with collision detection. I am using Rectangle.Intersects(as seen in code) to determine whether the player is intersecting with walls. It almost perfectly, except that when the player is colliding on just one axis (ie running into a vertical wall), they are locked in both directions. I want to implement sliding for more smooth gameplay.



I have a slight idea on how to do this, which would involve checking the x and y planes individually. However, I don't know if this is possible using my current method.



I am a little new to Java so I don't know much about different kinds of libraries I could use instead of Rectangles but they have been working great and I would like to stick to them.



If there is a solution using Rectangles, that would be great. If there isn't, I'm open to new ideas.



Thanks.



Here is the code I am using



//x and y are position of player and vx and vy are the velocities in respective directions
public void tick()
x+=vx;
y+=vy;

collision();
//movement
//...

private void collision() {
for(int i = 0; i< handler.object.size(); i++) {
GameObject tempObject = handler.object.get(i);

if(tempObject.getId() == ID.Block)
if(getBounds().intersects(tempObject.getBounds()))
x +=-vx;
y +=-vy;


//...
public Rectangle getBounds()
return new Rectangle((int)x,(int)y,32,32);







java






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 14 '18 at 1:21









play minecraftplay minecraft

61




61












  • You reset the position of the object, based on the amount it's move, but don't seem to change the delta values, so the collision will continue to occur, as an observation

    – MadProgrammer
    Nov 14 '18 at 3:24











  • thanks, this helped me solve my problem

    – play minecraft
    Nov 15 '18 at 1:55

















  • You reset the position of the object, based on the amount it's move, but don't seem to change the delta values, so the collision will continue to occur, as an observation

    – MadProgrammer
    Nov 14 '18 at 3:24











  • thanks, this helped me solve my problem

    – play minecraft
    Nov 15 '18 at 1:55
















You reset the position of the object, based on the amount it's move, but don't seem to change the delta values, so the collision will continue to occur, as an observation

– MadProgrammer
Nov 14 '18 at 3:24





You reset the position of the object, based on the amount it's move, but don't seem to change the delta values, so the collision will continue to occur, as an observation

– MadProgrammer
Nov 14 '18 at 3:24













thanks, this helped me solve my problem

– play minecraft
Nov 15 '18 at 1:55





thanks, this helped me solve my problem

– play minecraft
Nov 15 '18 at 1:55












1 Answer
1






active

oldest

votes


















0














In case anyone reading this is curious, I made a new function that handles movement and converted collision into a boolean that returns true if player is colliding with blocks (walls) and false if colliding with anything else.



I also change the check for the block collision, as seen in the code



private boolean collision(double vx, double vy) 
for(int i = 0; i< handler.object.size(); i++)
GameObject tempObject = handler.object.get(i);
if(tempObject.getId() == ID.Block)
if(new Rectangle((int)x+(int)vx,(int)y+(int)vy,31,31).intersects(tempObject.getBounds()))
return true;


if(tempObject.getId() == ID.Crate)
if(getBounds().intersects(tempObject.getBounds()))
main.ammo+=50;
handler.removeObject(tempObject);


if(tempObject.getId() == ID.Enemy)
if(getBounds().intersects(tempObject.getBounds()))
main.hp--;//TODO make it so you cant get stuck in enemies and they drain all your health



return false;



and the move function



public void Move(double vx, double vy) 
if(!collision(vx,vy))
x+=vx;
y+=vy;





the tick method is also slightly altered



public void tick() vy!=0)
Move(vx,0);
Move(0,vy);

//anim.runAnimation();






share|improve this answer






















    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53291840%2fusing-rectangle-intersects-for-collision-detection-causes-objects-to-stick-to%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    In case anyone reading this is curious, I made a new function that handles movement and converted collision into a boolean that returns true if player is colliding with blocks (walls) and false if colliding with anything else.



    I also change the check for the block collision, as seen in the code



    private boolean collision(double vx, double vy) 
    for(int i = 0; i< handler.object.size(); i++)
    GameObject tempObject = handler.object.get(i);
    if(tempObject.getId() == ID.Block)
    if(new Rectangle((int)x+(int)vx,(int)y+(int)vy,31,31).intersects(tempObject.getBounds()))
    return true;


    if(tempObject.getId() == ID.Crate)
    if(getBounds().intersects(tempObject.getBounds()))
    main.ammo+=50;
    handler.removeObject(tempObject);


    if(tempObject.getId() == ID.Enemy)
    if(getBounds().intersects(tempObject.getBounds()))
    main.hp--;//TODO make it so you cant get stuck in enemies and they drain all your health



    return false;



    and the move function



    public void Move(double vx, double vy) 
    if(!collision(vx,vy))
    x+=vx;
    y+=vy;





    the tick method is also slightly altered



    public void tick() vy!=0)
    Move(vx,0);
    Move(0,vy);

    //anim.runAnimation();






    share|improve this answer



























      0














      In case anyone reading this is curious, I made a new function that handles movement and converted collision into a boolean that returns true if player is colliding with blocks (walls) and false if colliding with anything else.



      I also change the check for the block collision, as seen in the code



      private boolean collision(double vx, double vy) 
      for(int i = 0; i< handler.object.size(); i++)
      GameObject tempObject = handler.object.get(i);
      if(tempObject.getId() == ID.Block)
      if(new Rectangle((int)x+(int)vx,(int)y+(int)vy,31,31).intersects(tempObject.getBounds()))
      return true;


      if(tempObject.getId() == ID.Crate)
      if(getBounds().intersects(tempObject.getBounds()))
      main.ammo+=50;
      handler.removeObject(tempObject);


      if(tempObject.getId() == ID.Enemy)
      if(getBounds().intersects(tempObject.getBounds()))
      main.hp--;//TODO make it so you cant get stuck in enemies and they drain all your health



      return false;



      and the move function



      public void Move(double vx, double vy) 
      if(!collision(vx,vy))
      x+=vx;
      y+=vy;





      the tick method is also slightly altered



      public void tick() vy!=0)
      Move(vx,0);
      Move(0,vy);

      //anim.runAnimation();






      share|improve this answer

























        0












        0








        0







        In case anyone reading this is curious, I made a new function that handles movement and converted collision into a boolean that returns true if player is colliding with blocks (walls) and false if colliding with anything else.



        I also change the check for the block collision, as seen in the code



        private boolean collision(double vx, double vy) 
        for(int i = 0; i< handler.object.size(); i++)
        GameObject tempObject = handler.object.get(i);
        if(tempObject.getId() == ID.Block)
        if(new Rectangle((int)x+(int)vx,(int)y+(int)vy,31,31).intersects(tempObject.getBounds()))
        return true;


        if(tempObject.getId() == ID.Crate)
        if(getBounds().intersects(tempObject.getBounds()))
        main.ammo+=50;
        handler.removeObject(tempObject);


        if(tempObject.getId() == ID.Enemy)
        if(getBounds().intersects(tempObject.getBounds()))
        main.hp--;//TODO make it so you cant get stuck in enemies and they drain all your health



        return false;



        and the move function



        public void Move(double vx, double vy) 
        if(!collision(vx,vy))
        x+=vx;
        y+=vy;





        the tick method is also slightly altered



        public void tick() vy!=0)
        Move(vx,0);
        Move(0,vy);

        //anim.runAnimation();






        share|improve this answer













        In case anyone reading this is curious, I made a new function that handles movement and converted collision into a boolean that returns true if player is colliding with blocks (walls) and false if colliding with anything else.



        I also change the check for the block collision, as seen in the code



        private boolean collision(double vx, double vy) 
        for(int i = 0; i< handler.object.size(); i++)
        GameObject tempObject = handler.object.get(i);
        if(tempObject.getId() == ID.Block)
        if(new Rectangle((int)x+(int)vx,(int)y+(int)vy,31,31).intersects(tempObject.getBounds()))
        return true;


        if(tempObject.getId() == ID.Crate)
        if(getBounds().intersects(tempObject.getBounds()))
        main.ammo+=50;
        handler.removeObject(tempObject);


        if(tempObject.getId() == ID.Enemy)
        if(getBounds().intersects(tempObject.getBounds()))
        main.hp--;//TODO make it so you cant get stuck in enemies and they drain all your health



        return false;



        and the move function



        public void Move(double vx, double vy) 
        if(!collision(vx,vy))
        x+=vx;
        y+=vy;





        the tick method is also slightly altered



        public void tick() vy!=0)
        Move(vx,0);
        Move(0,vy);

        //anim.runAnimation();







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 15 '18 at 2:07









        play minecraftplay minecraft

        61




        61



























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53291840%2fusing-rectangle-intersects-for-collision-detection-causes-objects-to-stick-to%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            這個網誌中的熱門文章

            How to read a connectionString WITH PROVIDER in .NET Core?

            In R, how to develop a multiplot heatmap.2 figure showing key labels successfully

            Museum of Modern and Contemporary Art of Trento and Rovereto