Shooting At Angles

Hi All,

How difficult are the registration questions. never seen anything like it 🙂

anyways

Please could somebody help me with the following issue.

I control my sprite in box2d with the sneaky joystick and rotate the sprite accordingly.

However when i shoot lasers/bullets from the sprite i find that sometime the bullets go off direction .. i.e. not straight forward.

this only happens when I'm rotating the sprite and shooting at the same time. its like I'm waiting for box2d to catch up. after x amount of time it sorts itself out.

please see below the code i am using to rotate the sprite and shoot

Code: Select all

- (void)movePlayer {
    //move the player with joypad
    CGPoint velocity = ccpMult(_hud->leftJoystick.velocity, 5);
    if (velocity.x != 0)
    {
        b2Vec2 force = b2Vec2(0,0);
        force = b2Vec2(velocity.x,velocity.y);
        playerBody->SetLinearVelocity(force);
        float bodyAngle = playerBody->GetAngle();
        b2Vec2 toTarget = playerBody->GetPosition();
        float desiredAngle = atan2f( -velocity.x, velocity.y );
        float totalRotation = desiredAngle - bodyAngle;
        while ( totalRotation < -180 * DEGTORAD ) totalRotation += 360 * DEGTORAD;
        while ( totalRotation >  180 * DEGTORAD ) totalRotation -= 360 * DEGTORAD;
        playerBody->SetTransform( playerBody->GetPosition(), desiredAngle );
    }
    if (velocity.x == 0)
    {
        playerBody->SetAwake(false) ;
       
    }
}

- (void)LaserGun:(ccTime)dt {
    for (b2Body* b = world->GetBodyList(); b; b = b->GetNext())
   {
    NSArray *players = [lh spritesWithTag:PLAYERSHIP];
    for (playerShip in players) {
       
        playerBody = [playerShip body];
        b2Vec2 eyeOffset = b2Vec2(0, 0.5);
        b2Vec2 eye = playerBody->GetWorldPoint(eyeOffset);
        b2Vec2 target = eye - playerBody->GetWorldCenter();
        target.Normalize();
        target *= 20.0;
        target = eye + target;
       
       
        playerData * playerData = [playerShip customValueWithKey:@"data"];           
        playerData.eye = ccp(eye.x * [LevelHelperLoader pixelsToMeterRatio], eye.y * [LevelHelperLoader pixelsToMeterRatio]);
        playerData.target = ccp(target.x * [LevelHelperLoader pixelsToMeterRatio], target.y * [LevelHelperLoader pixelsToMeterRatio]);
        playerData.canSeePlayer = NO;
       
   
        RaysCastCallback callback;
        world->RayCast(&callback, eye, target);
       
        if (callback.m_fixture)
        {
            playerData.target = ccp(callback.m_point.x * [LevelHelperLoader pixelsToMeterRatio],
                                    callback.m_point.y * [LevelHelperLoader pixelsToMeterRatio]);
           if (callback.m_fixture->GetBody() == monster1body)
           { playerData.canSeePlayer = TRUE;}
        }
        if(_hud->jumpButton.active)
        {
       
            if (CACurrentMediaTime() - playerData.lastShot > 0.1)
            {
            playerData.lastShot = CACurrentMediaTime();
           
            // Create and position laser
            b2Body *laserBody = [lh newBodyWithUniqueName:@"LaserBeam" world:world];
            laserSprite = (LHSprite *)laserBody->GetUserData();
            laserSprite.position = playerData.eye;
            laserSprite.rotation = playerShip.rotation;
            laserBody->SetTransform(b2Vec2(laserSprite.position.x/[LevelHelperLoader pixelsToMeterRatio],
                                           laserSprite.position.y/[LevelHelperLoader pixelsToMeterRatio]),
                                    CC_DEGREES_TO_RADIANS(-laserSprite.rotation));               
            // Make laser move
               
            b2Vec2 laserVel = callback.m_point - eye;
            laserVel.Normalize();
            laserVel *= 20.0;
            laserBody->SetLinearVelocity(laserVel);
                 
               
           
            if(!world->IsLocked())
            {
                b2Body* laserBody = [laserSprite body];
               
                if(laserBody)
                {
                    laserBody->SetLinearVelocity(laserVel);
             
                }
             
}}}}}}


thanks

Thomas
There is a lot going on in that code, and a lot of information extraneous to Box2D (eg. being stored in user data) so it's a bit hard to jump in and follow it well. One thing I am wondering about is the need for this loop:

Code: Select all

for (b2Body* b = world->GetBodyList(); b; b = b->GetNext())
... what purpose does that accomplish. I may have missed it but the variable b does not seem to be used anywhere, and as the number of bodies in the world grows it could cause a major slowdown. I would not be surprised if it is also the cause of the problem you are asking about.
Thanks for the response irrisistible forc.

I removed the reference you quoted as indeed it wasn't used. I musta left it in the code after trying something.

Still have the same problem.

The player data reference is used for the raycast

Original thread: https://box2d.org/forum/viewtopic.php?t=8497