Beispiel #1
0
void Body::update(double dt) {
    if (!fixedPosition) {
        move(speed*dt +force*0.5*dt*dt/mass);
        speed += force*dt/mass;
    }
    if (!fixedAngle) {
        rotate(angularSpeed*dt + 0.5*momentium*dt*dt/inertiaMoment);
        angularSpeed += momentium*dt/inertiaMoment;
    }

    resetForces();
}
void PhysicsBody::setDynamic(bool dynamic)
{
    if (dynamic != _dynamic)
    {
        _dynamic = dynamic;
        if (dynamic)
        {
            cpBodySetMass(_info->getBody(), _mass);
            cpBodySetMoment(_info->getBody(), _moment);
            
            if (_world != nullptr)
            {
                // reset the gravity enable
                if (isGravityEnabled())
                {
                    _gravityEnabled = false;
                    setGravityEnable(true);
                }
                
                cpSpaceAddBody(_world->_info->getSpace(), _info->getBody());
            }
        }
        else
        {
            if (_world != nullptr)
            {
                cpSpaceRemoveBody(_world->_info->getSpace(), _info->getBody());
            }
            
            // avoid incorrect collion simulation.
            cpBodySetMass(_info->getBody(), PHYSICS_INFINITY);
            cpBodySetMoment(_info->getBody(), PHYSICS_INFINITY);
            cpBodySetVel(_info->getBody(), cpvzero);
            cpBodySetAngVel(_info->getBody(), 0.0f);
            resetForces();
        }
        
    }
}
Beispiel #3
0
    void
    Object::update(double ts)
    {
      // Initialize variables.
      double c1 = std::cos(m_orientation[0]);
      double c2 = std::cos(m_orientation[1]);
      double c3 = std::cos(m_orientation[2]);

      double s1 = std::sin(m_orientation[0]);
      double s2 = std::sin(m_orientation[1]);
      double s3 = std::sin(m_orientation[2]);

      double t2 = std::tan(m_orientation[1]);

      double u = m_linear_velocity[0];
      double v = m_linear_velocity[1];
      double w = m_linear_velocity[2];
      double p = m_angular_velocity[0];
      double q = m_angular_velocity[1];
      double r = m_angular_velocity[2];

      double d_pos[6];
      double d_vel[6];

      // Accelerations.
      for (unsigned i = 0; i < 6; ++i)
        d_vel[i] = m_forces[i] / m_inertia[i];

      // Reset forces to zero.
      resetForces();

      // Compute Velocities
      // Transformation Matrix: eta1dot = J1(eta2)*nu1
      //    J1=[ c3*c2   c3*s2*s1-s3*c1  s3*s1+c3*c1*s2
      //         s3*c2   c1*c3+s1*s2*s3  c1*s2*s3-c3*s1
      //          -s2        c2*s1           c1*c2     ];
      d_pos[0] = (c3 * c2) * u + (c3 * s2 * s1 - s3 * c1) * v + (s3 * s1 + c3 * c1 * s2) * w;
      d_pos[1] = (s3 * c2) * u + (c1 * c3 + s1 * s2 * s3) * v + (c1 * s2 * s3 - c3 * s1) * w;
      d_pos[2] = (-s2) * u + (c2 * s1) * v + (c1 * c2) * w;

      // Transformation Matrix: eta2dot = J1(eta2)*nu2
      //   J2=[ 1   s1*t2   c1*t2
      //        0    c1      -s1
      //        0   s1/c2   c1/c2 ];
      d_pos[3] = p + (s1 * t2) * q + (c1 * t2) * r;
      d_pos[4] = c1 * q + (-s1) * r;
      d_pos[5] = (s1 / c2) * q + (c1 / c2) * r;

      // Integrate using Euler's method.
      for (unsigned i = 0; i < 3; i++)
      {
        m_position[i] += d_pos[i] * ts;
        m_orientation[i] += Angles::minSignedAngle(m_orientation[i],
                                                   m_orientation[i] + d_pos[i + 3] * ts);

        if (m_integration_method)
        {
          m_linear_velocity[i] += d_vel[i] * ts;
          m_angular_velocity[i] = m_angular_velocity[i] + d_vel[i + 3] * ts;
        }
        else
        {
          // ASV integration.
          m_linear_velocity[i] = d_vel[i];
          m_angular_velocity[i] = d_vel[i + 3];
        }
      }

      if (m_position[2] <= 0.0)
        m_position[2] = 0.0;
    }
Beispiel #4
0
int main(int argc, char* argv[]) {

  int number_of_stars = 200;
  int iterations = 1000;
  prec time_unit = gdt;

  if(argc == 3)
    {
      number_of_stars = atoi(argv[1]);
      iterations = atoi(argv[2]);
    }
  struct star* star_array = malloc(sizeof(struct star) * number_of_stars);
  generate_init_values(number_of_stars, star_array);
  
  
  
  
  
#ifdef ANIMATE
  XPoint* points = malloc(sizeof(XPoint)*number_of_stars);
  Display* disp;
  Window window, rootwin;
  int screen;

  disp = XOpenDisplay(NULL);
  screen = DefaultScreen(disp);
  rootwin = RootWindow(disp,screen);
  window = XCreateSimpleWindow(disp,rootwin,
                               0,0,X_SIZE,Y_SIZE,1,0,0);
  GC gc = XCreateGC(disp, window, 0, 0);
  XSetForeground(disp, gc, WhitePixel(disp, screen));
  XSetBackground(disp, gc, BlackPixel(disp, screen));
  XMapWindow(disp,window);

  XClearWindow(disp,window);	
	
  copyToXBuffer(star_array, points, number_of_stars);
  XDrawPoints(disp, window, gc, points, number_of_stars, 0);

  XFlush(disp);

#endif

  clock_t start = clock();
  for(int i = 0; i < iterations; i++)
    {

#ifndef ANIMATE
      
      resetForces(number_of_stars, star_array);
      updateForces(number_of_stars, number_of_stars - 1, star_array);
      for(int j = 0; j < number_of_stars; ++j){
        update_position(&star_array[j], time_unit);
      }

#endif
     
#ifdef ANIMATE

      resetForces(number_of_stars, star_array);
      updateForces(number_of_stars, number_of_stars - 1, star_array);
      for(int j = 0; j < number_of_stars; ++j){
        update_position(&star_array[j], time_unit);


        copyToXBuffer(star_array, points,number_of_stars );
        XDrawPoints(disp, window, gc, points, number_of_stars, CoordModeOrigin);
	

      }
      XClearWindow(disp,window);

    
#endif
 
    }
  clock_t stop = clock();
  float diff = (float)(stop - start)/CLOCKS_PER_SEC;
  printf("Total: %lf seconds\n",diff);
  printf("Bodies: %d\n",number_of_stars);
  printf("Iterations: %d\n", iterations);

#ifdef ANIMATE
  free(points);
  XCloseDisplay(disp);
#endif
  free(star_array);
  return 0;
}
Beispiel #5
0
void Pipe::SpawnPipe( cocos2d::Layer *layer )
{
    CCLOG( "SPAWN PIPE" );
    
    //SpriteBatchNode* spritebatch = SpriteBatchNode::create("flappySprites.png");
    //SpriteFrameCache* cache = SpriteFrameCache::getInstance();
    //cache->addSpriteFramesWithFile("flappySprites.plist");
    
    //auto backgroundSprite = Sprite::createWithSpriteFrameName("background1.png");

    
    auto topPipe = Sprite::createWithSpriteFrameName( "pipe.png" );
    auto bottomPipe = Sprite::createWithSpriteFrameName( "pipe.png" );
    
    auto topPipeBody = PhysicsBody::createBox( topPipe->getContentSize( ) );
    auto bottomPipeBody = PhysicsBody::createBox( bottomPipe->getContentSize( ) );
    
    auto random = CCRANDOM_0_1( );
    
    if ( random < LOWER_SCREEN_PIPE_THRESHOLD )
    {
        random = LOWER_SCREEN_PIPE_THRESHOLD;
    }
    else if ( random > UPPER_SCREEN_PIPE_THRESHOLD )
    {
        random = UPPER_SCREEN_PIPE_THRESHOLD;
    }
    
    
    auto topPipePosition = ( random * visibleSize.height ) + ( topPipe->getContentSize( ).height*2 / 3 );
    
    topPipeBody->setDynamic( false );
    bottomPipeBody->setDynamic( false );
    
    topPipeBody->setCollisionBitmask( OBSTACLE_COLLISION_BITMASK );
    bottomPipeBody->setCollisionBitmask( OBSTACLE_COLLISION_BITMASK );
    topPipeBody->setContactTestBitmask( true );
    bottomPipeBody->setContactTestBitmask( true );
    
    topPipe->setPhysicsBody( topPipeBody );
    bottomPipe->setPhysicsBody( bottomPipeBody );
    
    topPipe->setFlippedY(true);
    topPipe->setPosition( Point( visibleSize.width + topPipe->getContentSize( ).width + origin.x, topPipePosition ) );
    bottomPipe->setPosition( Point( topPipe->getPositionX(), topPipePosition - ( Sprite::createWithSpriteFrameName( "frame-1.png" )->getContentSize( ).height * PIPE_GAP ) - topPipe->getContentSize().height ) );
    
    layer->addChild( topPipe );
    layer->addChild( bottomPipe );
    
    auto topPipeAction = MoveBy::create( PIPE_MOVEMENT_SPEED * visibleSize.width, Point( -visibleSize.width * 1.5, 0 ) );
    auto bottomPipeAction = MoveBy::create( PIPE_MOVEMENT_SPEED * visibleSize.width, Point( -visibleSize.width * 1.5, 0 ) );
    
    topPipe->runAction( topPipeAction );
    bottomPipe->runAction( bottomPipeAction );
    
    auto pointNode = Node::create( );
    auto pointBody = PhysicsBody::createBox( Size( 1, Sprite::createWithSpriteFrameName( "frame-1.png" )->getContentSize( ).height * PIPE_GAP ) );
    
    pointBody->setDynamic( false );
    pointBody->setCollisionBitmask( POINT_COLLISION_BITMASK );
    pointBody->setContactTestBitmask( true );
    pointBody->resetForces();
    
    pointNode->setPhysicsBody( pointBody );
    pointNode->setPosition( Point( topPipe->getPositionX( ), topPipe->getPositionY( ) - ( topPipe->getContentSize( ).height / 2 ) - ( ( Sprite::createWithSpriteFrameName( "frame-1.png" )->getContentSize( ).height * PIPE_GAP ) / 2 ) ) );
    
    layer->addChild( pointNode );
    
    auto pointNodeAction = MoveBy::create( PIPE_MOVEMENT_SPEED * visibleSize.width, Point( -visibleSize.width * 1.5, 0 ) );
    
    pointNode->runAction( pointNodeAction );}