/* Reduce a number to its best type (i.e. no complex numbers with 0i, no doubles with 0's after the decimal point, etc */ void ReduceNumber(VyNumber** num){ /* Check that, if it is a double, it isnt an int in disguise */ if(num[0]->type == REAL){ double d = GetDouble(num); /* If it is, change it to an int */ if(d == (int)(d)){ num[0]->type = INT; IntNum* iNum = NumberToSubtype(num); iNum->i = (int)(d); } } /* Check that, if it is a complex number, the imaginary part isn't 0 */ else if(num[0]->type == COMPLEX){ if(EqualsZero(GetImaginary(num))){ /* Make it equal the real part */ VyNumber** realPart = GetReal(num); num[0]->type = realPart[0]->type; switch(num[0]->type){ case INT: ((IntNum*)(NumberToSubtype(num)))->i = GetInt(realPart); break; case REAL: ((RealNum*)(NumberToSubtype(num)))->d = GetDouble(realPart); break; } } } }
bool Unproject( const Vector4& windowPos, const Matrix& inverseMvp, float viewportWidth, float viewportHeight, Vector4& objectPos ) { objectPos.x = windowPos.x; objectPos.y = windowPos.y; objectPos.z = windowPos.z; objectPos.w = 1.0f; objectPos.x = objectPos.x / viewportWidth; objectPos.y = objectPos.y / viewportHeight; objectPos.x = objectPos.x * 2.0f - 1.0f; objectPos.y = objectPos.y * 2.0f - 1.0f; objectPos.z = objectPos.z * 2.0f - 1.0f; objectPos = inverseMvp * objectPos; // In the case where objectPos.w is exactly zero, the unproject fails if ( EqualsZero( objectPos.w ) ) { return false; } objectPos.x /= objectPos.w; objectPos.y /= objectPos.w; objectPos.z /= objectPos.w; return true; }
void BulletDynamicsBody::SetMass( float mass ) { DEBUG_PRINTF("%s(%.2f)\n", __func__, mass); if( Dali::DynamicsBodyConfig::RIGID == mSettings->type ) { btRigidBody* rigidBody( btRigidBody::upcast(mBody) ); DALI_ASSERT_DEBUG( rigidBody ); btVector3 inertia(0,0,0); if( ! EqualsZero( mass ) ) { btCollisionShape* shape = mBody->getCollisionShape(); shape->calculateLocalInertia(mass, inertia); } rigidBody->setMassProps(btScalar(mass), inertia); } else if( Dali::DynamicsBodyConfig::SOFT == mSettings->type ) { btSoftBody* softBody( btSoftBody::upcast(mBody) ); DALI_ASSERT_DEBUG( softBody ); softBody->setTotalMass(mass); } }
void BulletDynamicsBody::CreateRigidBody( const Integration::DynamicsBodySettings& bodySettings, Dali::Integration::DynamicsShape* shape, const Vector3& startPosition, const Quaternion& startRotation ) { DEBUG_PRINTF("%s\n", __func__ ); Vector3 axis; float angle( 0.0f ); startRotation.ToAxisAngle( axis, angle ); btVector3 inertia( 0.0f ,0.0f ,0.0f ); btCollisionShape* collisionShape( ((BulletDynamicsShape*)shape)->GetShape() ); if( ! EqualsZero( bodySettings.mass ) ) { collisionShape->calculateLocalInertia(bodySettings.mass, inertia); } btRigidBody::btRigidBodyConstructionInfo constructionInfo(bodySettings.mass, NULL, collisionShape, inertia); constructionInfo.m_restitution = bodySettings.elasticity; constructionInfo.m_linearDamping = bodySettings.linearDamping; constructionInfo.m_angularDamping = bodySettings.angularDamping; constructionInfo.m_friction = bodySettings.friction; constructionInfo.m_linearSleepingThreshold = bodySettings.linearSleepVelocity; constructionInfo.m_angularSleepingThreshold = bodySettings.angularSleepVelocity; constructionInfo.m_startWorldTransform.setOrigin(btVector3(startPosition.x, startPosition.y, startPosition.z)); if( axis != Vector3::ZERO ) { constructionInfo.m_startWorldTransform.setRotation( btQuaternion(btVector3(axis.x, axis.y, axis.z), btScalar(angle)) ); } mBody = new btRigidBody( constructionInfo ); mBody->setUserPointer(this); }
bool Weapon::LoadXml(const xmlNode* weapon) { const xmlNode* elem = XmlReader::GetMarker(weapon, m_id); if (!elem) { std::cout << Format(_("No element <%s> found in the xml config file!"), m_id.c_str()) << std::endl; return false; } XmlReader::ReadInt(elem, "available_after_turn", m_available_after_turn); XmlReader::ReadInt(elem, "nb_ammo", m_initial_nb_ammo); XmlReader::ReadInt(elem, "unit_per_ammo", m_initial_nb_unit_per_ammo); XmlReader::ReadInt(elem, "ammo_per_drop", ammo_per_drop); XmlReader::ReadDouble(elem, "drop_probability", drop_probability); if (m_initial_nb_ammo == INFINITE_AMMO && drop_probability.IsNotZero()) { std::cerr << Format(_("The weapon %s has infinite ammo, but bonus boxes might contain ammo for it!"), m_id.c_str()); std::cerr << std::endl; } // max strength // if max_strength = 0, no strength_bar ! XmlReader::ReadDouble(elem, "max_strength", max_strength); // change weapon after ? (for the grapple = true) XmlReader::ReadBool(elem, "change_weapon", m_can_change_weapon); // Disable crosshair ? XmlReader::ReadBool(elem, "display_crosshair", m_display_crosshair); // angle of weapon when drawing // if (min_angle == max_angle) no cross_hair ! // between -90 to 90 degrees int min_angle_deg = 0, max_angle_deg = 0; XmlReader::ReadInt(elem, "min_angle", min_angle_deg); XmlReader::ReadInt(elem, "max_angle", max_angle_deg); min_angle = min_angle_deg * PI / 180; max_angle = max_angle_deg * PI / 180; if (EqualsZero(min_angle - max_angle)) m_display_crosshair = false; #if 0 if (m_weapon_fire) { uint num = 32 * (max_angle - min_angle) / TWO_PI; m_weapon_fire->EnableCaches(true, num, min_angle, max_angle); } #endif // Load extra parameters if existing if (extra_params) extra_params->LoadXml(elem); if (drawable && origin == weapon_origin_HAND) m_image->SetRotation_HotSpot(position); return true; }
void Vector2::Normalize() { float length = Length(); if( ! EqualsZero(length) ) { MATH_INCREASE_BY(PerformanceMonitor::FLOAT_POINT_MULTIPLY,2); const float inverseLength = 1.0f / length; x *= inverseLength; y *= inverseLength; } }
void PhysicalObj::Drown() { ASSERT (m_alive != DROWNED); MSG_DEBUG("physic.state", "%s - Drowned...", GetName().c_str()); m_alive = DROWNED; // Set the air grab to water resist factor. SetAirResistFactor(m_cfg.m_water_resist_factor); // Ensure the gravity factor is upper than 0.0 if (EqualsZero(GetGravityFactor())) SetGravityFactor(0.1); // If fire, do smoke... if (m_is_fire) GetWorld().water.Smoke(GetPosition()); // make a splash in the water :-) else if (GetMass() >= 2 && GetName() != "water_particle") GetWorld().water.Splash(GetPosition()); StopMoving(); StartMoving(); SignalDrowning(); }
Weapon::Weapon(Weapon_type type, const std::string &id, EmptyWeaponConfig * params, bool drawable): drawable(drawable) { m_type = type; m_category = INVALID; m_id = id; m_help = ""; m_time_anim_begin = Time::GetInstance()->Read(); m_available_after_turn = 0; m_initial_nb_ammo = INFINITE_AMMO; m_initial_nb_unit_per_ammo = 1; ammo_per_drop = 1; drop_probability = 0; use_unit_on_first_shoot = true; can_be_used_on_closed_map = true; m_strength = 0; m_first_time_loading = 0; m_last_fire_time = 0; m_time_between_each_shot = 400; m_fire_remanence_time = 100; max_strength = min_angle = max_angle = 0; use_flipping = true; m_display_crosshair = true; origin = weapon_origin_HAND; m_can_change_weapon = false; m_image = NULL; m_weapon_fire = NULL; if (!use_flipping && !EqualsZero(min_angle - max_angle)) use_flipping = true; extra_params = params; if (drawable) { m_image = new Sprite(GetResourceManager().LoadImage(weapons_res_profile, m_id)); m_image->EnableCaches(use_flipping, 0); } icon = new Sprite(GetResourceManager().LoadImage(weapons_res_profile,m_id+"_ico")); mouse_character_selection = true; const xmlNode* elem = GetResourceManager().GetElement(weapons_res_profile, "position", m_id); if (elem) { // E.g. <position name="my_weapon_id" origin="hand" x="-1" y="0" /> std::string origin_xml; XmlReader::ReadIntAttr (elem, "x", position.x); XmlReader::ReadIntAttr (elem, "y", position.y); if (!XmlReader::ReadStringAttr (elem, "origin", origin_xml)) { std::cerr << "No \"origin\" flag found for weapon %s" << m_id <<std::endl; ASSERT(false); } if (origin_xml == "over") origin = weapon_origin_OVER; else origin = weapon_origin_HAND; } else { std::cerr << "No \"position\" flag found for weapon %s" << m_id <<std::endl; ASSERT(false); } elem = GetResourceManager().GetElement(weapons_res_profile, "hole", m_id); if (elem) { // E.g. <hole name="my_weapon_id" dx="-1" dy="0" /> XmlReader::ReadIntAttr(elem, "dx", hole_delta.x); XmlReader::ReadIntAttr(elem, "dy", hole_delta.y); } }
void Weapon::Draw(){ if (Game::GetInstance()->ReadState() != Game::PLAYING && m_last_fire_time + 100 < Time::GetInstance()->Read()) return; #ifndef DEBUG_HOLE if (m_last_fire_time + m_fire_remanence_time > Time::GetInstance()->Read()) #endif DrawWeaponFire(); DrawAmmoUnits(); ASSERT(drawable || !ShouldBeDrawn()); if (!(drawable && ShouldBeDrawn())) return; if (ActiveCharacter().IsGhost() || ActiveCharacter().IsDrowned() || ActiveCharacter().IsDead()) return; // rotate weapon if needed if (!EqualsZero(min_angle - max_angle)) { if (ActiveCharacter().GetDirection() == DIRECTION_RIGHT) m_image->SetRotation_rad(ActiveCharacter().GetFiringAngle()); else m_image->SetRotation_rad(ActiveCharacter().GetFiringAngle()-PI); } else { m_image->SetRotation_rad(ZERO); } // flip image if needed if (use_flipping) { m_image->SetFlipped(DIRECTION_LEFT == ActiveCharacter().GetDirection()); } // Calculate position of the image int x,y; PosXY (x, y); // Animate the display of the weapon: if (m_time_anim_begin + ANIM_DISPLAY_TIME > Time::GetInstance()->Read()) { if (!EqualsZero(min_angle - max_angle)) { Double angle = m_image->GetRotation_rad(); angle += sin(HALF_PI * Double(Time::GetInstance()->Read() - m_time_anim_begin) / ANIM_DISPLAY_TIME) * TWO_PI; m_image->SetRotation_rad(angle); m_image->Scale(ONE, ONE); } else { Double scale = sin((Double)1.5 * HALF_PI * Double(Time::GetInstance()->Read() - m_time_anim_begin) / ANIM_DISPLAY_TIME) / sin((Double)1.5 * HALF_PI); if (scale.IsNotZero()) { m_image->Scale(scale, scale); m_image->SetFlipped(DIRECTION_LEFT == ActiveCharacter().GetDirection()); } // Recompute position to get the icon centered over the skin if (origin == weapon_origin_OVER) PosXY(x,y); } } else m_image->Scale(ONE, ONE); if (m_image) m_image->Blit(GetMainWindow(), Point2i(x, y) - Camera::GetInstance()->GetPosition()); #ifdef DEBUG if (IsLOGGING("weapon")) { Point2i hand; ActiveCharacter().GetHandPosition(hand); Rectanglei rect(hand - 1 - Camera::GetInstance()->GetPosition(), Point2i(3, 3)); GetWorld().ToRedrawOnMap(rect); GetMainWindow().RectangleColor(rect, c_red); MSG_DEBUG("weapon.handposition", "Position: %d, %d - hand: %d, %d", ActiveCharacter().GetX(), ActiveCharacter().GetY(), hand.GetX(), hand.GetY()); } if (IsLOGGING("weapon.hole")) { Rectanglei rect(GetGunHolePosition() - Camera::GetInstance()->GetPosition() - 1, Point2i(3, 3)); GetWorld().ToRedrawOnMap(rect); GetMainWindow().RectangleColor(rect, c_red); } #endif }