0
votes

I am writing a code where there are different rings on the stage, and a ball is rotating in one of these orbits. On click, the ball leaves its ring and travels normal to the ring. On its way if it hits another ring, it starts rotating in that ring from the point it hits the ring. I could successfully code the initial part of the code. but I am getting a problem when the ball hits another ring, it does not start from the location it hit. Instead it starts rotating in the ring from a different position. I hope I am clear with my doubt. It is somewhat similar to this game https://play.google.com/store/apps/details?id=com.drgames.orbit.jumper

here is my code in enter frame :

    if(!isTravelling){
        rad = Math.abs(angle * (Math.PI / 180)); // Converting Degrees To Radians
        _ball.x = currOrbit.x + (currOrbit.width / 2) * Math.cos(rad); // Position The Orbiter Along x-axis
        _ball.y = currOrbit.y + (currOrbit.width / 2) * Math.sin(rad); // Position The Orbiter Along y-axis
        trace(_ball.x+" , "+_ball.y);
        angle -= speed; // Object will orbit clockwise
        _ball.rotation = (Math.atan2(_ball.y - currOrbit.y, _ball.x - currOrbit.x) * 180 / Math.PI) + 90;
    }

    for(var i = 0; i < orbits.length; i++){
        if(orbits[i] != currOrbit){
            if(ObjectsHit(orbits[i], _ball)){
                currOrbit = orbits[i];
                _ball.rotation = (Math.atan2(_ball.y - currOrbit.y, _ball.x - currOrbit.x) * 180 / Math.PI) + 90;
                isTravelling = false;
                stage.addEventListener(MouseEvent.CLICK, onClick);
            }
        }
    }

    if(isTravelling){
        _ball.x += Math.cos(rad) * speed * 2;  
        _ball.y += Math.sin(rad) * speed * 2;
    }
1

1 Answers

1
votes

You seem to have forgotten to calculate the starting angle for the new orbit:

if(!isTravelling){
        rad = Math.abs(angle * (Math.PI / 180)); // Converting Degrees To Radians
        _ball.x = currOrbit.x + (currOrbit.width / 2) * Math.cos(rad); // Position The Orbiter Along x-axis
        _ball.y = currOrbit.y + (currOrbit.width / 2) * Math.sin(rad); // Position The Orbiter Along y-axis
        trace(_ball.x+" , "+_ball.y);
        _ball.rotation = angle // no need for calculating the angle again
        angle -= speed; // Object will orbit clockwise
    }

    for(var i = 0; i < orbits.length; i++){
        if(orbits[i] != currOrbit){
            if(ObjectsHit(orbits[i], _ball)){
                currOrbit = orbits[i];
                angle = (Math.atan2(_ball.y - currOrbit.y, _ball.x - currOrbit.x) * 180 / Math.PI) + 90; // Calculate the starting angle on the new orbit
                _ball.rotation = angle // Set the balls rotation to the angle
                isTravelling = false;
                stage.addEventListener(MouseEvent.CLICK, onClick);
            }
        }
    }

    if(isTravelling){
        _ball.x += Math.cos(rad) * speed * 2;  
        _ball.y += Math.sin(rad) * speed * 2;
    }

This should fix it. Please note that you might have to add or subtract 90 degrees to your balls rotation (_ball.rotation = angle + 90) because im not 100% sure about that.