Moving an Object before physic

View previous topic View next topic Go down

Moving an Object before physic

Post  jj on Tue Sep 30, 2008 5:29 am

I'd like to take the handle I have on an object:

(in templateLoading)
_ball1 = (SIO2object *)sio2ResourceGet(sio2->_SIO2resource,
SIO2_OBJECT,
"object/Ball");

And (without setting physicPlay just yet) MOVE the object in space (x, y, and slightly z) using the accelerometer.

(added this for sure in template.mm):
void templateScreenAccelerometer( float _x, float _y, float _z )
{
accelerometer.x = _x;
accelerometer.y = _y;
accelerometer.z = _z;
}

I thought I might try to change the pos->x, pos->y somewhere in the templateRender loop with something like:

if (!ballLaunched) { //ball has not been launched, no physicPlay yet
_ball1->pos->x = _ball1->pos->x + accelerometer.x;
_ball1->pos->y = _ball1->pos->y + accelerometer.y;

sio2ObjectRender(_ball1, sio2->_SIO2camera, 1);
}

but I know this is wrong. Is there an easy way. I've begun to look into the accelerometer gravity vectors...As I would like to "launch" this ball with a detected "flick forward".

BTW I am attempting to do this is portrait mode...
Thanks in advance if anyone has pointers for a relative n00bie Wink

jj

Posts : 77
Join date : 2008-09-24

View user profile

Back to top Go down

Re: Moving an Object before physic

Post  sio2interactive on Tue Sep 30, 2008 5:38 am

Well you're pretty close after modifying any transformation on an object (position, rotation or scale) you should call sio2ObjectGetMatrix to recalculate the modelview matrix of this object. SIO2 is not using the typical glTranslate, glRotate & glScale on every frame, in order to gain some speed I calculate the matrix once and use it as long as the transformation didn't change.

I think thats what is missing in your code.

ho! and by the way no need to call sio2ObjectRender replace it with sio2ObjectGetMatrix and draw using sio2ResourceRender (all the dirty work is done in there Wink)
avatar
sio2interactive

Posts : 1526
Join date : 2008-08-26
Age : 38
Location : Shanghai

View user profile http://sio2interactive.com

Back to top Go down

Re: Moving an Object before physic

Post  jj on Tue Sep 30, 2008 6:00 am

Thanks!

The getMatrix is working for updating, but this code in the templateRender is not behaving quite like I need. Perhaps I need an Accelerometer guru. The sizes of the moves I'm sure are all out of whack, and I still don't quite have a grasp on the vector math and values being fed in by the accelerometer.

If I wanted to have a "hand on" the ball and move it around with the gravity vectors being given by the Accelerometer dispatch, is there some other manipulations I'm missing?

Thanks again!

Edit: I guess it would involve not just the reading from the _x, _y, _z, but the movement between timestamps? acceleration vector? I'm a little confused because how do some programs not just get the acceleration in relation to gravity, but the position of the object... hmm, guess I need to brush up on vector math

jj

Posts : 77
Join date : 2008-09-24

View user profile

Back to top Go down

Re: Moving an Object before physic

Post  sio2interactive on Tue Sep 30, 2008 6:08 am

Perhaps I need an Accelerometer guru. The sizes of the moves I'm sure are all out of whack, and I still don't quite have a grasp on the vector math and values being fed in by the accelerometer.

>> Nah no need Wink It's fairly easy, what I suggest you to do his to take tutorial05 and put on your iPhone and observe the value that are rendered at the bottom of the screen. You'll see how that values are handled when you move your phone.
avatar
sio2interactive

Posts : 1526
Join date : 2008-08-26
Age : 38
Location : Shanghai

View user profile http://sio2interactive.com

Back to top Go down

Re: Moving an Object before physic

Post  sio2interactive on Tue Sep 30, 2008 6:14 am

Edit: I guess it would involve not just the reading from the _x, _y, _z, but the movement between timestamps? acceleration vector? I'm a little confused because how do some programs not just get the acceleration in relation to gravity, but the position of the object... hmm, guess I need to brush up on vector math

>>Just simple take the XYZ value from the accelerometer and put a factor on them that represent the units in your world. Like positive X go forward -X go backward and use the direction vector of your camera to apply it in the world. Like I do on the video of the home page with the player there's my code:

Code:


...
...
...
         // RotZ
         if( ROTZ_DIR > 0 )
         {
            SMOOTH_Z -= 1.0f * sio2->_SIO2window->d_time;
            ROTZ += SMOOTH_Z;
         }
         else if( ROTZ_DIR < 0 )
         {
            SMOOTH_Z -= 1.0f * sio2->_SIO2window->d_time;
            ROTZ -= SMOOTH_Z;
         }
         
         if( SMOOTH_Z < 0.0f )
         {
            SMOOTH_Z = 0.0f;
            ROTZ_DIR = 0;
         }

            
         // Camera Collision   
         {
            vec3 tmp_pos;
            sio2Orbit( &tmp_pos, object->pos, 10.0f, ROTZ, 15.0f );

            
            // Set Angle
            object->rot->z = ROTZ;
            
            if( sio2->_SIO2window->last_sync )
            {
               sio2ObjectGetMatrix( object );

               btMatrix3x3 mat3;
               mat3.setFromOpenGLSubMatrix( (btScalar *)object->mat );
               object->_btRigidBody->getWorldTransform().setBasis( mat3 );
            }


            // Position Ray
            btVector3 from( object->pos->x,
                        object->pos->y,
                        object->pos->z + object->dim->z + 0.1f );

            btVector3 to( tmp_pos.x, tmp_pos.y, tmp_pos.z );

            btCollisionWorld::ClosestRayResultCallback ray( from, to );
            
            sio2->_SIO2physic->_btDiscreteDynamicsWorld->getCollisionWorld()->rayTest( from, to, ray );


            if( ray.hasHit() )
            {
               camera->pos->x = ray.m_hitPointWorld.x();
               camera->pos->y = ray.m_hitPointWorld.y();
               camera->pos->z = ray.m_hitPointWorld.z();               
            }
            else
            {
               camera->pos->x = tmp_pos.x;
               camera->pos->y = tmp_pos.y;
               camera->pos->z = tmp_pos.z;
            }
            


            // Update Target
            camera->tar->x = object->pos->x;
            camera->tar->y = object->pos->y;
            camera->tar->z = object->pos->z + object->dim->z + 0.1f;
            
            sio2CameraUpdateDir( camera );
            
            
            
            if( MOV_DIR )
            {
               object->_btRigidBody->setActivationState( ACTIVE_TAG );
            
               sio2Normalize( camera->dir, camera->dir );
               
               object->_btRigidBody->setLinearVelocity( btVector3( ( MOV_DIR * camera->dir->x ) * SPEED,
                                     ( MOV_DIR * camera->dir->y ) * SPEED,
                                     object->_btRigidBody->getLinearVelocity()[2] ) );
               //camera->speed = 15.0f;
               //sio2CameraMove( camera, sio2->_SIO2window, MOV_DIR );
            }            
         }
...
...
...


void kmHomeScreenAccelerometer( float _x, float _y, float _z )
{
   if( _y > 0.1f || _y < -0.1f )
   {
      float tmp = _y * SENSITIVITY;
      
      if( _y > 0.0 )
      { ROTZ_DIR = -1; }
      else if( _y < 0.0 )
      { ROTZ_DIR = 1; }
      
      if( abs( tmp ) > abs( SMOOTH_Z ) )
      { SMOOTH_Z = abs( tmp ) * SMOOTH_FACTOR; }

      ROTZ -= tmp;
   }
}


avatar
sio2interactive

Posts : 1526
Join date : 2008-08-26
Age : 38
Location : Shanghai

View user profile http://sio2interactive.com

Back to top Go down

Re: Moving an Object before physic

Post  jj on Tue Sep 30, 2008 6:23 am

Ok, good suggestion. Looking at 05 I see that they stay between -1 and 1 (+x to the right, +y down, +z toward chest).

I guess there is no way to measure the distance the iphone moves laterally across your body from right to left and up and down. Only the relative rotation of the device? ...stupid question I know....

jj

Posts : 77
Join date : 2008-09-24

View user profile

Back to top Go down

Re: Moving an Object before physic

Post  sio2interactive on Tue Sep 30, 2008 6:40 am

I guess there is no way to measure the distance the iphone moves laterally across your body from right to left and up and down. Only the relative rotation of the device? ...stupid question I know....

>> Not really but just a normalized vector is already good enough to do practically whatever you want to do in 3D space Wink
avatar
sio2interactive

Posts : 1526
Join date : 2008-08-26
Age : 38
Location : Shanghai

View user profile http://sio2interactive.com

Back to top Go down

Re: Moving an Object before physic

Post  jj on Sat Oct 25, 2008 2:59 am

I have the following code for my templateRender loop. I am attempting to view the object _ball1 in a different (starting position, BEFORE the ball is launched, based on the (int) playerTurn variable.

My NSLog records the change in _ball1->pos->x from 22.0 to -22.0, but the ball does NOT render in the view to match.

Is this code correct? or am I missing something still

Code:

http://template.mm

void templateRender( void )
{
   vec2 pos;   
   
   glMatrixMode( GL_MODELVIEW );
   glLoadIdentity();
   
   if ((ballInFlight == 0) && (playerTurn == 1)) {
      _ball1->pos->x = 22.0f;
      _ball1->pos->y = 0.0f;
      _ball1->pos->z = 6.0f;
      NSLog(@"should switch ball matrix pos!1: p1: x: %f", _ball1->pos->x);
      
      if( sio2->_SIO2window->last_sync )
      {
         sio2ObjectGetMatrix( _ball1 );
         
         btMatrix3x3 mat3;
         mat3.setFromOpenGLSubMatrix( (btScalar *)_ball1->mat );
         _ball1->_btRigidBody->getWorldTransform().setBasis( mat3 );
         
         NSLog(@"should switch ball matrix pos!2: p1");
      }
      
   }
   if ((ballInFlight == 0) && (playerTurn == 2)) {
      _ball1->pos->x = -22.0f;
      _ball1->pos->y = 0.0f;
      _ball1->pos->z = 6.0f;
      NSLog(@"should switch ball matrix pos!1: p2: x: %f", _ball1->pos->x);
      
      if( sio2->_SIO2window->last_sync )
      {
         sio2ObjectGetMatrix( _ball1 );
         
         btMatrix3x3 mat3;
         mat3.setFromOpenGLSubMatrix( (btScalar *)_ball1->mat );
         _ball1->_btRigidBody->getWorldTransform().setBasis( mat3 );
         
         NSLog(@"should switch ball matrix pos!2: p2");
      }
   }

   //for the collision sensor detecting
   sio2ResourceEvaluateSensors(sio2->_SIO2resource);
   
   { //Touch screen camera moves not needed soon.
      if(ROT_Z) {
         sio2CameraRotateZ(sio2->_SIO2camera, ROT_Z);
         sio2CameraUpdateDir(sio2->_SIO2camera);
      }
      if (MOV_DIR) {
         sio2CameraUpdateDir(sio2->_SIO2camera);
         
         sio2CameraMove(sio2->_SIO2camera,
                    sio2->_SIO2window,
                    MOV_DIR);
      }
   }
   
   sio2->_SIO2window->fps = 6;
   
   glClear( GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT );

   if( sio2->_SIO2camera )
   {
      {
         sio2CameraRender( sio2->_SIO2camera );
         
         sio2CameraUpdateFrustum( sio2->_SIO2camera );
         
         sio2ResourceCull( sio2->_SIO2resource,
                      sio2->_SIO2camera );
         
         sio2PhysicRender( sio2->_SIO2physic,
                      sio2->_SIO2window );
         
         sio2LampEnableLight();
         {
            sio2ResourceRender( sio2->_SIO2resource,
                          sio2->_SIO2window,
                          sio2->_SIO2camera );
         }
         // Turn OFF the lights.
         sio2LampResetLight();
      }
      
      
   }
   
   // Enter in OpenGL orthographic mode ( in 2D for the rest of us )
   sio2WindowEnter2D( sio2->_SIO2window );
   {
      // Adjust the position where the font will start to be
      // drawed on the screen.
      pos.y = 250.0f;
      
      // Change the color of the font by accessing the _SIO2material
      // property of the current font.
      _SIO2font_default->_SIO2material->diffuse->x = 0.0f;
      _SIO2font_default->_SIO2material->diffuse->y = 1.0f;
      _SIO2font_default->_SIO2material->diffuse->z = 0.0f;
      _SIO2font_default->_SIO2material->diffuse->w = 1.0f;         
      
      sio2FontPrint( _SIO2font_default, &pos, "Accelerometer X:%.2f Y:%.2f Z:%.2f", accelerometer.x,
                 accelerometer.y,
                 accelerometer.z );

      if (playerTurn != assignedPlayerNumber) {
         pos.y = 230.0f;
         sio2FontPrint( _SIO2font_default, &pos, "Waiting On Opponent");
      }
      
      
      sio2FontReset();
      // Then leave the 2D mode, the order is important, as the matrix stacks
      // needs to be pushed and poped in a specific way.
      sio2WindowLeave2D();
   }      

}

jj

Posts : 77
Join date : 2008-09-24

View user profile

Back to top Go down

Re: Moving an Object before physic

Post  sio2interactive on Sat Oct 25, 2008 5:50 pm

The problem comes because the physic is ON. As long as the physic is ON Bullet will handle all the transformation, if you are using physic you need to use the Bullet specific API for your transformation based on the _btRigidBody handle. SIO2 will then extract the matrix from the physic simulation and use it for rendering.

The function: sio2ObjectGetMatrix is useless if the physic is ON...
avatar
sio2interactive

Posts : 1526
Join date : 2008-08-26
Age : 38
Location : Shanghai

View user profile http://sio2interactive.com

Back to top Go down

Re: Moving an Object before physic

Post  jj on Sat Oct 25, 2008 6:59 pm

thx: checking this out now

EDIT: odd thing. this seems to work on the render (after PhysicReset/PhysicPause) in the simulator, but the doesn't get rendered on the iphone...

In other words: It appears that the transformation is taking place. On the simulator the _ball1 physic object is rendered with the transform correctly, however on the iphone device, the _ball1 is not displayed after the transform call is made.

However, when the physic is PLAY again on the device, the _ball1 suddenly appears in the (transformed) position and physics play according to the new starting transform correctly...

On the iphone device, is there a line of code still missing? Is something needed inside the render loop? Or is it a bug in the device capabilities?

does that make sense? Let me know if the code looks correct below including the bullet API calls...

Here is the code I am trying to run when the physics world reports a "MISS":

Code:

http://template.mm

void templateThrowMissed ( void ) {
   ballInFlight = 0;
   bounceCount = 0;
   
        //stops the animation on the "MISS"
   sio2PhysicReset(sio2->_SIO2physic);
   sio2PhysicPause(sio2->_SIO2physic);

   //change the transformation on _ball1 before changing the player in control
   btMotionState* motion = _ball1->_btRigidBody->getMotionState();
   btTransform world;
   
    if (playerTurn == 1) {
       world.setOrigin(btVector3(22.0, 0.0, 6.0));
    }
   
   if (playerTurn == 2) {
      world.setOrigin(btVector3(-22.0, 0.0, 6.0));
   }

   motion->setWorldTransform(world);
      _ball1->_btRigidBody->setMotionState(motion);

   templateTurnChange(playerTurn);
   
   templateAppDelegate *delegate = [[UIApplication sharedApplication] delegate];
   [delegate iMissedATarget];

}

jj

Posts : 77
Join date : 2008-09-24

View user profile

Back to top Go down

Re: Moving an Object before physic

Post  sctm81 on Tue May 19, 2009 2:30 am

what happened to the sio2ObjectGetMatrix command ? it doesn't seem available in the latest version. is it redundant or is there a replacement?

sctm81

Posts : 29
Join date : 2009-04-24

View user profile

Back to top Go down

Re: Moving an Object before physic

Post  sio2interactive on Tue May 19, 2009 2:03 pm

jj: Use this instead to move your object manually using a matrix in order to affect it to the current Bullet matrix assigned on the object:

_SIO2object->_btRigidBody->getWorldTransform().setFromOpenGLMatrix( _SIO2object->_SIO2transform->mat );


sctm81: It have been moved in SIO2transform, sio2TransformBindMatrix

_________________
SIO2 Interactive
Free Open Source 3D Game Engine for iPhone and iPod Touch
http://sio2interactive.com
avatar
sio2interactive

Posts : 1526
Join date : 2008-08-26
Age : 38
Location : Shanghai

View user profile http://sio2interactive.com

Back to top Go down

Re: Moving an Object before physic

Post  Sponsored content


Sponsored content


Back to top Go down

View previous topic View next topic Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum