开发者

Simple JQuery Game - How to know when two cars (divs) run into each other?

开发者 https://www.devze.com 2023-02-10 16:14 出处:网络
I have tried to make this simple Jquery game http://www.abkeya.com/2011/02/14/simple-jquery-game/ Most of the parts work good but as you would see I have used two div one is player\'s car and 2nd is

I have tried to make this simple Jquery game

http://www.abkeya.com/2011/02/14/simple-jquery-game/

Most of the parts work good but as you would see I have used two div one is player's car and 2nd is opponent car, w开发者_如何学Gohat would be best for me if I wish to know when two cars run into each other? or any better suggestions to take it a but further?

Also the street lines get toggle height when they reach bottom of the street and disappear, I wonder how would I do the same for start of the street so the car and street lines come in naturally.


Take a look at this if you don't mind using plugins for jQuery: http://www.labs.skengdon.com/?page=/_hittest/


One strategy you can use is to use the idea of bounding boxes for your sprites. Since your cars are fairly rectangle it is relatively easy. At each clock cycle you would just need to check to see if one of the four axes of Car1 are inside of the bounding box of Car2. To do this you would just need to grab the top, left, top + height, and top + width (a box) of each car.

As for the seamless entry of the objects, have you tried starting them at a negative position so that they are off the screen initially? This will then cause them to gradually come onto the screen to make it look for seamless.

Let me know if you need further examples for any of these things!


I think you just need something like a simple box collision.

The fun thing of such a car is that it's probably just a DIV. The car has a fixed height and width and a position that changes with the mouse.

What we could do on every mousemove or every opponent-car-move.

function intersects(a,b)
{
    if(a.x + a.w > b.x)
        sectHor = true;
    if(a.x < b.x + b.w)
        sectHor = true;
    if(a.y < b.y + b.h)
        sectVert = true;
    if(a.y + a.h > b.y)
        sectVert = true;

    if(sectHor && sectVert)
        return true;
    else return false;
}

I'm not sure if this works, but there are of course a lot of other ways to calculate this. A thing I'm worried about, is when A is larger than B in all aspects. It will overlap and maybe my intersection function will not return true.

0

精彩评论

暂无评论...
验证码 换一张
取 消