物理角度反弹as3

时间:2012-03-14 18:00:49

标签: actionscript-3 vector game-physics

下面是我目前的代码,我试图让我的球以适当的折射角反弹,基于随机倾斜的墙壁和球的方向...我的问题是我只参加了一个基本的物理课并且知道方程“入射角=折射角”也,这是我在as3的第一年,所以我的编码相当粗糙。角度似乎是关闭...问题是代码“Bullet.hitTestObject(myblockadeHolder [t])”谢谢你们。

stage.addEventListener(Event.ENTER_FRAME,rotate);
var _trueRotation:Number;
var _dx:Number;
var _dy:Number;
function rotate (e:Event){
// calculate rotation based on mouse X & Y
    _dx = Turret.x - stage.mouseX;
    _dy = Turret.y - stage.mouseY;

    // which way to rotate
    var rotateTo:Number = getDegrees(getRadians(_dx, _dy)); 

    // keep rotation positive, between 0 and 360 degrees
    if (rotateTo > Turret.rotation + 180) rotateTo -= 360;
    if (rotateTo < Turret.rotation - 180) rotateTo += 360;

    // ease rotation
    _trueRotation = (rotateTo - Turret.rotation - 90) / 3;

    // update rotation
    Turret.rotation += _trueRotation;           
}
//Turret Rotation
//Create an array to hold multiple sprites
var mySpriteHolder:Array = [];
//Create a counter to keep track of the number of sprites
var lbCounter:int = 0;
//Maximum number of sprites on the canvas
var maxLB:int = 1;
//Keypress Code
stage.addEventListener(MouseEvent.CLICK, dropBullet);
//Function for the mouse event to fire bullet
function dropBullet(evt:MouseEvent):void{

     var bcos:Number = Math.cos((Turret.rotation - 90) * Math.PI / 180);
     var bsin:Number = Math.sin((Turret.rotation - 90) * Math.PI / 180);

     //starting x and y
     var startx:int = Turret.x + (70 * bcos);
     var starty:int = Turret.y + (70 * bsin);
    //calculates where the bullet needs to go by aiming in front of the gun
    var endx:int = Turret.x + (100 * bcos);
    var endy:int = Turret.y + (100 * bsin);

    var Bullet:MovieClip = new bullet();

    Bullet.x = startx;
    Bullet.y = starty;

    Bullet.xspeed = (endx - startx)/5;
    Bullet.yspeed =  (endy - starty)/5;

    mySpriteHolder.push(Bullet);
    stage.addChild(Bullet);

    //this calls the move down function
    stage.addEventListener(Event.ENTER_FRAME,BulletFire);
}
//Function to shoot bullet
var Points:Number = 0;
var Life:Number = 100;
stage.addEventListener(Event.ENTER_FRAME, TextCounter);
function BulletFire(evt:Event):void{
    var Bullet:MovieClip;
    //Use a for loop to move the Bullets
    for(var i:int=mySpriteHolder.length-1; i>=0; i--){
        Bullet = mySpriteHolder[i];
    //Bounds Collision
    if(Bullet.hitTestObject(Up)){
        Bullet.yspeed*=-1;
    }
    if(Bullet.hitTestObject(Lower)){
        Bullet.yspeed*=-1;
    }
    if(Bullet.hitTestObject(Left)){
        Bullet.xspeed*=-1;
    }
    if(Bullet.hitTestObject(Right)){
        Bullet.xspeed*=-1;
    }
    if(Bullet.hitTestObject(Tank)){
        stage.removeChild(Bullet);
        mySpriteHolder.splice(i,1);
        lbCounter --;
        Life -= 10;
    }
    //Blockade Collision
    for(var t in myBlockadeHolder){
            if(Bullet.hitTestObject(myBlockadeHolder[t])){
                _trueRotation*=2
            var newAngle = (180 - (_trueRotation) - (smallangle))*-1;
            var newXspeed = Math.cos(newAngle);
            var newYspeed = Math.sin(newAngle);
            Bullet.xspeed = newXspeed+2.5;
            Bullet.yspeed = newYspeed+2.5;

            }
        }
    //Target Collision
        for(var c in mytargetHolder){
            if(Bullet.hitTestObject(mytargetHolder[c])){
            stage.removeChild(Bullet);
            mySpriteHolder.splice(i,1);
            lbCounter --;
            Points += 10;
            mytargetHolder[c].y = Math.random()*380 + 10;
            mytargetHolder[c].x = Math.random()*380 + 10;
                while(mytargetHolder[c].hitTestObject(Turret)){
                    mytargetHolder[c].y = Math.random()*380 + 10;
                    mytargetHolder[c].x = Math.random()*380 + 10;
                }
            }
            for(var a in mytargetHolder){
                for(var s in mytargetHolder){
                    while(mytargetHolder[a].hitTestObject(mytargetHolder[s])&& a!=s){
                        mytargetHolder[a].y = Math.random()*380 + 10;
                        mytargetHolder[a].x = Math.random()*380 + 10;
                    }
                }
                for(var g in myBlockadeHolder){
                    while(mytargetHolder[a].hitTestObject(myBlockadeHolder[g])&& a!=g){
                        mytargetHolder[a].y = Math.random()*380 + 10;
                        mytargetHolder[a].x = Math.random()*380 + 10;
                    }
                }
            }
        }
        Bullet.y += Bullet.yspeed;
        Bullet.x += Bullet.xspeed;
    }
}//Bullet Code
stage.addEventListener(Event.ENTER_FRAME, HealthCheck);
function HealthCheck(e:Event):void{
    if(Life<=0){
        stage.removeEventListener(MouseEvent.CLICK, dropBullet);
        stage.removeEventListener(Event.ENTER_FRAME, BulletFire);
        stage.removeEventListener(Event.ENTER_FRAME, droptarget);
        stage.removeEventListener(Event.ENTER_FRAME, dropblockade);
    }
}//Health Code
//variables for blockade
var myblockadeSprite:Sprite;
//blockade is the linkage name in the library
var blockade:Blockade;
//Create an array to hold multiple sprites
var myBlockadeHolder:Array = new Array();
//Create a counter to keep track of the number of sprites
var LbCounter:int = 0;
//Maximum number of sprites on the canvas
var maxlb:int = 6;
//Keypress Code
stage.addEventListener(Event.ENTER_FRAME, dropblockade);
//Function for the mouse event to fire blockade
function dropblockade(evt:Event):void{
    for(var i:int=0;i<maxlb; i++){
        //PLACE DO LOOP INSIDE TO GENERATE EMPTY IN RANDOM COORDS
        //add the blockades to the canvas
        myblockadeSprite = new Sprite();
        stage.addChild(myblockadeSprite);
        //Get the actual picture from the library
        blockade = new Blockade();
        myblockadeSprite.addChild(blockade);
        //Going to load up the array with the sprites
        myBlockadeHolder[i] = myblockadeSprite;
        myBlockadeHolder[i].y = Math.random()*390 + 10;
        myBlockadeHolder[i].x = Math.random()*390 + 10;
        myBlockadeHolder[i].rotation = Math.random()*360;
        while(myBlockadeHolder[i].hitTestObject(Tank)){
            myBlockadeHolder[i].y = Math.random()*390 + 10;
            myBlockadeHolder[i].x = Math.random()*390 + 10;
        }   
    }
    for(var t:int=0;t<maxlb; t++){
        for(var d:int=0;d<maxlb; d++){
            while(myBlockadeHolder[t].hitTestObject(myBlockadeHolder[d])&& t!=d){
            myBlockadeHolder[t].y = Math.random()*390 + 10;
            myBlockadeHolder[t].x = Math.random()*390 + 10;
            }
        }
    }
    stage.removeEventListener(Event.ENTER_FRAME, dropblockade);
}//Blockade Code
//variables for target
var mytargetSprite:Sprite;
//target is the linkage name in the library
var target:Target;
//Create an array to hold multiple sprites
var mytargetHolder:Array = new Array();
//Create a counter to keep track of the number of sprites
var TargetCounter:int = 0;
//Maximum number of sprites on the canvas
var maxtrgs:int = 3;
//Keypress Code
stage.addEventListener(Event.ENTER_FRAME, droptarget);
function droptarget(evt:Event):void{
    for(var i:int=0;i<maxtrgs; i++){
        //PLACE DO LOOP INSIDE TO GENERATE EMPTY IN RANDOM COORDS
        //add the targets to the canvas
        mytargetSprite = new Sprite();
        stage.addChild(mytargetSprite);
        //Get the actual picture from the library
        target = new Target();
        mytargetSprite.addChild(target);
        //Going to load up the array with the sprites
        mytargetHolder[i] = mytargetSprite;
        mytargetHolder[i].y = Math.random()*390 + 10;
        mytargetHolder[i].x = Math.random()*390 + 10;
        while(mytargetHolder[i].hitTestObject(Tank)){
            mytargetHolder[i].y = Math.random()*390 + 10;
            mytargetHolder[i].x = Math.random()*390 + 10;
        }   
    }
    for(var t:int=0;t<maxtrgs; t++){
        for(var d:int=0;d<maxtrgs; d++){
            while(mytargetHolder[t].hitTestObject(mytargetHolder[d])&& t!=d){
            mytargetHolder[t].y = Math.random()*390 + 10;
            mytargetHolder[t].x = Math.random()*390 + 10;
            }
        }
        for(var w:int=0;w<maxtrgs; w++){
            while(mytargetHolder[t].hitTestObject(myBlockadeHolder[w])&& t!=w){
            mytargetHolder[t].y = Math.random()*390 + 10;
            mytargetHolder[t].x = Math.random()*390 + 10;
            }
        }
    }
    stage.removeEventListener(Event.ENTER_FRAME, droptarget);
}//Target Code
function getRadians(delta_x:Number, delta_y:Number):Number{
    var r:Number = Math.atan2(delta_y, delta_x);

    if (delta_y < 0){
        r += (2 * Math.PI);
        }
    return r;
}

/**
* Get degrees
* @param    radians Takes radians
* @return   Returns degrees
*/
function getDegrees(radians:Number):Number{
    return Math.floor(radians/(Math.PI/180));
}
function TextCounter(e:Event):void{
    PointCounter.text = String(Points);
    LifeCounter.text = String(Life);
}

1 个答案:

答案 0 :(得分:1)

我不是一个真正的物理人,但我可以推荐你一篇文章/教程,它很可能会帮助你解决这个问题。如果是,请将您的固定代码作为答案发布,因为这是对这个问题的更好/更直接的答案。如果没有,希望其他人会得到一个更好的答案:

http://blog.generalrelativity.org/actionscript-30/dynamic-circlecircle-collision-detection-in-actionscript-3/