如何抓住b2Body并在屏幕上移动? (cocos2d,box2d,iphone) [英] How to grab a b2Body and move it around the screen? (cocos2d,box2d,iphone)

查看:269
本文介绍了如何抓住b2Body并在屏幕上移动? (cocos2d,box2d,iphone)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想移动屏幕周围屏幕上触摸的任何b2body。我听说过有关鼠标关节的事。

I want to move any b2body that is touched on the screen around the screen. I've heard something about mouse joints..

我发现: http://iphonedev.net/2009/08/05/how-to-grab-a-sprite-with-cocos2d -and-box2d /

但我只是给了我很多错误,如果我只是复制ccTouch方法到一个新的项目(当然,标题太)。例如。 world->查询< - 没有成员

but I just gives me a lot of errors if i just copy the ccTouch Methods into a new project (of course the variables in the header too). E.g. world->Query <- NO MEMBER FOUND

有人可以做一个tut /一个新项目,并在这里上传。或者有更好的方法吗?

May someone make a tut/a new project and upload it here. Or is there a better way?

推荐答案

首先你必须创建b2QueryCallback子类:

First you have to create b2QueryCallback subclass:

class QueryCallback : public b2QueryCallback
{
public:
    QueryCallback(const b2Vec2& point)
    {
        m_point = point;
        m_object = nil;
    }

    bool ReportFixture(b2Fixture* fixture)
    {
        if (fixture->IsSensor()) return true; //ignore sensors

        bool inside = fixture->TestPoint(m_point);
        if (inside)
        {
             // We are done, terminate the query.
             m_object = fixture->GetBody();
                 return false;
        }

        // Continue the query.
        return true;
    }

    b2Vec2  m_point;
    b2Body* m_object;
};

然后在touchBegan方法中:

Then in your touchBegan method:

    b2Vec2 pos = yourTouchPos;
// Make a small box.
b2AABB aabb;
b2Vec2 d;
d.Set(0.001f, 0.001f);
aabb.lowerBound = pos - d;
aabb.upperBound = pos + d;

// Query the world for overlapping shapes.
QueryCallback callback(pos);
world_->QueryAABB(&callback, aabb);         

b2Body *body = callback.m_object;
if (body)
    {
        //pick the body
    }


b $ b

有两种方式我看到你可以控制选择的身体。第一个,你注意到 - 创建一个鼠标点,第二个是使你的身体运动学和控制它的速度(不是位置! - 当碰撞时,它将提供非物理行为,因为速度将为零)。在第一种情况下,如果你将非常快地移动你的对象,会有一些延迟,当移动。我没有尝试自己的第二种方式,因为在这种情况下,身体不会与其他运动和静态身体碰撞。

There are two ways I see you can control the picked body. The first one, as you notices - to create a mouseJoint and the second is to make your body kinematic and control it's velocity (not position! - it will provide non-physical behavior when collide because the speed will be zero). In first case if you will move your objects very fast there will be some delay when moving. I did not try the second way myself because in this case the body will not collide with other kinematic and static bodies.

也可以在移动时锁定身体的旋转。

Also you may want to lock body's rotation when moving.

这篇关于如何抓住b2Body并在屏幕上移动? (cocos2d,box2d,iphone)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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