Question

Essentially I'm building an app with "tap" controls to control a character in a side scrolling view, the user taps where they want to move the character then the character should move to that x location. I'm just thinking about the best way of implementing this, I thought I could apply a linear velocity to the character and then repeatedly check to see if it has reached its target (lets say every 0.05 seconds) in which case its velocity can be set to 0 but that doesn't seem particularly elegant. Any suggestions?

Thanks in advance :)

Was it helpful?

Solution

Try Using Linear Impulse which will move the body.. to the desired location. :)

OTHER TIPS

You could easily animate a view position. Just do something like

CGPoint destination = ... ;
[UIView animateWithDuration:.3 animations:^{
  view.center = destination;
}];

You will have to capture the touch in the parent view. Also, the coordinates for destination should be in the view's parent coordinates.

For changing b2Body position you can use SetTransform method. It allows you to set new position in b2World as b2Vec and body angle.

But if you need to check collisions, you can be faced with trouble. If you move body using forces/velocities/impulses, it will prevent situation when, for exmple, two objects, that should collide, can appear at the same location. In case of SetTransform, results of collision of such objects can be wrong.

For example, if you bordered your game area with few edges, in case of SetTransform, when your game object will be placed over one of your edges, it can be pushed out of your game area.

I'm working on something similar at the moment. I wanted a constant running rate for my guy so I implemented something using the SetLinearVelocity(b2Vec2) command a whole bunch. I'm not sure whether it's "proper" in Box2D to be calling SetLinearVelocity for every update:(ccTime)dt that runs while you're moving - its sort of like circumventing the physics engine which seems silly - but its working out alright so far for me.

The effect of the update: routine below is such that the player body does not abruptly stop moving. It has a sweet little slide to it as it stops, if you tweak your player body and environment well.

My touch callback -(BOOL) ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event calls the setGoalX:(float)x function of my Player object below:

//somewhere initially, I set up linear damping for some extra drag in my player class
//note: GROUND_DAMPING is defined earlier in my file, and is not a Box2d thing.
self.physicsBody->SetLinearDamping(GROUND_DAMPING);


-(void) setGoalX:(float)x
{
    goal_->x = x; //b2Vec2* goal
}

-(void) update:(ccTime)dt
{
    int dx = (goal_->x - body->GetPosition().x)*10;
    if (dx != 0) {
        xdir = (dx > 0) ? 1 : -1;
        if (abs(dx) >= 10) 
            body->SetLinearVelocity(b2Vec2(RUN_SPEED*xdir,body->GetLinearVelocity().y));
    } else xdir = 0;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top