Box2D Forums

It is currently Tue May 21, 2013 1:16 pm

All times are UTC - 8 hours [ DST ]




Post new topic Reply to topic  [ 3 posts ] 
Author Message
 Post subject: Shooting At Angles
PostPosted: Thu May 17, 2012 10:47 am 
Offline

Joined: Thu May 17, 2012 10:41 am
Posts: 2
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:
- (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


Top
 Profile  
 
 Post subject: Re: Shooting At Angles
PostPosted: Sat May 19, 2012 8:42 pm 
Offline

Joined: Tue Jun 24, 2008 8:25 pm
Posts: 1515
Location: Tokyo
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:
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.


Top
 Profile  
 
 Post subject: Re: Shooting At Angles
PostPosted: Sun May 20, 2012 8:47 am 
Offline

Joined: Thu May 17, 2012 10:41 am
Posts: 2
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


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC - 8 hours [ DST ]


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group