本文介绍了如何不让运动物理机构通过静态物体?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的游戏场景包括四个墙壁,这是静态的机构,以及一个平台板,它的类型的运动,只有水平滑动,像下面的图片。

My game scene consist of four walls, which are static bodies, and one platform plate, which is of type kinematic and slides only horizontally, something like the picture below.

基于加速度传感器平台身体动作,看到这个codeS

The platform body moves based on acceleration sensor, see this codes

@Override
public void onAccelerationChanged(AccelerationData pAccelerationData) {
    mPlatformBody.setLinearVelocity(pAccelerationData.getX() * 10, 0);
}

我的问题是当平台熄灭围墙,它不应该。为了解决这个问题,一旦它试图打破界限我设置它的速度为零。看到这个codeS

My problem is when the platform goes off the boundary walls, which it should not. For resolving this issue, I've set its velocity to zero once it tries to break out the boundaries. see this codes

Rectangle rect = new Rectangle(camWidth / 2 - 40, camHeight / 2 - 5,
        80, 10, mEngine.getVertexBufferObjectManager()) {
    @Override
    protected void onManagedUpdate(float pSecondsElapsed) {

        if (this.getX() <= 1) {
            mPlatformBody.setLinearVelocity(0, 0);
        }

        if ((this.getX() + 80 >= camWidth - 1)) {
            mPlatformBody.setLinearVelocity(0, 0);
        }

        super.onManagedUpdate(pSecondsElapsed);
    }
};

随着codeS以上的还是的这个平台可以熄灭屏幕。

With the codes above, still this platform can goes off the screen.

任何人都可以请帮我我该怎么解决这个问题?

Could anyone please help me out how can I overcome this issue?

推荐答案

作为@ LearnCocos2D说,我应该在它试图离开屏幕平台身体恢复到一个法律地位。对于这一点,我应该使用的setTransform 机身类的方法(如@ iforce2d说的)。

As @LearnCocos2D stated, I should reset platform body to a legal position while it tries to leave the screen. For this, I should use setTransform method of Body class (as @iforce2d said).

有关使用的setTransform 处理,有两点很重要。

For dealing with setTransform, there is two important points.


  • AndEngine使用的左上的作为精灵的主播,但Box2D的使用的中心的作为机构的支撑点。

  • Box2D中使用的仪表的为单位,所以我们必须等转换所有的像素的单位计量单位。

  • AndEngine uses top-left as anchor of sprites, but Box2d uses center as anchor of bodies.
  • Box2d uses meter as its units, so we must tranform all pixel units to meter unit.

例如:假设我们想移动身体的体内的到(3,4)点(像素)

Example: Suppose we want to move body body to (3, 4) point (in pixels).

    float x = 3; // in pixels (AndEngine unit)
    float y = 4; // in pixels (AndEngine unit)
    float wD2 = sprite.getWidth()  / 2;
    float hD2 = sprite.getHeight() / 2;
    float angle = body.getAngle(); // preserve original angle

    body.setTransform((x + wD2) / PIXEL_TO_METER_RATIO_DEFAULT,
                      (y + hD2) / PIXEL_TO_METER_RATIO_DEFAULT,
                      angle);

注意 PIXEL_TO_METER_RATIO_DEFAULT 32

这篇关于如何不让运动物理机构通过静态物体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 01:01