1
votes

I have 3 NPC's that each have their own circular physicsBody meant to mimic bouncy balls in zero gravity - more precisely, bouncy balls that never stop moving due to restitution = 1.0; (bounciness 100%) friction = 0.0; (No friction) linearDampening = 0.0; (Collisions don't impact the NPC). If either of these NPC's collide/contact with another one, then their velocity increases. They start moving so fast that it begins to force the levels boundaries/physicsBodies (which are supposedly non-dynamic) actually shift/move from the impact. Below are the if statements I wrote in the update method to keep these 3 NPC's physicsBody.velocities under control. Could someone take a look and tell me if there's a better way of doing this? I basically want to have limited maximum velocities/moving speeds for all 3 NPC's at all times that they can't exceed.

-(void)update:(CFTimeInterval)currentTime
{
    /* Called before each frame is rendered */


    //Track ball velocities.
    if (_npcRed.physicsBody.velocity.dx > 1000 || _npcRed.physicsBody.velocity.dy > 1000)
    {
        NSLog(@"RED's moving too fast; lowering velocity");
        _npcRed.physicsBody.velocity = CGVectorMake(500, 500);
    }
    else
    {
        NSLog(@"RED's at stable speed");
    }
    if (_npcBlue.physicsBody.velocity.dx > 1000 || _npcBlue.physicsBody.velocity.dy > 1000)
    {
        NSLog(@"BLUE's moving too fast; lowering velocity");
        _npcBlue.physicsBody.velocity = CGVectorMake(500, 500);
    }
    else
    {
        NSLog(@"BLUE's at stable speed");
    }
    if (_npcGreen.physicsBody.velocity.dx > 1000 || _npcGreen.physicsBody.velocity.dy > 1000)
    {
        NSLog(@"GREEN's moving too fast; lowering velocity");
        _npcGreen.physicsBody.velocity = CGVectorMake(500, 500);
    }
    else
    {
        NSLog(@"GREEN's at stable speed");
    }
}
1

1 Answers

1
votes

What you are doing works fine except for two things. Your if statement

if (_npcBlue.physicsBody.velocity.dx > 1000 || _npcBlue.physicsBody.velocity.dy > 1000)
    _npcBlue.physicsBody.velocity = CGVectorMake(500, 500);

is setup to fire if either the dx or dy velocity is greater than 1000. What if npcBlue has a dx velocity of 100 and a dy velocity of 1010. npcBlue's dx velocity jumps from 100 to 500. Not so good.

Second, why throttle at 1000 for dx and dy and then set to 500? That would make movement very jerky. Why not check if dx or dy is greater than 500 and if yes, set to 500?