开发者

i want my shape when hit with inside of stage , rotate and countinue move in true angle forever

![My Map][1]

i want simulate Billiard ball in my project. but my code does not work good. when start with (for example) 34 degree , when hit with wall(stage in this example) return with true degree. in flash , as3

public function loop(e:Event) : void
        {
            if(luanch)
            {



                y += Math.sin(degreesToRadians(rotation)) * speed;
                x += Math.cos(degreesToRadians(rotation)) * speed;






            if (x > stage.stageWidth){

                rotation -= 90;
                x = stage.stageWidth;


                trace("X :" , x , rotation);


                }
            else if (x < 0)
            {
                rotation += 90;
                x=3;

                //rotation += 90;

                trace("X :" , x , rotation);



            }

            if (y > stage.stageHeight)
            {
                y = stage.stageHeight;
                rotation -= 90;

                trace("Y : " , y , rotation);


            }

             else if (y <0)
            {
                rotation += 90;

                //rotation += 90;
                trace("Y :" , y , rotation);


            }
        }






        }

        public function degreesToRadians(degrees:Number) : Num开发者_StackOverflowber
        {
            return degrees * Math.PI / 180;
        }

    }


}


Your physics is wrong: when a ball bounces off a wall, its velocity does not (usually) change by 90 degrees, it reflects. Also, you are using x to mean x position and also x velocity, but they are different.

Your question is about the angle, but you cannot fix that until you fix the position/velocity thing.


My code in this answer will show you how a good way to move your objects. (same system your using now) You could easily adapt this to your system. Movement of Objects in a simulation

Now to reflect off the wall just reverse the x or y velocity depending on what wall it hit.

vx *= -1; // if you hit the left or right side of the stage.
vy *= -1; // if you hit the top or bottom of the stage.

Note: There is an issue with bouncing things off the wall like this. Every once in a while when you bounce the object will oscillate and get stuck bouncing off the wall ever frame.

The easiest way around this (with the code your using) is when you check for a collision with the stage boundaries make sure you take into account the size of the object.

Something like this.

if(x - width < 0)
{
     x = width;
     // we would now want to change the velocity.
     vx *= -1;
}

if( x + width > stage.stageWidth)
{
     x = stage.stageWidth - width;
     // we would now want to change the velocity.
     vx *= -1;
}

same basic thing for the y axis. Hope this helps.

Edit: by the way if your wanting to do billiard balls you really should use vectors. They will save you some headaches when you go to do collision detection against fast moving objects.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜