简体   繁体   English

触摸后连续运动?

[英]Continuous movement after touch?

Found plenty of questions that were similar, but nothing that answers my own problem. 找到许多相似的问题,但没有任何问题可以回答我自己的问题。 It's probably something really bloody simple, but it's late and I can't fathom it. 这可能真的很简单,但是已经晚了,我无法理解。 I've got an android game I'm working on, where you touch the screen and it 'fires' a sprite in that direction. 我正在开发一个Android游戏,您可以在该屏幕上触摸屏幕,并朝该方向“发射”一个精灵。 I've got most of the code working, however, after the sprite fires and moves off, it gets to the touch point and then just wiggles. 我已经完成了大多数代码的工作,但是,在精灵被触发并移开后,它到达了接触点,然后开始摆动。 I would like it to carry on along that angle and eventually bounce around... 我希望它沿着那个角度继续前进,最终反弹……

Here's my code (well the bits that matter): 这是我的代码(重要的是位):

List<TouchEvent> touchEvents = game.getInput().getTouchEvents();
        int len = touchEvents.size();

        for(int i = 0; i < len; i++) {
            TouchEvent event = touchEvents.get(i);                        
            if(event.type == TouchEvent.TOUCH_UP) {
                touchPoint.set(event.x, event.y);
                guiCam.touchToWorld(touchPoint);                               
            }
        }

        //Log.d("PANDAM", touchPoint.x + "|" + touchPoint.y);
        float speed = 112f;
        double theta = 180.0 / Math.PI * Math.atan2(panda_y - touchPoint.y, panda_x - touchPoint.x);
        Log.d("PANDAM", " > "+theta+" < ");

        movePanda(theta, speed, deltaTime);

And the "movePanda" method: 和“ movePanda”方法:

    private void movePanda(double angle, float speed, float deltaTime)
{   
    panda_x += speed * Math.cos(angle)*deltaTime;
    panda_y += speed * Math.sin(angle)*deltaTime;       
}

My question is, how do I get the panda to carry on along the touch vector and not spaz out when it reaches the original touch point? 我的问题是,如何让熊猫沿着触摸矢量继续前进,而不会在到达原始触摸点时散布出来?

You seem to be recalculating the angle each step of the animation, but you actually need to remember what the angle was when you first started moving. 您似乎正在重新计算动画每一步的角度,但是实际上您需要记住刚开始移动时的角度。 As you pass the original touch point, the angle changes to point the other direction, so if you reclcalculate it every animation step, it will change and eventually point the other way. 当您通过原始接触点时,角度将更改为指向另一个方向,因此,如果在每个动画步骤重新计算角度,它将改变并最终指向另一个方向。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM