A Flash Developer Resource Site

Results 1 to 16 of 16

Thread: [MX]Collision detection in 3d perspective

  1. #1
    Junior Member
    Join Date
    Jun 2008
    Posts
    18

    [MX]Collision detection in 3d perspective

    Hi,

    How do I go about collision detection or hittest in a one point perspective game .
    for eg how do I perform hittest between the ball and bat in the baseball game?
    Or how do I detect collision between two cars in 3d perspective car racing game?

    Thanks for your replies.

  2. #2
    Student
    Join Date
    Apr 2001
    Location
    -
    Posts
    4,756
    hitTest is typically for designers or the ones who dont want to think much in depth of how to do it in a different way. It usually works best in 2d space or in combination with the mouse because mouse coordinates are always screen 2d coordinates.


    Other possibilities are for example using vectors and check for intersections between 2 of them,- or in other words checking if 2 lines collide and if where.
    One advantage of this way is that not only can you check a more complex situration (not just if one spot hits a MC, or the bounding boxes of 2 MC´s hit each other) but for example how deep something gets stuck into the other object ect.

    here is a mathmatical approach:
    http://mathworld.wolfram.com/Line-LineIntersection.html
    (though even I dont understand it at first glance)

    a AS3 in depth comparison between multiple solutions:
    http://www.actionscript.org/resource...ons/Page1.html

    cheezworld (a member here) recently released some of his classes including some math stuff like intersecting of 2 lines,- you can find it under math classes under:
    http://cheezeworld.com/actionscript-...me-developers/

  3. #3
    Senior Member
    Join Date
    Sep 2004
    Posts
    633
    hitTest is typically for designers or the ones who dont want to think much in depth of how to do it in a different way
    And the other ways are for code masturbators.

    Just test x, y coordinates/points of ball and bat ( for example ) then add scale property. Let's say that the ball is coming at you, even if it is hitting the bat according to hitTest, it should not register as a hit until ball scales up to 100%, meaning it has reached the plate or bat. Then you can add other factors depending on how real you want to get with physics.

    Same thing with cars, if they are roughly the same scale, that means they are about the same distance from your eyes/camera, then they can hit each other, otherwise they can't.

    It's pretty simple without getting into some quantum mechanics math. People who do those never do anything beyond theories and demos anyway. If your goal is to complete a game, you have to use a practical approach.

  4. #4
    M.D. mr_malee's Avatar
    Join Date
    Dec 2002
    Location
    Shelter
    Posts
    4,139
    gonna have to agree with render, collision detection/reaction should really be done properly. It will help in the future, if you keep doing hacky sloppy workarounds you'll never get anywhere.
    lather yourself up with soap - soap arcade

  5. #5
    Senior Member
    Join Date
    Jun 2007
    Location
    Planet Earth
    Posts
    358
    You should google Raycasting. I hear thats what they use for most 3d games these days. It basically involves drawing 3d vectors (two 2D vectors = 3d vector) and testing if they hit a particular target.
    America!

  6. #6
    Senior Member
    Join Date
    Sep 2004
    Posts
    633
    There is nothing improper or sloppy about what I described.

    What is improper is using 10 times the required code to do the same thing that can be done in a much simpler way, as it would be in original poster's example ( baseball / bat collision ).

    Proper is whatever works.

    You should google Raycasting. I hear thats what they use for most 3d games these days.
    If he has to ask, how to check for 3D collision, don't you think it's a bit too much.

    Nothing wrong with learning all these things, but they are not necessary for what he's asking. And in case of racing, do you want to have a game with "proper" collision detection that because of that can only handle 2-3 cars at a time and crawls along like most flash racers we see, or do you want to have something that feels fast and gets by on slightly "improper" collision detection?
    Last edited by MikeMD; 06-13-2008 at 11:23 PM.

  7. #7
    Senior Member
    Join Date
    Nov 2003
    Location
    Las Vegas
    Posts
    770
    If you use 100% scale plus hitTest, you are only testing for a particular degree of bat rotation in the swing. Without some fudge factor, you would either only get a hit at a very precise moment, or always get a hit when the ball hit's 100% and the bat is swinging.

    If you want to take this method to any level of precision, you will be doing several extra checks in order to determine a true hit and using some of the slower trig functions to find it's angle. This negates any programing or processing advantage over vectors.

    In other words, use vectors. They are easy to learn, have none of the rotation issues of flash's trig functions, and are both efficient and precise.

  8. #8
    Senior Member
    Join Date
    Sep 2004
    Posts
    633
    or always get a hit when the ball hit's 100% and the bat is swinging.
    Not even close, you can swing too early or too late and bat will not hit when the ball is at 100%. It also depends on animation, player swinging could have 3-4 frames as the bat passes over the plate, with frames #3 and 4 being on the way up and past the plate, in which case it could make contact with the ball in front of the plate ( if the ball is there ) and then the ball would be at 90-95% scale. 100% is just a reference point where both the ball and bat are in an ideal position. This is still extremely simple to do and there is no need for vector math. Even if you do use vector math and use z property that you create to track the ball, you will scale it according to that. In the end it comes down to scale anyway, so cut out the middle men when you don't need them.

    You guys are giving typical programmer responses which completely disregard player animation and its setup in the game. Bat swing has to happen fast, so you can't have more than 3-4 frames where a hit is possible. Use that to your advantage, you only need to check for hits during those frames, and you can get all the info using x,y and scale ( that is unless you are just a programmer and can't think like a game designer ).

  9. #9
    Senior Member
    Join Date
    Jun 2007
    Location
    Planet Earth
    Posts
    358
    Actually, if you're doing that, then you don't have to do any 3d collision detection at all. You can just prerender or throw together some rendering code, and then predefine where the ball will go, depending on how the user hits.
    America!

  10. #10
    Senior Member
    Join Date
    Sep 2004
    Posts
    633
    Actually, if you're doing that,
    If you are doing what? Is there any other way to have a player swing?

    There is nothing pre-rendered other than the player animation. you still have complately calculated ball path depending on when and where it makes contact with the bat. Nothing predefined, and it can all be done with just x, y and scale info from the ball. And there is nothing predetermined about the filght of the ball coming towards you either. You just set certain parameters so some of them are fastballs, some curve and so on.
    Last edited by MikeMD; 06-14-2008 at 12:54 AM.

  11. #11
    Student
    Join Date
    Apr 2001
    Location
    -
    Posts
    4,756
    here is a simple AS1/2 example code with a function based on cheezworlds function from AS3,
    just copy paste it in the main timeline and hit test movie, you should get a demo where you can cut on a path with the mouse.

    Using vectors ({x:?,y:?} objects) it should be even possible doing it in 3d space,- though usually a abstraction into the 2d world is done for the mechanics and rather ported into the 3d world
    PHP Code:
    function checkIntersection(A:ObjectB:ObjectC:ObjectD:Object){
        var 
    rTop:Number = (A.y-C.y)*(D.x-C.x)-(A.x-C.x)*(D.y-C.y);
        var 
    sTop:Number = (A.y-C.y)*(B.x-A.x)-(A.x-C.x)*(B.y-A.y);
        var 
    bot:Number     = (B.x-A.x)*(D.y-C.y)-(B.y-A.y)*(D.x-C.x);
        
        if(
    bot == 0) return false//parallel lines
        
        
    var invBot:Number     bot;
        var 
    r:Number        rTop invBot;
        var 
    s:Number        sTop invBot;
        
        if( (
    0) && (1) && (0) && (1) ) return true;
        return 
    false;
    }
    //usage:

    var path:Array = new Array();
    path = [{x:32,y:32},{x:320,y:40},{x:280,y:160},{x:320,y:240},{x:40,y:240},{x:32,y:32}];

    _root.createEmptyMovieClip("mc_lines1",1);
    _root.createEmptyMovieClip("mc_lines2",2);    

    var 
    mousePos = {x:_xmouse,y:_ymouse};//the reference position of the mouse
    var timer getTimer();
    var 
    hit false;



    function 
    codeMasturbating(){
        
    hit false;//reset hit each frame
        
        //moveClip for border MC
        
    mc_lines1.clear();
        
    mc_lines1.lineStyle(0,0x0000ff,100);
        
    mc_lines1.moveTo(path[0].x,path[0].y);
        
        for (var 
    i:Number 0;i<path.length-1;i++){
            var 
    A:Object = {x:path[i].x,y:path[i].y};//first loop element
            
    var B:Object = {x:path[i+1].x,y:path[i+1].y};//next loop element
            
            
    if (checkIntersection(A,B,{x:_xmouse,y:_ymouse},mousePos)){//if the line intersects just once with any line of the path,- abort this loop
                
    hit true;//important!!! flag this variable true so we know afterwards that the line intersects
                
    mc_lines1.lineStyle(2,0xFF952B,100);
                
    mc_lines1.lineTo(B.x,B.y);
            }else{
                
    mc_lines1.lineStyle(0,0x0000ff,100);
                
    mc_lines1.lineTo(B.x,B.y);
            }
        }
        
    //
        
    if(hit){//if we detected a hit with the border before do this:
            
    mc_lines2.lineStyle(0,0xff0000,100);
        }else{
            
    mc_lines2.lineStyle(0,0x00ff00,100);
        }
        
    mc_lines2.moveTo(mousePos.x,mousePos.y);
        
    mc_lines2.lineTo(_xmouse,_ymouse+random(2));
        
        if (
    getTimer() > timer+100){//after a certain time (in miliseconds) reset the mouse reference position
            
    mousePos._xmouse;//update mouse object
            
    mousePos._ymouse;
            
    timer getTimer();//update timer so that the next interval will come in future
        
    }
    }
    _root.onEnterFrame codeMasturbating

  12. #12
    Senior Member
    Join Date
    Sep 2006
    Posts
    132
    Proper is whatever works
    I agree, but would further say, proper is whatever works for the game maker. You don't have to use complex math for some effects in 3D collision for example.

    Depends if you plan to do a simple game where the cam doesn't move much. If making something where the cam looks/moves around and more complex physics, maybe then best to use rays, math.

    In my case, I will start with a simple collision system to get a game engine off the ground, then work on the more complex stuff later, but that's me not sure how many ppl want to work that way.
    May 2009 - Working on Thorenzitha RPG - episode 7

  13. #13
    Senior Member
    Join Date
    Nov 2003
    Location
    Las Vegas
    Posts
    770
    Quote Originally Posted by MikeMD
    You guys are giving typical programmer responses which completely disregard player animation and its setup in the game. Bat swing has to happen fast, so you can't have more than 3-4 frames where a hit is possible. Use that to your advantage, you only need to check for hits during those frames, and you can get all the info using x,y and scale ( that is unless you are just a programmer and can't think like a game designer ).
    If you only use three to four frames, then you only have 4 angles at which the bat can make contact with the ball. Even giving additional pitch types such as curve, slider, or fastball, hat's a pretty limited simulation.

    A couple of dot products is not complicated, and is in fact less complicated than using timeline animation principles.

  14. #14
    Senior Member
    Join Date
    Sep 2004
    Posts
    633
    If you only use three to four frames, then you only have 4 angles at which the bat can make contact with the ball. Even giving additional pitch types such as curve, slider, or fastball, hat's a pretty limited simulation.
    No, more frames you use ( beyond those 3-4 ) , more limited of a simulation it becomes. Have you made one baseball game yet?

    Assuming we are trying to hit a home run, even 4 frames is one too many if you are running your game at 30-40 fps.
    Swing of the bat has to happen instantly after a player clicks to swing, and if you have too many frames it will feel like you have no control, as the animation plays out too slowly. It'll be like a 3 year old swinging.

    You can add more variety by moving the position of the player relative to mouse position. The most important thing is that player playing the game feels like he's in control and that it feels like a swing, not satisfying programmer's simulation requirements at the expense of gameplay. You can still have an infinite number of reactions depending on what you use to callculate ball flight ( you could use mouse position at the moment of contact, mouse under the ball it flies higher, mouse over the ball it goes down, left right positioning, whatever you want, and the ball can go anywhere, just like in real life ).

    A couple of dot products is not complicated, and is in fact less complicated than using timeline animation principles.
    I didn't say it was complicated, I just said it wasn't necessary and won't give you anything more accurate calculations wise, so why bother. You have to take into account timeline animation if your player is an animation ( if it isn't then who wants to play that anyway )
    The original poster asked how to hitTest in 3D. Obviously the most basic thing there is scale because you judge how far something is by its size as you view it from your perspective.
    Knowing that, he can have it done in a few minutes even if he doesn't know much about scripting. Using some of the above samples he may spend a few days going nowhere ( if indeed he is a beginner ). Then he can add the rest later as needed.

    If he already knows more, and wants to jump right into full 3d, he can do that too, I guess.

    Vector, Schmector
    Last edited by MikeMD; 06-15-2008 at 12:52 AM.

  15. #15
    Junior Member
    Join Date
    Jun 2008
    Posts
    18
    Thanks guys. My programming skills are of intermediate level. I will definitely learn the real 3d mathematics. But for now I will go for the simplier collision detection (mikeMD).

  16. #16
    M.D. mr_malee's Avatar
    Join Date
    Dec 2002
    Location
    Shelter
    Posts
    4,139
    good luck, I look forward to hearing from you soon
    lather yourself up with soap - soap arcade

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center