Exemple #1
0
void PlayerShip::accelerate()
{
	if (getSpeed() < 5.0F)
		setSpeed(getSpeed() + 1.0F);
	writeVelocity().x_ += cos(deg2rad(getAngle())) * getSpeed();
	writeVelocity().y_ += sin(deg2rad(getAngle())) * getSpeed();
}
Exemple #2
0
void Bullet::fire()
{
	writeVelocity().x_ += cos(deg2rad(getAngle())) * getSpeed();
	writeVelocity().y_ += sin(deg2rad(getAngle())) * getSpeed();
}
void Structure0815:: iterate
(
  double dt )
{
  DynVector zero(_dim, 0.0);
  DynVector force(3, 0.0);
  DynVector totalForce(_gravity);
  totalForce *= _totalMass; // Makes gravity acceleration a force
  DynVector r(3, 0.0); // Distance vector from center of gravity
  DynVector totalTorque(3, 0.0); // Always init with three components
  DynVector torque(3, 0.0);

  int verticesSize = _vertices.size() / _dim;
  for (int iVertex=0; iVertex < verticesSize; iVertex++){

    for (int i=0; i < _dim; i++){
      force[i] = _forces[iVertex*_dim + i];
      totalForce[i] += force[i];
    }

    for (int i=0; i<_dim; i++){
      r[i] = _vertices[iVertex*_dim + i] - _centerOfGravity[i];
    }
    tarch::la::cross(r, force, torque);
    totalTorque += torque;
  }

  //STRUCTURE_DEBUG("Total mass = " << _totalMass);
  STRUCTURE_DEBUG("Total force = " << totalForce);
  //STRUCTURE_DEBUG("Center of gravity = " << _centerOfGravity);
  STRUCTURE_DEBUG("Total torque = " << totalTorque);

  DynVector translVelocityDelta(zero);
  if (not _fixed){
    // Compute values of next timestep
    translVelocityDelta = (totalForce / _totalMass) * dt;
    STRUCTURE_DEBUG("translVelocityDelta = " << translVelocityDelta);
  }
  for (int i=0; i < _dim; i++){
    if (_fixedTranslationDirections[i]){
      translVelocityDelta[i] = 0.0;
    }
  }

  // Set values of next timestep
  DynVector rotForce(3, 0.0);
  DynVector rotVelocityDelta(zero);
  DynVector writeVelocity(zero);
  DynVector writeDisplacement(zero);
  DynVector velocityDelta(zero);
  double normR = 0.0;
  for (int iVertex=0; iVertex < verticesSize; iVertex++){
    int index = iVertex *_dim;
    // Compute and write velocity
    for (int i=0; i<_dim; i++){
      r[i] = _vertices[index + i] - _centerOfGravity[i];
    }
    normR = tarch::la::norm2(r);

    tarch::la::cross(totalTorque, r, rotForce);
    //rotForce[0] = torque * -1.0 * r[1]; // TODO
    //rotForce[1] = torque * r[0]; // TODO
    for (int i=0; i < _dim; i++){
      rotVelocityDelta[i] = (rotForce[i] / _totalMass) * dt;
    }
    velocityDelta = rotVelocityDelta + translVelocityDelta;
    for (int i=0; i < _dim; i++){
      _velocityDeltas[index+i] = velocityDelta[i];
      _velocities[index+i] = _oldVelocities[index+i] + _velocityDeltas[index+i];
      _displacementDeltas[index+i] =
          (_oldVelocities[index+i] + _velocityDeltas[index+i])*dt;
      _displacements[index+i] =
          _oldDisplacements[index+i] + _displacementDeltas[index+i];
    }
  }

  STRUCTURE_DEBUG("Computed time = " << _time
                  << ", computed timesteps = " << _timesteps);
}