gpt4 book ai didi

java - 通过 AndEngine 触摸旋转

转载 作者:太空狗 更新时间:2023-10-29 12:51:22 25 4
gpt4 key购买 nike

我有一个名为“gun”的 Sprite,我想通过用户触摸来旋转它。
我的问题:当我触摸并向下移动时它会旋转并且运行良好,但是当我触摸并向上移动时它不会旋转。
这是我的代码:

package com.example.samplegameone;

import java.io.IOException;
import java.io.InputStream;

import org.andengine.engine.camera.Camera;
import org.andengine.engine.options.EngineOptions;
import org.andengine.engine.options.ScreenOrientation;
import org.andengine.engine.options.resolutionpolicy.RatioResolutionPolicy;
import org.andengine.entity.scene.Scene;
import org.andengine.entity.scene.background.Background;
import org.andengine.entity.sprite.Sprite;
import org.andengine.entity.util.FPSLogger;
import org.andengine.input.touch.TouchEvent;
import org.andengine.opengl.texture.ITexture;
import org.andengine.opengl.texture.bitmap.BitmapTexture;
import org.andengine.opengl.texture.region.ITextureRegion;
import org.andengine.opengl.texture.region.TextureRegionFactory;
import org.andengine.ui.activity.SimpleBaseGameActivity;
import org.andengine.util.adt.io.in.IInputStreamOpener;
import org.andengine.util.debug.Debug;

import android.util.Log;
import android.widget.Toast;

public class SampleGameOne extends SimpleBaseGameActivity {

private static final int CAMERA_WIDTH = 480;
private static final int CAMERA_HEIGHT = 320;

private ITexture mTexture;
private ITextureRegion mFaceTextureRegion;
Sprite gun = null;

@Override
public EngineOptions onCreateEngineOptions() {
final Camera camera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);

return new EngineOptions(true, ScreenOrientation.LANDSCAPE_SENSOR,
new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), camera);
}

@Override
public void onCreateResources() {
try {
this.mTexture = new BitmapTexture(this.getTextureManager(),
new IInputStreamOpener() {
@Override
public InputStream open() throws IOException {
return getAssets().open("gfx/gun.png");
}
});

this.mTexture.load();
this.mFaceTextureRegion = TextureRegionFactory
.extractFromTexture(this.mTexture);
} catch (IOException e) {
Debug.e(e);
}
}

@Override
public Scene onCreateScene() {
this.mEngine.registerUpdateHandler(new FPSLogger());

final Scene scene = new Scene();
scene.setBackground(new Background(0.09804f, 0.6274f, 0.8784f));

final float centerX = (CAMERA_WIDTH - this.mFaceTextureRegion
.getWidth()) / 2;
final float centerY = (CAMERA_HEIGHT - this.mFaceTextureRegion
.getHeight()) / 2;

gun = new Sprite(centerX, centerY, this.mFaceTextureRegion,
this.getVertexBufferObjectManager()) {

@Override
public boolean onAreaTouched(TouchEvent pSceneTouchEvent,
float pTouchAreaLocalX, float pTouchAreaLocalY) {
switch (pSceneTouchEvent.getAction()) {
case TouchEvent.ACTION_DOWN:

break;
case TouchEvent.ACTION_MOVE:
float pValueX = pSceneTouchEvent.getX();
float pValueY = CAMERA_HEIGHT - pSceneTouchEvent.getY();

float directionX = pValueX - gun.getX();
float directionY = (CAMERA_HEIGHT - pValueY) - gun.getY();

float rotationAngle = (float) Math.atan2(directionY,
directionX);
gun.setRotationCenter(0, gun.getHeight() / 2);
gun.setRotation(org.andengine.util.math.MathUtils
.radToDeg(rotationAngle));


break;
case TouchEvent.ACTION_UP:
break;

default:
break;
}
return super.onAreaTouched(pSceneTouchEvent, pTouchAreaLocalX,
pTouchAreaLocalY);
}

};
scene.registerTouchArea(gun);
scene.setTouchAreaBindingOnActionDownEnabled(true);
scene.attachChild(gun);

return scene;
}
}

我认为这部分计算旋转角度的问题

                float pValueX = pSceneTouchEvent.getX();
float pValueY = CAMERA_HEIGHT - pSceneTouchEvent.getY();

float directionX = pValueX - gun.getX();
float directionY = (CAMERA_HEIGHT - pValueY) - gun.getY();

float rotationAngle = (float) Math.atan2(directionY,
directionX);
gun.setRotationCenter(0, gun.getHeight() / 2);
gun.setRotation(org.andengine.util.math.MathUtils
.radToDeg(rotationAngle));

非常感谢

最佳答案

有两件事你应该解决:

  • 实际上是 directionY是(让我们回顾一下你的公式):

    float directionY = pSceneTouchEvent.getY() - gun.getY();
  • 你的 directionXdirectionY与你的枪的 [top,left] 相比,触摸位置的移位元素是什么?世界位置 => 所以你的计算角度 实际上是围绕这把枪的 [top,left] 的旋转位置。因此,事情应该是:

    gun.setRotationCenter(0, 0);

其中 (0,0) 是局部 [top, left] 相对位置,用于设置枪中旋转的 anchor 。

注意:

您应该了解更多关于 AndEngine (Box2D) 中的物理知识。要想为所欲为,通常情况下,您应该这样做:

  • 创建一个 fix anchor body (可能是一个圆体)在你想要你的 gun 的地方旋转。
  • 创建一个 RevoluteJoint 用这个 anchor 固定你的枪。
  • 创建并处置 Mouse Joint 每次您触摸您的枪并释放您的触摸时,您的枪和鼠标点之间。

Physics\Using a RevoluteJoint + MouseJoint中可以看到AndEngine Examples的例子.

这将帮助您创建真实世界中发生的正常交互,而不管您的 body 对象的位置、旋转或速度如何计算(因为物理引擎会为您完成这些)。

此外,您可以在游戏中的任何对象上应用相同的机制(通过将上述过程封装在函数或类中)。

关于java - 通过 AndEngine 触摸旋转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12002013/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com