0
votes

so I'm making a platformer style game and trying to make a hit test with a line if that makes any sense. Right now I have a object that moves around and whenever you're moving right, if the lower right or upper right side hit the world you stop moving. same thing set up with left. This works however if the "world" movieclip is too small and doesn't hit any of the points you can go right through it. So I need to make a solid line between the points and have a hitTest with that?

else if (keyIsDown(Keyboard.RIGHT))
        {
            //dude.gotoAndStop("right");
            //obj.scaleX = 1;
            for (i = 0; i<speedX; i++)
            {
                obj.x++;
                dude.ball.rotation++;
                if (status == "ground")
                {
                    dude.height+= 0.05;
                    dude.width += 0.05;

                }
                    if (world.hitTestPoint(obj.x + obj.width/8 - obj.width/2, obj.y - obj.height/4,true) || world.hitTestPoint(obj.x + obj.width/2,obj.y - obj.height + obj.height/4,true))
//this is what dictates the points that hit, trying to make it test a hit with a solid line between the 2 points.
                    //if (world.hitTestObject(dude.hitD))
                    {

                        dude.ball.rotation--;


    obj.x--;
                    break;
                }
            }
        }
        dude.gotoAndStop(1);

    }

line code I tried adding

else if (keyIsDown(Keyboard.RIGHT))
        {
            //dude.gotoAndStop("right");
            //obj.scaleX = 1;
            for (i = 0; i<speedX; i++)
            {
                obj.x++;
                dude.ball.rotation++;
                myShape.graphics.moveTo(obj.x + obj.width/8 - obj.width/2,obj.y - obj.height/4); 
                myShape.graphics.lineTo(obj.x + obj.width/2, obj.y - obj.height + obj.height/4);
                // The number in obj.y-4 affects the climbing ability
                if (status == "ground")
                {
                    //dude.height+= 0.05;
                    //dude.width += 0.05;
                }


                if (obj.hitTestObject(myShape))
                {
                    dude.ball.rotation--;
                    obj.x--;
                    break;
                }
            }
        }
        dude.gotoAndStop(1);

    }
1
so why not make this solid line and give it a solid_line.alpha = 0; now its invisible to the eye but the computer can detect a "hit" against it? Your question is not clear.. you dont know how to make the line or what?VC.One
@VC.One Already tried this by adding this line shape, maybe I'm not doing it right? See editTrows

1 Answers

1
votes

As far as I can understand, you are trying to make a platformer game where the player controls a ball. So you need circular collision detection, at least for going right and left.

While implementing any kind of program, especially on parts which will executed so often, like collision detection, you should always be looking for shortcuts.

I can suggest couple approaches;

  1. Shortcut for this case can be having an invisible box shape slightly smaller than your ball, and making collision detection with it. Sure, it will reduce the visual quality; e.g. when you approach a low edge which can fit your object's bottom left, your object will crash early, without visually touching the block.

  2. Or you just ignore the performance and increase the number of the points around the circle. Even you can write an algorithm to find positions on the outer edge of your circle with certain spacing, and check if any of them colliding. This way your collision detection cannot miss small objects.

  3. However, there's a better approach. You can implement two-level collision detection. First is what you have now, and second is an invisible box with same height of your ball, and smaller in width enough to it's edges fall behind your primary detection points. In this way, the secondary detection will not miss anything, but it will only work when the primary detection fails to catch anything between or below.