如何在世界各地制作Box2D包装纸? [英] How do I make a Box2D wrap around world?

查看:83
本文介绍了如何在世界各地制作Box2D包装纸?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想做一个环绕游戏的效果,使物体从x轴的一侧离开屏幕,并在屏幕的另一侧以新的y轴位置重新出现.宽度为250像素,因此基本上可以通过(0,y1),然后重新出现在(300,y2).

I want to make a wrap around game effect where an object would go off the screen from the x-axis one side and reappear in a new y-axis position on the other side of the screen. The width is 250 pixels, so basically it would pass (0, y1) and would reappear at (300, y2).

    a.applyForceToCenter(aMovement, true);
    a.applyTorque(3000, true);

    FixtureDef fDef = new FixtureDef();
    BodyDef ballD = new BodyDef();

    ballD.type = BodyType.DynamicBody;

    //random location for asteroid
    int aLoc = (int) (aLocation * 15);
    float x = 300;
    switch(aLoc)
    {
    case 0:
        ballD.position.set(x, -105);
        break;
    case 1:
        ballD.position.set(x, -95);
        break;
    case 2:
        ballD.position.set(x, -80);
        break;
    case 3:
        ballD.position.set(x, -65);
        break;
    case 4:
        ballD.position.set(x, -50);
        break;
    case 5:
        ballD.position.set(x, -35);
        break;
    case 6:
        ballD.position.set(x, -20);
        break;
    case 7:
        ballD.position.set(x, -5);
        break;
    case 8:
        ballD.position.set(x, 10);
        break;
    case 9:
        ballD.position.set(x, 25);
        break;
    case 10:
        ballD.position.set(x, 40);
        break;
    case 11:
        ballD.position.set(x, 55);
        break;
    case 12:
        ballD.position.set(x, 70);
        break;
    case 13:
        ballD.position.set(x, 85);
        break;
    default:
        ballD.position.set(x, 0);
    }

    PolygonShape asteroid = new PolygonShape();
    asteroid.setAsBox(12.5f, 12.5f);

    //asteroid definition
    fDef.shape = asteroid;
    fDef.density = .5f;
    fDef.friction = .25f;
    fDef.restitution = .75f;

    a = world.createBody(ballD);
    a.createFixture(fDef);
    a.setFixedRotation(false);

   //asteroid image
    aSprite = new Sprite(new Texture("img/asteroid-icon.png"));
    aSprite.setSize(12.5f * 4, 12.5f * 4);
    aSprite.setOrigin(aSprite.getWidth() / 2, aSprite.getHeight() / 2);
    a.setUserData(aSprite);
    asteroid.dispose();

推荐答案

如果球离开屏幕一侧,则可以在球上使用setTransform()方法. 基本上,在每个模拟步骤之后,您都需要检查球是否已在一侧移出,然后使用新的位置和旋转设置setTransform().

You could use the setTransform() method on the ball if it leaves the screen on one side. Basically, after each simulation step you'd check if the ball has been moved out on one side, then setTransform() with the new position and rotation.

这篇关于如何在世界各地制作Box2D包装纸?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆