简体   繁体   English

如何绑定libGDX中的压缩变焦?

[英]How to bound the pinch zoom in libGDX?

I have been trying to sort out the pinch zooming for a CameraInputController. 我一直试图解决CameraInputController的缩放缩放问题。 I want it to zoom in and out of the origin, but be bounded between 3 and 10. I put this bit of code in the zoom() function: 我想让它放大和缩小原点,但是在3到10之间。我把这段代码放在zoom()函数中:

public boolean zoom (float amount) {
if(camera.position.len() < 3 && amount > 0)
{
    return  false;
}
if(camera.position.len() > 10 && amount < 0)
{
    return false;
}
if (!alwaysScroll && activateKey != 0 && !activatePressed)
    return false;
Gdx.app.log("zoom", amount + "");
camera.translate(tmpV1.set(camera.direction).scl(amount));
if (scrollTarget)
    target.add(tmpV1);
if (autoUpdate)
    camera.update();
return true;

} }

This works fine if I just call the zoom function (for example a zoom button), but when I pinch zoom, the camera is able to zoom in way past 3, and when zooming out the camera jumps in quite drastically when it gets near to 10. Strangely, the pinch zooming is very smooth if I don't have these bounds. 如果我只是调用缩放功能(例如缩放按钮),这可以正常工作,但是当我捏缩放时,相机可以放大3倍,当缩小时相机会在接近时大幅跳跃10.奇怪的是,如果我没有这些边界,则捏缩放非常平滑。

If I output the amount variable, then pinch zooming gives me quite a range of numbers, something like this when zooming out: 如果我输出金额变量,那么捏缩放给我很多数字,缩小时就像这样:

-3.4762511    
3.425479
-3.386308
3.353984

and this when zooming in: 放大时这个:

3.6231816
-3.553997
3.8673449
-3.81199

When I look at the zoom() function inside the CameraGestureListener, the amount value in that is also varying loads. 当我查看CameraGestureListener中的zoom()函数时,其中的金额值也会变化。 If I look at the pinch() function, then each time it is called the pointer2 position alternates between the position of the first and second finger. 如果我查看pinch()函数,则每次调用它时,指针2位置在第一个和第二个手指的位置之间交替。

Could this be a bug? 这可能是个错误吗? Anyone got any idea what is happening? 任何人都知道发生了什么事? And how I can sort this out? 我怎么能解决这个问题呢?

I figured it out myself! 我自己想通了! Turns out If you just check for the camera position and move it to where you want in the render() function, rather than in the listeners inside the camera controller, then everything works fine. 结果如果您只是检查相机位置并将其移动到render()函数中所需的位置,而不是在相机控制器内的侦听器中,那么一切正常。 I guess I was over complicating it somewhat. 我想我有点过于复杂了。

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

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