joacampb
joacampb

Reputation: 177

can anyone explain the math behind the red ship in this simple game? [homing missile esque]

the game is a little flash component found here: http://www.rydeman.com/erik/skepp2.html

i found that link here: http://board.flashkit.com/board/archive/index.php/t-175556.html

I have been using/converting the examples they give in the second link but cant get my "missile" to behave correctly. I am using java and andEngine combo. Here is what I have right now:

public boolean onSceneTouchEvent(Scene pScene,TouchEvent pSceneTouchEvent) {
            int Missile_speed=20;

            int dx=(int) (pSceneTouchEvent.getX()-(face.getWidth()/2));
            int dy=(int) (pSceneTouchEvent.getY()-(face.getHeight()/2));

            double distance=Math.sqrt((dx*dx)+(dy*dy));

            //dx/=distance;  /** not really sure what " /= " is on this line and the next **/
            //dy/=distance;

            if (pSceneTouchEvent.getAction() == MotionEvent.ACTION_DOWN)
            {
                //to do
            }

            if (pSceneTouchEvent.getAction() == MotionEvent.ACTION_MOVE)
            {

                face.registerEntityModifier(new MoveModifier(1, face.getX(), (int)(dx/distance)*Missile_speed, face.getY(),(int)(dy/distance)*Missile_speed, EaseQuadOut.getInstance()));
            }

            if (pSceneTouchEvent.getAction() == MotionEvent.ACTION_UP)
            {
                //to do
            }
            return true;            
            }

this code just makes the object travel to the upper right hand corner of the screen and it doesnt move after that. so i am missing something

my end goal is to have an object that will chase the users touch coordinates. I know how to have the object follow such coordinates but I dont want it to follow the exact path, but rather the shortest path to the most updated coordinates. so more like an intercept course

thank you

Upvotes: 1

Views: 293

Answers (1)

Petrucio
Petrucio

Reputation: 5679

dx and dy start with the distances in x and y to your target position (I guess you figured out that much, but bear with me)

When you divide dx or dy by the distance, you now have those values constrained between 0 and 1, and these values tell you where it is relation to that axis, without regard to distance (example: if the target is exactly to the right, no matter how far or close, dx/dist is 1 and dy/dist is 0).

Now when you multiply Missile_speed by those axis, the missile will chase that direction in it's maximum speed.

Those values between 0 and 1 are the cosine of the angle between the target position and the missile, btw (it's important that you visualize this so you can think through similar problems in these terms on your own in the future).

I'm not sure this is what you wanted - it seems you wanted the trigonometry of the situation explained and not working code, so that's what I tried to do (not sure if I succeeded). Really understand this, and you should have no problem with similar problems in the future (and these situations are really ubiquitous in game development, btw)

Upvotes: 4

Related Questions