如何使用鼠标关节的Andr​​oid?鼠标、如何使用、关节、oid

2023-09-04 05:43:32 作者:不负如来不负卿

我想使用MouseJoint在Java中为Android。我是新的Box2D的和cocos2d中。我不知道如何使用鼠标接头。

I want to use the MouseJoint in Java for Android. I am new in box2d and cocos2d. I don't know how to use the mouse joint.

推荐答案

我建议你看到的这个的教程例子。从它复制(对不起,我不喜欢损坏的链接:))... 的

i recomend you to see this tutorial example. Copy from it (sorry i don't like broken links :) ) ...

拖动你的身体与MouseJoint的帮助下,它会碰撞在世界上其他人体的并施加力给他们。

you drag your body with the help of the MouseJoint, it will collide with the other bodys in the world and apply force to them.

的Box2D - 手册 - http://www.box2d.org/manual.html #_Toc258082974

Box2d - Manual - http://www.box2d.org/manual.html#_Toc258082974

8.10鼠标联

鼠标接头被用在测试床来操纵鼠标机构。它试图驱动一个点上的主体朝着光标的当前位置。没有关于旋转没有限制。

The mouse joint is used in the testbed to manipulate bodies with the mouse. It attempts to drive a point on a body towards the current position of the cursor. There is no restriction on rotation.

鼠标接头定义有一个目标点,最大的力,频率和阻尼比。目标点最初与人体的锚点一致。最大的力量来prevent剧烈的反应,当多个动态物体互动。你可以让这个大如你喜欢。的频率和阻尼比用于创建类似于距离关节的弹簧/阻尼器的效果。

The mouse joint definition has a target point, maximum force, frequency, and damping ratio. The target point initially coincides with the body’s anchor point. The maximum force is used to prevent violent reactions when multiple dynamic bodies interact. You can make this as large as you like. The frequency and damping ratio are used to create a spring/damper effect similar to the distance joint.

很多用户都试图去适应鼠标关节的游戏。用户通常要达到precise定位和瞬时响应。鼠标关节不能在这方面很好地工作。你不妨考虑使用运动机构来代替。

Many users have tried to adapt the mouse joint for game play. Users often want to achieve precise positioning and instantaneous response. The mouse joint doesn’t work very well in that context. You may wish to consider using kinematic bodies instead.

让我们开始。

您必须创建PhysicWorld和至少一个身在其中。 (签出PhysicExample如何..)

- MouseJoint法

public MouseJoint createMouseJoint(AnimatedSprite box , float x, float y)
{
    final Body boxBody =
    this.mPhysicsWorld.getPhysicsConnectorManager().findBodyByShape(box);

    Vector2 v = boxBody.getWorldPoint(
                    new Vector2(x/pixelToMeteRatio, y/pixelToMeteRatio)
                    );

    MouseJointDef mjd = new MouseJointDef();
    mjd.bodyA               = groundBody;
    mjd.bodyB               = boxBody;
    mjd.dampingRatio        = 0.2f;
    mjd.frequencyHz         = 30;
    mjd.maxForce            = (float) (200.0f * boxBody.getMass());
    mjd.collideConnected    = true;
    mjd.target.set(v);
    return (MouseJoint) this.mPhysicsWorld.createJoint(mjd);
}

- 触摸车身

我们必须覆盖我们onAreaTouched方法创建关于触摸位置的MouseJoint锚点。

we have to override our onAreaTouched method to create an MouseJoint anchor-point on the touch position.

MouseJoint mjActive = null;
private float pixelToMeteRatio = PhysicsConstants.PIXEL_TO_METER_RATIO_DEFAULT;
@Override
public boolean onAreaTouched(
                final TouchEvent        pSceneTouchEvent,
                final ITouchArea        pTouchArea      ,
                final float             pTouchAreaLocalX,
                final float             pTouchAreaLocalY )
{

        if(pSceneTouchEvent.getAction() == MotionEvent.ACTION_DOWN) {

                this.runOnUpdateThread(new Runnable() {
                        @Override
                        public void run() {

                        final AnimatedSprite face = (AnimatedSprite)pTouchArea; //The touched body
                        //If we have a active MouseJoint, we are just moving arround don't create an 2nd one.
                        if( mjActive == null)
                        {
                                Vector2 vector = new Vector2(pTouchAreaLocalX/pixelToMeteRatio,pTouchAreaLocalY/pixelToMeteRatio);
                                //=====================================
                                // GROUNDBODY - Used for the MouseJoint
                                //=====================================
                                BodyDef groundBodyDef = new BodyDef();
                                groundBodyDef.position.set(vector);
                                groundBody      = mPhysicsWorld.createBody(groundBodyDef);
                                //====================================
                                // CREATE THE MOUSEJOINT
                                //====================================
                                mjActive        = PhysicsJumpExample.this.createMouseJoint(face, pTouchAreaLocalX, pTouchAreaLocalY);
                        }
                }});

                return true;
        }

return false;
}

- 移动身体

我们正朝着我们的手指在现场,所以我们要移动MouseJoint了。 如果我们松开手指。我们必须销毁MouseJoint ..

We are moving our finger over the scene, so we have to move the MouseJoint too. If we release the finger.. we must destroy the MouseJoint..

@Override
public boolean onSceneTouchEvent(final Scene pScene, final TouchEvent pSceneTouchEvent) {

        if(this.mPhysicsWorld != null) {


        if(pSceneTouchEvent.getAction() == MotionEvent.ACTION_MOVE) {

                this.runOnUpdateThread(new Runnable() {
                        @Override
                        public void run() {

                        if( mjActive != null ){ //If the MJ is active move it ..

                                // =========================================
                                // MOVE THE MOUSEJOINT WITH THE FINGER..
                                // =========================================
                                Vecotr2 vec = new Vector2(pSceneTouchEvent.getX()/pixelToMeteRatio, pSceneTouchEvent.getY()/pixelToMeteRatio);
                                mjActive.setTarget(vec);

                        }
                }});
                return true;
        }

        //===========================================
        // RELEASE THE FINGER FROM THE SCENE..
        //===========================================
        if(     pSceneTouchEvent.getAction() == MotionEvent.ACTION_UP           ||
                        pSceneTouchEvent.getAction() == MotionEvent.ACTION_CANCEL
          ) {

                this.runOnUpdateThread(new Runnable() {
                        @Override
                        public void run() {


                        if( mjActive != null )
                        {
                                //======================================
                                // DESTROY OUR MOUSEJOINT
                                //======================================
                                PhysicsJumpExample.this.mPhysicsWorld.destroyJoint(mjActive);
                                PhysicsJumpExample.this.mPhysicsWorld.destroyBody(groundBody);
                                mjActive = null;
                        }

                }});

                return true;
        }

        return false;
}

供参考: 为了满足您的需求,您有这个设置玩(在createMouseJoint方法)

FYI: To fit your needs, you have to play with this settings ( in the createMouseJoint method )

mjd.dampingRatio = 0.2f;

mjd.frequencyHz = 30;

mjd.maxForce = (float) (200.0f * boxBody.getMass());