[英]dragging in libgdx flickers
我想在LibGDX
Mouse
拖動一個Actor
。 我的代碼:
// overrides in the ClickListener
@Override
public boolean touchDown(InputEvent event, float x, float y,
int pointer, int button) {
touching = true;
prevTouchPosition = new Vector2(x, y);
return super.touchDown(event, x, y, pointer, button);
}
@Override
public void touchDragged(InputEvent event, float x, float y,
int pointer) {
dragging = true;
lastTouchPosition = new Vector2(x, y);
super.touchDragged(event, x, y, pointer);
}
@Override
public void touchUp(InputEvent event, float x, float y,
int pointer, int button) {
touching = false;
super.touchUp(event, x, y, pointer, button);
}
// the update method
@Override
public void act(float delta) {
super.act(delta);
if(dragging){
Vector2 diff = new Vector2(lastTouchPosition).sub(prevTouchPosition);
translate(diff.x, diff.y);
prevTouchPosition = new Vector2(lastTouchPosition);
dragging = false;
}
}
我越是移動Actor
圍繞它得到更壞。 我們的想法是保留最后兩個Mouse
位置並使用它們之間的差異來更新Actor
的位置。
任何像這樣增量的東西都會受到舍入錯誤的影響。 而且,我認為你不需要每幀都更新prevTouchPosition
; 在沒有舍入錯誤的情況下,它不應該改變。 請注意,只有在未旋轉或縮放對象時,您的代碼才有效。
更強大的拖動算法的工作原理如下:
請注意,我們永遠不會在拖動操作期間更新G; 它用於記住物體上被“抓住”的點,我們不斷對齊物體,使此點在當前觸摸下。 在這里,toGlobal(L) - toGlobal(G)為您提供全局坐標中的“delta”向量,我們將其添加到當前位置。
請注意,我假設沒有對象層次結構,因此當前位置本身位於全局坐標中。 否則你也需要考慮父變換。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.