Beispiel #1
0
CL_Angle CarImpl::vecToAngle(const CL_Vec2f &p_vec)
{
	const static CL_Vec2f ANGLE_ZERO(1.0f, 0.0f);
	CL_Angle angle = p_vec.angle(ANGLE_ZERO);

	if (p_vec.y < 0) {
		angle.set_radians(-angle.to_radians());
	}

	return angle;

}
std::vector<CL_Pointf> CL_BezierCurve_Impl::generate_curve_points(const CL_Angle &split_angle)
{
	std::vector<CL_Pointf> points;
/*
	for (float i = 0.0; i < 1.0; i += 0.01)
	{
		points.push_back(get_point_relative(i));
	}
	points.push_back(get_point_relative(1.0));
*/

	split_angle_rad = split_angle.to_radians(); 

	points.push_back( get_point_relative(0.0) );

	std::vector<CL_Pointf> sub_points = subdivide_bezier(0.0, 0.5);
	points.insert( points.end(), sub_points.begin(), sub_points.end() );

	sub_points = subdivide_bezier(0.5, 1.0);
	points.insert( points.end(), sub_points.begin(), sub_points.end() );

	return points;
}
void CL_Collada_Triangles_Impl::create_vertices_normal(CL_Vec3f *destination, int stride, const CL_String &semantic, const CL_Angle &smoothing_angle)
{
	CL_Collada_Input_Shared &input = get_input(semantic);

	CL_Collada_Source &source = input.get_source();
	if (source.is_null())
	{
		throw CL_Exception("unsupported situation. fixme");
	}

	std::vector<CL_Vec3f> &pos_array = source.get_vec3f_array();

	unsigned int primitive_offset = input.get_offset();
	std::vector<CL_Vec3f> face_normals;
	calculate_face_normals(face_normals, pos_array, primitive_offset);

	std::vector<int> face_offsets;
	std::vector<int> faces;

	generate_point_facelist(face_offsets, faces, pos_array.size(), primitive_offset);

	calculate_point_normals(face_offsets, faces, face_normals, destination, stride, smoothing_angle.to_radians(), primitive_offset );
}
Beispiel #4
0
CL_Mat3<Type> CL_Mat3<Type>::rotate(const CL_Angle &angle, Type x, Type y, Type z, bool normalize)
{
	if (normalize)
	{
		Type len2 = x*x+y*y+z*z;
		if (len2 != (Type)1)
		{	
			Type length = sqrt(len2);
			if (length > (Type) 0)
			{
				x /= length;
				y /= length;
				z /= length;
			}
			else
			{
				x = (Type) 0;
				y = (Type) 0;
				z = (Type) 0;
			}
		}
	}

	CL_Mat3<Type> rotate_matrix;
	Type c = cos(angle.to_radians());
	Type s = sin(angle.to_radians());
	rotate_matrix.matrix[0+0*3] = (Type) (x*x*(1.0f - c) + c);
	rotate_matrix.matrix[0+1*3] = (Type) (x*y*(1.0f - c) - z*s);
	rotate_matrix.matrix[0+2*3] = (Type) (x*z*(1.0f - c) + y*s);
	rotate_matrix.matrix[1+0*3] = (Type) (y*x*(1.0f - c) + z*s);
	rotate_matrix.matrix[1+1*3] = (Type) (y*y*(1.0f - c) + c);
	rotate_matrix.matrix[1+2*3] = (Type) (y*z*(1.0f - c) - x*s);
	rotate_matrix.matrix[2+0*3] = (Type) (x*z*(1.0f - c) - y*s);
	rotate_matrix.matrix[2+1*3] = (Type) (y*z*(1.0f - c) + x*s);
	rotate_matrix.matrix[2+2*3] = (Type) (z*z*(1.0f - c) + c);
	return rotate_matrix;
}
Beispiel #5
0
CL_Mat3<int> CL_Mat3<int>::rotate(const CL_Angle &angle, int x, int y, int z, bool normalize)
{
	if (normalize)
	{
		int len2 = x*x+y*y+z*z;
		if (len2 != (int)1)
		{	
			int length = sqrt( (float) len2);
			if (length > 0)
			{
				x /= length;
				y /= length;
				z /= length;
			}
			else
			{
				x = 0;
				y = 0;
				z = 0;
			}
		}
	}

	CL_Mat3<int> rotate_matrix;
	float c = cos(angle.to_radians());
	float s = sin(angle.to_radians());
	rotate_matrix.matrix[0+0*3] = (int) floor((x*x*(1.0f - c) + c)+0.5f);
	rotate_matrix.matrix[0+1*3] = (int) floor((x*y*(1.0f - c) - z*s)+0.5f);
	rotate_matrix.matrix[0+2*3] = (int) floor((x*z*(1.0f - c) + y*s)+0.5f);
	rotate_matrix.matrix[1+0*3] = (int) floor((y*x*(1.0f - c) + z*s)+0.5f);
	rotate_matrix.matrix[1+1*3] = (int) floor((y*y*(1.0f - c) + c)+0.5f);
	rotate_matrix.matrix[1+2*3] = (int) floor((y*z*(1.0f - c) - x*s)+0.5f);
	rotate_matrix.matrix[2+0*3] = (int) floor((x*z*(1.0f - c) - y*s)+0.5f);
	rotate_matrix.matrix[2+1*3] = (int) floor((y*z*(1.0f - c) + x*s)+0.5f);
	rotate_matrix.matrix[2+2*3] = (int) floor((z*z*(1.0f - c) + c)+0.5f);
	return rotate_matrix;
}
Beispiel #6
0
void CarImpl::alignRotation(CL_Angle &p_what, const CL_Angle &p_to, float p_stepRad)
{
	// works only on normalized values
	CL_Angle normWhat(p_what);
	CL_Angle normTo(p_to);

	Workarounds::clAngleNormalize(&normWhat);
	Workarounds::clAngleNormalize(&normTo);

	const CL_Angle diffAngle = normWhat - normTo;

	float diffRad = diffAngle.to_radians();

	// if difference is higher than 180, then rotate in shorten way
	if (diffRad > CL_PI) {
		diffRad -= CL_PI * 2;
	} else if (diffRad < -CL_PI) {
		diffRad += CL_PI * 2;
	}

	const float diffRadAbs = fabs(diffRad);

	if (diffRadAbs > 0.01f) {
		if (diffRadAbs > p_stepRad) {

			const CL_Angle stepAngle(p_stepRad, cl_radians);

			if (diffRad > 0.0f) {
				p_what -= stepAngle;
			} else {
				p_what += stepAngle;
			}
		} else {
			p_what = p_to;
		}
	}
}
Beispiel #7
0
void MotionBlurShaderImpl::setUniforms(CL_ProgramObject &p_program)
{
	p_program.set_uniform1i("radius", m_radius);
	p_program.set_uniform1f("angle", m_angle.to_radians());
}
Beispiel #8
0
void CarImpl::update1_60() {
	
	static const float BRAKE_POWER = 0.1f;
	static const float ACCEL_POWER = 0.014f;
	static const float SPEED_LIMIT = 15.0f;
	static const float WHEEL_TURN_SPEED = 1.0f / 10.0f;
	static const float TURN_POWER  = (2 * CL_PI / 360.0f) * 2.5f;
	static const float MOV_ALIGN_POWER = TURN_POWER / 2.0f;
	static const float ROT_ALIGN_POWER = TURN_POWER * 0.7f;
	static const float AIR_RESITANCE = 0.003f; // per one speed unit
	static const float DRIFT_SPEED_REDUCTION_RATE = 0.1f;

	// speed limit under what physics angle reduction will be more aggressive
	static const float LOWER_SPEED_ANGLE_REDUCTION = 6.0f;
	// speed limit under what angle difference will be lower than normal
	static const float LOWER_SPEED_ROTATION_REDUCTION = 6.0f;
	// speed limit under what turn power will decrease
	static const float LOWER_SPEED_TURN_REDUCTION = 2.0f;


	// increase the iteration id
	// be aware of 32-bit integer limit
	if (m_iterId != std::numeric_limits<int32_t>::max()) {
		m_iterId++;
	} else {
		m_iterId = 0;
	}

	// don't do anything if car is locked
	if (m_inputLocked) {
		return;
	}
	
	const float prevSpeed = m_speed; // for m_phySpeedDelta

	// apply inputs to speed
	if (m_inputState.brake) {
		m_speed -= BRAKE_POWER;
	} else if (m_inputState.accel) {
		// only if not choking
		if (!isChoking()) {
			m_chocking = false;
			m_speed += (SPEED_LIMIT - m_speed) * ACCEL_POWER;
		} else {
			m_chocking = true;
		}
	}
	
	// rotate steering wheels
	const float diff = m_inputState.turn - m_phyWheelsTurn;

	if (fabs(diff) > WHEEL_TURN_SPEED) {
		m_phyWheelsTurn += diff > 0.0f ? WHEEL_TURN_SPEED : -WHEEL_TURN_SPEED;
	} else {
		m_phyWheelsTurn = m_inputState.turn;
	}

	const float absSpeed = fabs(m_speed);

	// calculate rotations
	if (m_phyWheelsTurn != 0.0f) {

		// rotate corpse and later physics movement
		CL_Angle turnAngle(TURN_POWER * m_phyWheelsTurn, cl_radians);

		if (absSpeed <= LOWER_SPEED_TURN_REDUCTION) {
			// reduce turn if car speed is too low
			turnAngle.set_radians(turnAngle.to_radians() * (absSpeed / LOWER_SPEED_TURN_REDUCTION));
		}

		if (m_speed > 0.0f) {
			m_rotation += turnAngle;
		} else {
			m_rotation -= turnAngle;
		}

		// rotate corpse and physics movement
		if (absSpeed > LOWER_SPEED_ROTATION_REDUCTION) {
			alignRotation(m_phyMoveRot, m_rotation, MOV_ALIGN_POWER);
		} else {
			alignRotation(m_phyMoveRot, m_rotation, MOV_ALIGN_POWER * ((LOWER_SPEED_ROTATION_REDUCTION + 1.0f) - absSpeed));
		}

	} else {

		// align corpse back to physics movement
		alignRotation(m_rotation, m_phyMoveRot, MOV_ALIGN_POWER);

		// makes car stop rotating if speed is too low
		if (absSpeed > LOWER_SPEED_ANGLE_REDUCTION) {
			alignRotation(m_phyMoveRot, m_rotation, ROT_ALIGN_POWER);
		} else {
			alignRotation(m_phyMoveRot, m_rotation, ROT_ALIGN_POWER * ((LOWER_SPEED_ANGLE_REDUCTION + 1.0f) - absSpeed));
		}

		// normalize rotations only when equal
		if (m_rotation == m_phyMoveRot) {
			Workarounds::clAngleNormalize(&m_rotation);
			Workarounds::clAngleNormalize(&m_phyMoveRot);
		}

	}

	Workarounds::clAngleNormalize(&m_phyMoveRot);
	Workarounds::clAngleNormalize(&m_rotation);


	// reduce speed
	const CL_Angle diffAngle = m_rotation - m_phyMoveRot;
	float diffDegAbs = fabs(diffAngle.to_degrees());

	if (diffDegAbs > 0.1f) {

		CL_Angle diffAngleNorm = diffAngle;
		Workarounds::clAngleNormalize180(&diffAngleNorm);

		// 0.0 when going straight, 1.0 when 90 deg, > 1.0 when more than 90 deg
		const float angleRate = fabs(1.0f - (fabs(diffAngleNorm.to_degrees()) - 90.0f) / 90.0f);
		const float speedReduction = -DRIFT_SPEED_REDUCTION_RATE * angleRate;

		if (absSpeed > speedReduction) {
			m_speed += m_speed > 0.0f ? speedReduction : -speedReduction;
		} else {
			m_speed = 0.0f;
		}
	}

	// car cannot travel too quickly
	m_speed -= m_speed * AIR_RESITANCE;

	// calculate next move vector
	const float m_rotationRad = m_phyMoveRot.to_radians();

	m_phyMoveVec.x = cos(m_rotationRad);
	m_phyMoveVec.y = sin(m_rotationRad);

	m_phyMoveVec.normalize();
	m_phyMoveVec *= m_speed;

	// apply movement (invert y)
	m_position.x += m_phyMoveVec.x;
	m_position.y += m_phyMoveVec.y;

	// set speed delta
	m_phySpeedDelta = m_speed - prevSpeed;


#if defined(CLIENT)
#if !defined(NDEBUG)
	// print debug information
	DebugLayer *dbgl = Gfx::Stage::getDebugLayer();

	dbgl->putMessage("speed", cl_format("%1", m_speed));
//	if (!m_level) {
//		const float resistance = m_level->getResistance(m_position.x, m_position.y);
//		dbgl->putMessage("resist", cl_format("%1", resistance));
//	}
#endif // NDEBUG
#endif // CLIENT
}