简体   繁体   English

在没有重力的情况下将对象移动到iPhone中的box2d中

[英]Move object into screen without gravity into box2d in iPhone

i use following code to move my Box2D object into screen, but because gravity of my world or something else i dont know why my objects is forced to move down, i am new to box2d. 我使用下面的代码将我的Box2D对象移动到屏幕,但由于我的世界或其他东西的重力,我不知道为什么我的对象被迫向下移动,我是box2d的新手。

i want to move my object in entire world without gravity. 我想在没有重力的情况下在整个世界中移动我的物体。

-(void) tick:(NSTimer *)timer {

    int32 velocityIterations = 8;
    int32 positionIterations = 1;
    world->Step(1.0f/60.0f, velocityIterations, positionIterations);

    for (b2Body* b = world->GetBodyList(); b; b = b->GetNext())
    {
        if (b->GetUserData() != NULL)
        {
            UIView *oneView = (UIView *)b->GetUserData();
            CGPoint newCenter = CGPointMake(b->GetPosition().x * PTM_RATIO,self.view.bounds.size.height - b->GetPosition().y * PTM_RATIO);
            oneView.center = newCenter;
            CGAffineTransform transform = CGAffineTransformMakeRotation(- b->GetAngle());
            oneView.transform = transform;
        }
    }
}

my accelerometer code is as follow. 我的加速度计代码如下。

- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration {
    b2Vec2 gravity;
    gravity.Set( acceleration.x * 1.81,  acceleration.y * 1.81 );
    world->SetGravity(gravity);
}

pleas if any one work around. 如果有人解决,请求。

Thanks. 谢谢。

As i understand you want to move your object setting it's position. 据我所知,你想移动你的对象设置它的位置。 It's a bad idea because it will provide non-physical behavior of the bodies colliding with your object. 这是一个坏主意,因为它将提供与您的对象碰撞的物体的非物理行为。 That's because if you will only change the position of your body it's velocity for physics engine will be still zero and collision will be processed according to zero speed of your object. 那是因为如果你只改变你身体的位置,物理引擎的速度仍然是零,并且会根据物体的零速度处理碰撞。

A better solution is to use b2_kinematicBody type for your object. 更好的解决方案是为对象使用b2_kinematicBody类型。 Then you will be able to control it's motion specifying it's velocity vector and physics will behave as expected. 然后你将能够控制它的运动,指定它的速度矢量,物理将按预期运行。 Also the gravity (and no other forces) will not be applied to your object because of it's type. 此外,由于它的类型,重力(并没有其他力)也不会应用于您的对象。

EDIT 编辑

//creation
b2BodyDef bDef;
bDef.type = b2_kinematicBody;
bDef.position.Set(5, 6);

b2Body *body  = physWorld->CreateBody(&bDef);


//control
body->SetLinearVelocity(b2Vec2(3, 4));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM