Example #1
0
void Game::applyCollision(Car& car1, Car& car2)
{
    int angle1 = car1.getTravelAngle();
    int angle2 = car2.getTravelAngle();

    sf::Vector2f unitVector1(cos(angle1*PI/180), sin(angle1*PI/180));
    sf::Vector2f unitVector2(cos(angle1*PI/180), sin(angle1*PI/180));

    int mass1 = car1.getMass();
    int mass2 = car2.getMass();
    int massTotal = mass1 + mass2;

    double posX1 = car1.getPosX();
    double posY1 = car1.getPosY();
    double posX2 = car2.getPosX();
    double posY2 = car2.getPosY();

    double vel1 = car1.getVelocity();
    double vel2 = car2.getVelocity();

    double momentumInit1X = mass1 * vel1 * unitVector1.x;
    double momentumInit2X = mass2 * vel2 * unitVector2.x;

    double momentumInit1Y = mass1 * vel1 * unitVector1.y;
    double momentumInit2Y = mass2 * vel2 * unitVector2.y;

    double momentumTotalInitX = momentumInit1X + momentumInit2X;
    double momentumTotalInitY = momentumInit1Y + momentumInit2Y;

    double velFinalX = momentumTotalInitX/massTotal;
    double velFinalY = momentumTotalInitY/massTotal;

    double velFinal = sqrt(velFinalX * velFinalX + velFinalY * velFinalY)*2;
    double angleFinal = tan(velFinalY/velFinalX)*10;

    car1.setDriveState(4);
    car2.setDriveState(4);

    //NEED to separate (minimum distance) before setting cars off on their ways
    car1.setTravelAngle(angle2);
    car2.setTravelAngle(angle1);

    car1.setVelocity(vel2);
    car2.setVelocity(vel1);

    std::cout << "car1: " << theCar.getTravelAngle() << "\n";
    std::cout << "car2: " << car.getTravelAngle() << "\n";
}
Example #2
0
void RemoteProcessClient::writeCar(const Car& car) {
    writeBoolean(true);

    writeLong(car.getId());
    writeDouble(car.getMass());
    writeDouble(car.getX());
    writeDouble(car.getY());
    writeDouble(car.getSpeedX());
    writeDouble(car.getSpeedY());
    writeDouble(car.getAngle());
    writeDouble(car.getAngularSpeed());
    writeDouble(car.getWidth());
    writeDouble(car.getHeight());
    writeLong(car.getPlayerId());
    writeInt(car.getTeammateIndex());
    writeBoolean(car.isTeammate());
    writeEnum<CarType>(car.getType());
    writeInt(car.getProjectileCount());
    writeInt(car.getNitroChargeCount());
    writeInt(car.getOilCanisterCount());
    writeInt(car.getRemainingProjectileCooldownTicks());
    writeInt(car.getRemainingNitroCooldownTicks());
    writeInt(car.getRemainingOilCooldownTicks());
    writeInt(car.getRemainingNitroTicks());
    writeInt(car.getRemainingOiledTicks());
    writeDouble(car.getDurability());
    writeDouble(car.getEnginePower());
    writeDouble(car.getWheelTurn());
    writeInt(car.getNextWaypointX());
    writeInt(car.getNextWaypointY());
    writeBoolean(car.isFinishedTrack());
}