Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7537613
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T06:53:56+00:00 2026-05-30T06:53:56+00:00

I have this game I’m writing with the jQuery Collision, and it uses keyboard

  • 0

I have this game I’m writing with the jQuery Collision, and it uses keyboard keys to move a div, when a div touches another div, it should prevent overlapping.

How on earth do I do that?

HTML —-

<!DOCTYPE html>
<html>
<head>
    <title></title>

    <link href="style.css" rel="stylesheet" />

    <script src="jquery.min.js" rel="javascript"></script>
    <script src="game.js" rel="javascript"></script>
    <script src="jquery-collision-1.0.1.js" rel="javascript"></script>
    <script src="jquery-ui-draggable-collision-1.0.1.js" rel="javascript"></script>

</head>
<body>

<div id="office">

    <div class="popup">
        <p>hello desk</p>
        <a class="close">X</a>
    </div>

    <div class="chris"></div>

    <!--Rian, Peter, Chris, Christopher ---------------- DESK -->
    <div class="desk-full" style="position: absolute; right: 50px; top: 50px;">

        <div class="christopher-stuff"></div>

    </div>

    <!--Adrian, Tatam, Ellize ---------------- DESK -->
    <div class="desk-full" style="position: absolute; right: 50px; top: 300px;">

    </div>

</div>

</body>
</html>

JAVASCRIPT—-

$(document).ready(function(){

$(".chris").click(function(){

var KEY = {
    UP: 38,
    DOWN: 40,
    LEFT: 37,
    RIGHT: 39
}

// a global object to store all global variable used for the game
var office = {
}

// an array to remember which key is pressed and which is not.
office.pressedKeys = [];

$(function(){
    // set interval to call gameloop every 30 milliseconds
    office.timer = setInterval(gameloop,30);

    // mark down what key is down and up into an array called "pressedKeys"
    $(document).keydown(function(e){
        office.pressedKeys[e.keyCode] = true;
    });
    $(document).keyup(function(e){
        office.pressedKeys[e.keyCode] = false;
    });
});

// this function is called every 30 milliseconds 
function gameloop()
{
    moveMe();
    collideDetection();
}

function moveMe()
{  

    if (office.pressedKeys[KEY.UP]) // arrow up
    {
        var direction = parseInt($(".chris").css("top"));
        $(".chris").css({
                top: direction-5,
                background: "url(chris-top.gif) no-repeat !important",
                width: "43px",
                height: "44px"
            }); 
    }
    if (office.pressedKeys[KEY.DOWN]) // arrow down
    {
        var direction = parseInt($(".chris").css("top"));
        $(".chris").css({
            top: direction+5,
            background: "url(chris-down.png) no-repeat !important",
            width: "48px",
            height: "25px"
            });
    }
    if (office.pressedKeys[KEY.LEFT]) // left
    {
        var direction = parseInt($(".chris").css("left"));
        $(".chris").css({
            left: direction-5,
            background: "url(chris-left.gif) no-repeat !important",
            width: "43px",
            height: "44px"
            });
    }
    if (office.pressedKeys[KEY.RIGHT]) // right
    {
        var direction = parseInt($(".chris").css("left"));
        $(".chris").css({
            left: direction+5,
            background: "url(chris-right.gif) no-repeat !important",
            width: "43px",
            height: "44px"
            });         
    }



        if (office.pressedKeys[KEY.UP] & office.pressedKeys[KEY.RIGHT]) // arrow up
    {
        $(".chris").css({
                background: "url(chris-top-right.png) no-repeat left top !important",
                width: "43px",
                height: "44px"
            }); 
    }
        if (office.pressedKeys[KEY.UP] & office.pressedKeys[KEY.LEFT]) // arrow up
    {
        $(".chris").css({
                background: "url(chris-top-left.png) no-repeat !important",
                width: "43px",
                height: "44px"
            }); 
    }
        if (office.pressedKeys[KEY.DOWN] & office.pressedKeys[KEY.RIGHT]) // arrow down
    {
        $(".chris").css({
                background: "url(chris-down-right.png) no-repeat !important",
                width: "43px",
                height: "44px"
            });
    }
        if (office.pressedKeys[KEY.DOWN] & office.pressedKeys[KEY.LEFT]) // arrow down
    {
        $(".chris").css({
                background: "url(chris-down-left.png) no-repeat !important",
                width: "43px",
                height: "44px"
            });
    } 
}

function collideDetection(){
    var chrisY = parseInt($(".chris").css("top"));
    var chrisX = parseInt($(".chris").css("left"));

    var chrisY = chrisY + 50;
    var chrisX = chrisX + 50;

    // -------------------- jQuery Collision Dtection and Prevention of Overlapping ---------------------
    var hit_list = $(".chris").collision( { obstacle: ".desk-full", preventCollision: true } );

    var officeHeight = parseInt($("#office").height());
    var officeWidth = parseInt($("#office").width());

    if(shipX <= 0) {
        $(".chris").css({
            left: "0",
            background: "url(chris-right.png) no-repeat !important",
            width: "25px",
            height: "48px"
        }); 
        }

    if(chrisX > officeWidth) {
        $(".chris").css({
            left: "0px",
            background: "url(chris-left.png) no-repeat !important",
            width: "25px",
            height: "48px"
        }); 
        }

    if(chrisX <= 0) {
        $(".chris").css({
            top: "0",
            background: "url(chris-down.png) no-repeat !important",
            width: "48px",
            height: "25px"
        }); 
        }

    if(chrisY > officeHeight) {
        $(".chris").css({
            top: "0px",
            background: "url(chris-up.png) no-repeat !important",
            width: "48px",
            height: "25px"
        }); 
        }
}

});    
});
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T06:53:57+00:00Added an answer on May 30, 2026 at 6:53 am
    $("#right").click(function(){
      $(".block").animate({"left": "+=50px"}, "fast", checkCollisions);
    });
    
    $("#left").click(function(){
      $(".block").animate({"left": "-=50px"}, "fast", checkCollisions);
    });
    
    $("#up").click(function(){
      $(".block").animate({"top": "-=50px"}, "fast", checkCollisions);
    });
    
    $("#down").click(function(){
      $(".block").animate({"top": "+=50px"}, "fast", checkCollisions);
    });
    
    function getPositions(box) {
      var $box = $(box);
      var pos = $box.position();
      var width = $box.width();
      var height = $box.height();
      return [ [ pos.left, pos.left + width ], [ pos.top, pos.top + height ] ];
    }
            
    function comparePositions(p1, p2) {
      var x1 = p1[0] < p2[0] ? p1 : p2;
      var x2 = p1[0] < p2[0] ? p2 : p1;
      return x1[1] > x2[0] || x1[0] === x2[0] ? true : false;
    }
    
    function checkCollisions(){
      var box = $(".bomb")[0];
      var pos = getPositions(box);
    
      var pos2 = getPositions(this);
      var horizontalMatch = comparePositions(pos[0], pos2[0]);
      var verticalMatch = comparePositions(pos[1], pos2[1]);            
      var match = horizontalMatch && verticalMatch;
      if (match) { $("body").append("<p>COLLISION !!!</p>"); }
    }
    .block {
      position:absolute;
      background-color:#abc;
      left:50px;
      width:90px;
      height:90px;
      margin:5px;
    }
    .bomb {
      position:absolute;
      background-color:red;
      left:250px;
      width:40px;
      height:40px;
      margin:5px;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
    <button id="left">left</button>
    <button id="right">right</button>
    <button id="up">up</button>
    <button id="down">down</button>
    
    <div class="block"></div>
    <div class="bomb"></div>

    You can use JQuery Collision and JQuery UI Draggable Collision.

    JQuery Collision extension returns the collisions between two selectors. Handles padding, margin, borders, and can determine either overlap or portion outside.

    With JQuery Collision you can find all overlaps:

    var list = $("#selector").collision(".obstacle");
    

    Returns a list of all elements that overlap “#selector”.

    With JQuery UI Draggable, you can bind an event when you drag an element and a collision happens and you can prevent the collision:

    $("#selector").draggable( { obstacle: ".obstacle", preventCollision: true } );
    

    Some plugins for collision detection:
    Collidable Draggables,
    Collision Check Plugin v1.1,
    $.event.special.drop

    Also :

    var overlaps = (function () {
        function getPositions( elem ) {
            var pos, width, height;
            pos = $( elem ).position();
            width = $( elem ).width();
            height = $( elem ).height();
            return [ [ pos.left, pos.left + width ], [ pos.top, pos.top + height ] ];
        }
    
        function comparePositions( p1, p2 ) {
            var r1, r2;
            r1 = p1[0] < p2[0] ? p1 : p2;
            r2 = p1[0] < p2[0] ? p2 : p1;
            return r1[1] > r2[0] || r1[0] === r2[0];
        }
    
        return function ( a, b ) {
            var pos1 = getPositions( a ),
                pos2 = getPositions( b );
            return comparePositions( pos1[0], pos2[0] ) && comparePositions( pos1[1], pos2[1] );
        };
    })();
    
    $(function () {
        var area = $( '#area' )[0],
            box = $( '#box0' )[0],
            html;
        
        html = $( area ).children().not( box ).map( function ( i ) {
            return '<p>Red box + Box ' + ( i + 1 ) + ' = ' + overlaps( box, this ) + '</p>';
        }).get().join( '' );
    
        $( 'body' ).append( html );
    });
    body {
        padding: 30px;
        color: #444;
        font-family: Arial, sans-serif;
    }
    
    h1 {
        font-size: 24px;
        margin-bottom: 20px;
    }
    
    #area {
        border: 2px solid gray;
        width: 500px;
        height: 400px;
        position: relative;
    }
    
    #area > div {
        background-color: rgba(122, 122, 122, 0.3);
        position: absolute;
        text-align: center;
        font-size: 50px;
        width: 60px;
        height: 60px;
    }
    
    #box0 {
        background-color: rgba(255, 0, 0, 0.5) !important;
        top: 150px;
        left: 150px;
    }
    
    #box1 {
        top: 260px;
        left: 50px;
    }
    
    #box2 {
        top: 110px;
        left: 160px;
    }
    
    #box3 {
        top: 200px;
        left: 200px;
    }
    
    #box4 {
        top: 50px;
        left: 400px;
    }
    
    p {
        margin: 5px 0;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <h1>Detect overlapping</h1>
    <div id="area">
        <div id="box0"></div>
        <div id="box1">1</div>
        <div id="box2">2</div>
        <div id="box3">3</div>
        <div id="box4">4</div>
    </div>
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Okay, so I have this partially working game, I know the solutions should be
I have a problem with my current game.In this game i have to move
I have developed a game using HTML5 and Javascript/JQuery. This game is a Tile
I have this Tetris game written in Java, which uses DB to record high
I have this game where balloons are coming from bottom and player has to
I have this game with some variety of textures. Whenever I draw a texture
So I have this game database, where I have several users with fields, id,
I'm making this game where I'm trying to pair people. So I have this
Ok, so I have this key listener for my game and it isn't working
I have found this line of code in a game that I study int

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.