当前位置: 首页>>代码示例>>C++>>正文


C++ Vector2::Length方法代码示例

本文整理汇总了C++中Vector2::Length方法的典型用法代码示例。如果您正苦于以下问题:C++ Vector2::Length方法的具体用法?C++ Vector2::Length怎么用?C++ Vector2::Length使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Vector2的用法示例。


在下文中一共展示了Vector2::Length方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: Update

void PlayerArrow::Update()
{
	// アングル
	Vector3 playerFront = Vector3(player->GetParameter().mat.GetFront() * Vector3(1, 0, 1)).Normalized();
	if (playerFront.Length() != 0)
	{
		angle_ = Vector3::Inner(playerFront, -Vector3::Forward);
		if (Vector3::Dot(playerFront, Vector3::Left) > 0.0f)
			angle_ *= -1;
	}

	/* プレイヤーデータ */
	// ポジション
	Vector3 playerPos = player->GetParameter().mat.GetPosition();
	Vector2 pos = Vector2(playerPos.x, -playerPos.z);
	if (pos.Length() != 0.0f)
		drawPos_ = MAP_DRAW_POSITION + pos.Normalized() * pos.Length() * RE_SIZE_SCALE;
	else
		drawPos_ = MAP_DRAW_POSITION;

	/* 気流発生 */
	isDash = player->ReturnTackleParameter().dashFlag;
	if (isDash && isDash != prevDash)
		world.UIAdd(UI_ID::FLOWROOT_UI, std::make_shared<FlowRoot>(world, player, &drawPos_, resPiece));
	prevDash = isDash;
}
开发者ID:Jimendaisuki,项目名称:RideTheFlow,代码行数:26,代码来源:PlayerArrow.cpp

示例2: return

bool 
collides( const Circle & c, const LineSegment & l)
{
  
  Vector2 posToCenter = c.GetCenter() - l.GetStart();
  Vector2 dirVec      = l.GetEnd()    - l.GetStart();

  float segmentLength = dirVec.Length();
  // direction vector must be unit vector (ie. length = 1) in this case!
  // otherwise we would need another formula for scalar projection.
  dirVec = dirVec / segmentLength; 

  // scalar projection of posToCenter to direction vector.
  float d = dirVec.Dot(posToCenter);

  // if d value exceeds original segment length, then we put a cap on it.
  // if these two lines are dismissed, then algorithm sees line segment 
  // as a infinite line.
  if ( d >  segmentLength ) d = segmentLength;
  if ( d < -segmentLength ) d = -segmentLength;
  
  // compute closest point to circle center from line start 
  // along direction vector.
  Vector2 closest_point =l.GetStart() + dirVec * d;

  // vectorfrom circle center to closest point on line
  Vector2 S = closest_point - c.GetCenter();

  return (S.Length() <= c.GetRadius());
}
开发者ID:agrohn,项目名称:sdl-examples,代码行数:30,代码来源:sdl_collision_line_circle.cpp

示例3: AccumulateForce

		// This function calculates how much of its max steering force the bot has left to apply 
		// and then applies that amount of the force to add.
		bool TikiSteering::AccumulateForce(Vector2 &RunningTot, Vector2 ForceToAdd)
		{
			// calculate how much steering force the bot has used so far
			float MagnitudeSoFar = RunningTot.Length();

			// calculate how much steering force remains to be used by this vehicle
			float MagnitudeRemaining = (float)tikiBot->MaxForce() - MagnitudeSoFar;

			// return false if there is no more force left to use
			if (MagnitudeRemaining <= 0.0f) 
				return false;

			//calculate the magnitude of the force we want to add
			float MagnitudeToAdd = ForceToAdd.Length();
  
			// if the magnitude of the sum of ForceToAdd and the running total
			// does not exceed the maximum force available to this bot, just
			// add together. Otherwise add as much of the ForceToAdd vector is
			// possible without going over the max.
			if (MagnitudeToAdd < MagnitudeRemaining)
			{
				RunningTot += ForceToAdd;
			}
			else
			{
				MagnitudeToAdd = MagnitudeRemaining;

				// add it to the steering force
				RunningTot += (Vector2::Normalize(ForceToAdd) * (float)MagnitudeToAdd); 
			}

			return true;
		}
开发者ID:IreNox,项目名称:tiki2,代码行数:35,代码来源:TikiSteering.cpp

示例4: CheckForSpriteScaling

void LevelEditor::CheckForSpriteScaling()
{
	if (mSelectedObject)
	{
		static bool pressingScale = false;
		static Vector2 mouseStartPos = Vector2(0,0);
		static Vector2 originalDimensions = Vector2(0,0);
		if (!pressingScale && GetAsyncKeyState('X') < 0 && GetAsyncKeyState(VK_LBUTTON) < 0 && GetAsyncKeyState(VK_CONTROL) >= 0)
		{
			pressingScale = true;

			POINT currentMouse;
			GetCursorPos(&currentMouse);
			ScreenToClient(DXWindow::GetInstance()->Hwnd(), &currentMouse);
			mouseStartPos = Vector2(currentMouse.x, currentMouse.y);

			originalDimensions = Vector2(mSelectedObject->Dimensions().X, mSelectedObject->Dimensions().Y);
		}
		else if (pressingScale && GetAsyncKeyState(VK_LBUTTON) < 0)
		{
			// get the initial distance between the game object and the mouse
			Vector2 origDistance = mouseStartPos - Vector2(mSelectedObject->X(), mSelectedObject->Y());
			// origDistance = Vector2(abs(origDistance.X), abs(origDistance.Y));

			float origLength = origDistance.Length();

			// now get the distance between the current mouse position
			POINT currentMouse;
			GetCursorPos(&currentMouse);
			ScreenToClient(DXWindow::GetInstance()->Hwnd(), &currentMouse);
			Vector2 newDistance = Vector2(currentMouse.x, currentMouse.y) - Vector2(mSelectedObject->X(), mSelectedObject->Y());

			float newLength = newDistance.Length();

			float scale = newLength / origLength;

			if (scale != 0)
			{
				mSelectedObject->SetDimensionsXYZ(originalDimensions.X * scale, originalDimensions.Y * scale, mSelectedObject->Dimensions().Z);

				Sprite * s = GetAsSprite(mSelectedObject);
				if (s)
				{
					s->ScaleSpriteOnly(scale, scale);
					s->SetIsNativeDimensions(false);
					s->ApplyChange(Graphics::GetInstance()->Device());
				}
			}
		}

		if (GetAsyncKeyState('X') >= 0)
		{
			pressingScale = false;
			mouseStartPos = Vector2(0,0);
			originalDimensions = Vector2(0,0);
		}
	}
}
开发者ID:jeromebyrne,项目名称:2DPlatformer,代码行数:58,代码来源:LevelEditor.cpp

示例5: CheckForRotating

void LevelEditor::CheckForRotating()
{
	if (mSelectedObject)
	{
		static bool pressingRotation = false;
		static Vector2 mouseStartPos = Vector2(0,0);

		if (!pressingRotation && GetAsyncKeyState('R') < 0 && (GetAsyncKeyState(VK_LBUTTON) < 0 || GetAsyncKeyState(VK_RBUTTON) < 0))
		{
			pressingRotation = true;

				POINT currentMouse;
				GetCursorPos(&currentMouse);
				ScreenToClient(DXWindow::GetInstance()->Hwnd(), &currentMouse);
				mouseStartPos = Vector2(currentMouse.x, currentMouse.y);
		}
		else if (pressingRotation && GetAsyncKeyState(VK_LBUTTON) < 0 ||
			     pressingRotation && GetAsyncKeyState(VK_RBUTTON) < 0)
		{
			// get the initial distance between the game object and the mouse
			Vector2 origDistance = mouseStartPos - Vector2(mSelectedObject->X(), mSelectedObject->Y());

			float origLength = origDistance.Length();

			// now get the distance between the current mouse position
			POINT currentMouse;
			GetCursorPos(&currentMouse);
			ScreenToClient(DXWindow::GetInstance()->Hwnd(), &currentMouse);
			Vector2 newDistance = Vector2(currentMouse.x, currentMouse.y) - Vector2(mSelectedObject->X(), mSelectedObject->Y());

			float newLength = newDistance.Length();
			float scale = newLength / origLength;

			if (pressingRotation && GetAsyncKeyState(VK_RBUTTON) < 0)
			{
				scale *= -1;
			}

			if (scale != 0)
			{
				mSelectedObject->SetRotationAngle(mSelectedObject->GetRotationAngle() + (scale * 0.01));
				mSelectedObject->Update(0);
			}
		}

		if (GetAsyncKeyState('R') >= 0)
		{
			pressingRotation = false;
			mouseStartPos = Vector2(0,0);
		}
		else if (GetAsyncKeyState('R') < 0 && GetAsyncKeyState(VK_CONTROL) < 0)
		{
			// reset rotation to 0
			mSelectedObject->SetRotationAngle(0);
			mSelectedObject->Update(0);
		}
	}
}
开发者ID:jeromebyrne,项目名称:2DPlatformer,代码行数:58,代码来源:LevelEditor.cpp

示例6: CheckForCollisionBoxScaling

void LevelEditor::CheckForCollisionBoxScaling()
{
	if (mSelectedObject)
	{
		SolidMovingSprite * solidSprite = GetAsSolidMovingSprite(mSelectedObject);

		if (solidSprite)
		{
			static bool pressingCollisionScale = false;
			static Vector2 mouseStartPos = Vector2(0,0);
			static Vector2 originalDimensions = Vector2(0,0);
			if (!pressingCollisionScale && GetAsyncKeyState('C') < 0 && GetAsyncKeyState(VK_LBUTTON) < 0)
			{
				pressingCollisionScale = true;

				POINT currentMouse;
				GetCursorPos(&currentMouse);
				ScreenToClient(DXWindow::GetInstance()->Hwnd(), &currentMouse);
				mouseStartPos = Vector2(currentMouse.x, currentMouse.y);

				originalDimensions = Vector2(solidSprite->CollisionDimensions().X, solidSprite->CollisionDimensions().Y);
			}
			else if (pressingCollisionScale && GetAsyncKeyState(VK_LBUTTON) < 0)
			{
				// get the initial distance between the game object and the mouse
				Vector2 origDistance = mouseStartPos - Vector2(mSelectedObject->X(), mSelectedObject->Y());

				float origLength = origDistance.Length();

				// now get the distance between the current mouse position
				POINT currentMouse;
				GetCursorPos(&currentMouse);
				ScreenToClient(DXWindow::GetInstance()->Hwnd(), &currentMouse);
				Vector2 newDistance = Vector2(currentMouse.x, currentMouse.y) - Vector2(mSelectedObject->X(), mSelectedObject->Y());

				float newLength = newDistance.Length();

				float scale = newLength / origLength;

				if (scale != 0)
				{
					solidSprite->SetCollisionDimensions(Vector3(originalDimensions.X * scale, originalDimensions.Y * scale, solidSprite->CollisionDimensions().Z));
					solidSprite->RecalculateVertices();
					solidSprite->ApplyChange(Graphics::GetInstance()->Device());
				}
			}

			if (GetAsyncKeyState('C') >= 0)
			{
				pressingCollisionScale = false;
				mouseStartPos = Vector2(0,0);
				originalDimensions = Vector2(0,0);
			}
		}
	}
}
开发者ID:jeromebyrne,项目名称:2DPlatformer,代码行数:56,代码来源:LevelEditor.cpp

示例7: ComputeSeperation

Vector3 CFSM::ComputeSeperation(vector<CFSM*> ListOfCharacters)
{
	Vector2 steer;
	steer.SetZero();

	int count = 0;

	for (int i = 0; i < ListOfCharacters.size(); ++i)
	{
		if (ListOfCharacters[i] != this && ListOfCharacters[i]->GetAlive())
		{
			float distance = (m_CurrentPosition - ListOfCharacters[i]->GetPosition()).Length();
			if (distance < m_seperationZone)
			{
				Vector3 DirectionCopy = m_CurrentPosition - ListOfCharacters[i]->GetPosition();
				Vector2 diff = Vector2(DirectionCopy.x, DirectionCopy.z);
				if (!diff.IsZero())
				{
					diff = diff.Normalized();
					diff.x /= distance;
					diff.y /= distance;
				}
				steer += diff;
				count++;
			}
		}
	}

	if (count > 0)
	{
		steer.x /= (float)count;
		steer.y /= (float)count;
	}

	if (steer.Length() > 0)
	{
		steer = steer.Normalized();
		steer *= m_MovementSpeed;
		Vector2 DirectionCopy = Vector2(m_CurrentDirection.x, m_CurrentDirection.z);
		steer -= DirectionCopy;

		if (steer.Length() >= m_maxforce)
		{
			steer *= (m_maxforce / steer.Length());
		}
	}

	return Vector3(steer.x, 0, steer.y);
}
开发者ID:pongtato,项目名称:AI_ASGN2,代码行数:49,代码来源:FSM.cpp

示例8: PopulateFrameData

void AnimationSkeleton::PopulateFrameData(unsigned int frame, list<AnimationSkeletonFramePiece> framePieces)
{
	mSkeletonLines[frame].clear();
	mSkeletonLines[frame].reserve(framePieces.size());

	for (auto & piece : framePieces)
	{
		AnimationSkeletonFramePiece internalPiece;
		internalPiece.mStartPos = piece.mStartPos;
		internalPiece.mEndPos = piece.mEndPos;

		Vector2 lineDirection = piece.mEndPos - piece.mStartPos;
		internalPiece.mLength = lineDirection.Length();

		if (internalPiece.mLength <= 0)
		{
			GAME_ASSERT(false);
			continue;
		}

		internalPiece.mLineDirection.X = lineDirection.X / internalPiece.mLength;
		internalPiece.mLineDirection.Y = lineDirection.Y / internalPiece.mLength;

		internalPiece.mNormal.X = -internalPiece.mLineDirection.Y;
		internalPiece.mNormal.Y = internalPiece.mLineDirection.X;

		mSkeletonLines[frame].push_back(internalPiece);
	}
}
开发者ID:jeromebyrne,项目名称:2DPlatformer,代码行数:29,代码来源:AnimationSkeleton.cpp

示例9: Separation

		// calculates a force repelling from the other neighbors
		Vector2 TikiSteering::Separation()
		{
			Vector2 separationForce = Vector2::Zero;

			tikiBot->GetGameState()->GetScene()->SceneGraph.DoWithinRange(tikiBot->Pos3D(), (float)tikiBot->BRadius() + 0.1f, [&](GameObject* go) 
			{

 				if (go != 0)
 				{
 					TikiBot *owner = tikiBot;
 
 					TikiBot* bot = 0;
 					bot = go->GetComponent<TikiBot>();
 
 					if (bot != 0 && bot->ID() != owner->ID())
 					{
 						Vector2 toBot = owner->Pos() - bot->Pos();
 
 						// scale the force inversely proportional to the agents distance from its neighbor.
 						separationForce += Vector2::Normalize(toBot) / toBot.Length();
 					}
 				}

			});

			return separationForce;
		}
开发者ID:IreNox,项目名称:tiki2,代码行数:28,代码来源:TikiSteering.cpp

示例10: Arrive

		Vector2 TikiSteering::Arrive(const Vector2& targetPos, const Deceleration decel)
		{
			Vector2 ToTarget = targetPos - tikiBot->Pos();

			//calculate the distance to the target
			float dist = ToTarget.Length();

			if (dist > 0)
			{
				//because Deceleration is enumerated as an int, this value is required
				//to provide fine tweaking of the deceleration..
				const float DecelerationTweaker = 0.3f;

				//calculate the speed required to reach the target given the desired deceleration
				float speed =  dist / ((float)decel * DecelerationTweaker);     

				//make sure the velocity does not exceed the max
				speed = MinOf(speed, (float)tikiBot->MaxSpeed());

				//from here proceed just like Seek except we don't need to normalize 
				//the ToTarget vector because we have already gone to the trouble of calculating its length: dist. 
				Vector2 DesiredVelocity =  ToTarget * speed / dist;

				return (DesiredVelocity - tikiBot->Velocity());
			}

			return Vector2::Zero;
		}
开发者ID:IreNox,项目名称:tiki2,代码行数:28,代码来源:TikiSteering.cpp

示例11: IsCollision

bool HitBox::IsCollision(Vector2 offset, HitBox *pHitBox, Vector2 hitBoxOffset, CollisionParameter *pParam) const
{
    bool isCollision = false;
    Vector2 overlapCompensation = Vector2(0, 0);

    if (pHitBox != NULL)
    {
        for (unsigned int i = 0; i < collidableObjectList.size(); i++)
        {
            CollidableObject *pCollidableObject1 = collidableObjectList[i];

            for (unsigned int j = 0; j < pHitBox->collidableObjectList.size(); j++)
            {
                CollidableObject *pCollidableObject2 = pHitBox->collidableObjectList[j];
                CollisionParameter tempParam;

                if (CollisionExists(pCollidableObject1, offset + overlapCompensation, pCollidableObject2, hitBoxOffset, &tempParam)
                    && fabs(tempParam.OverlapDistance) > 0.0001)
                {
                    for (unsigned int k = 0; k < tempParam.OverlapEntryList.size(); k++)
                    {
                        pParam->OverlapEntryList.push_back(tempParam.OverlapEntryList[k]);
                    }

                    overlapCompensation += tempParam.OverlapAxis * tempParam.OverlapDistance;
                    isCollision = true;
                }
            }
        }
    }

    pParam->OverlapDistance = overlapCompensation.Length();
    pParam->OverlapAxis = overlapCompensation.Normalize();
    return isCollision;
}
开发者ID:mbasaglia,项目名称:my-little-investigations,代码行数:35,代码来源:Collisions.cpp

示例12: Math_Intersect

bool Math_Intersect(const Circle& c, const LineSegment& l)
{
	Vector2 startToCenter = c.center - l.from;
	Vector2 startToEnd = l.to - l.from;
	float len = startToEnd.Length();
	Vector2 dir = startToEnd / len;

	// Find the closest point to the line segment
	float projection = Math_Dot(startToCenter, dir);
	Vector2 closestPoint;
	if (projection > len)
	{
		closestPoint = l.to;
	}
	else if (projection < 0.0f)
	{
		closestPoint = l.from;
	}
	else
	{
		closestPoint = l.from + (dir * projection);
	}

	// Check if the closest point is within the circle
	Vector2 closestToCenter = c.center - closestPoint;
	if (closestToCenter.LengthSquared() > c.radius * c.radius)
	{
		return false;
	}
	return true;
}
开发者ID:NGAEVA,项目名称:2Dgame,代码行数:31,代码来源:SGE_Math.cpp

示例13: v

void	Move::OnUpdate(float elapsedTime)
{
    EntityPtr e = GetEntity();

    float ds = 1.0f;

    Vector2 v(cos(mAngle) * mSpeed * ds , sin(mAngle) * mSpeed * ds);

    b2Body* body = ((Box2DBody*)e->GetBody())->GetBox2DBody();

    //body->SetLinearVelocity(v);

    v.x *= body->GetMass() * 10;
    v.y *= body->GetMass() * 10;
    body->ApplyForce(v, e->GetPosition());

    Vector2 cv = body->GetLinearVelocity();
    float fv = abs(cv.Length());
    if (fv > abs(mSpeed) && fv > 0.00001)
    {
        cv.x *= abs(mSpeed) / fv;
        cv.y *= abs(mSpeed) / fv;
        body->SetLinearVelocity(cv);
    }
}
开发者ID:weimingtom,项目名称:isilme,代码行数:25,代码来源:Move.cpp

示例14: Simulate

void SpringSystem::Simulate(float dt) {

    for (auto object : Objects()) {

        Spring* spring = object->GetComponent<Spring>();
        if (!spring->particleA) continue;
        if (!spring->particleB) continue;
     
        Vector2 delta = spring->particleB->position - spring->particleA->position;
        
        spring->currentLength = delta.Length();
        float inverseLength = 1.0f/spring->currentLength;
        
        delta.x*=inverseLength;
        delta.y*=inverseLength;
        
        float totalMass = spring->particleA->mass + spring->particleB->mass;
        
        float m1 = spring->particleA->mass / totalMass;
        float m2 = 1.0f - m1;
        
        float displacement = (spring->currentLength - spring->length) * spring->elasticity;
        float dLength = displacement * dt;
        
        spring->tension += dLength;
        
        if (!spring->particleA->immovable) {
            spring->particleA->position += delta * dLength * m2;
        }
        if (!spring->particleB->immovable) {
            spring->particleB->position -= delta * dLength * m1;
        }
    }
}
开发者ID:JeppeNielsen,项目名称:PocketEngine,代码行数:34,代码来源:SpringSystem.cpp

示例15:

Real TCBSpline2<Real>::GetSpeedKey (int key, Real t) const
{
    Vector2<Real> velocity = mB[key] + t*(mC[key]*((Real)2) +
        mD[key]*(((Real)3)*t));

    return velocity.Length();
}
开发者ID:Kiichi77,项目名称:WildMagic,代码行数:7,代码来源:Wm5TCBSpline2.cpp


注:本文中的Vector2::Length方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。