Jump to content

Look at the cursor [Top-down view][+Code]


DWORD
 Share

Recommended Posts

Hello. I'm new with LW engine(and here).

 

I'm just messing around with the engine to learn. I just built a top-down (simple) game with a player that look at the cursor but isn't accurate for isometric views( is perfectly good in 2.5d shooters and top-down view).

 

 



               Engine engine;
               Mouse m;

               float res = 0;

               int xw = 0, yh = 0, result = 0;

               //X
	if(m.GetX() > engine.GetWidth()/2)
	{
		xw = -((engine.GetWidth()/2)-m.GetX());
	}
	else
	{
		if(m.GetX() < engine.GetWidth()/2)
		{
			xw = -((engine.GetWidth()/2)-m.GetX());
		}
			else
		{
			xw = 0;
		}
	}

	//Y
	if(m.GetY() > engine.GetHeight()/2)
	{
		yh = ((engine.GetHeight()/2)-m.GetY());
	}
	else
	{
		if(m.GetY() < engine.GetHeight()/2)
		{
			yh = ((engine.GetHeight()/2)-m.GetY());
		}
		else
		{
			yh = 0;
		}
	}


               //Set the angle
	if(yh > 0 && xw == 0)
	{
		result = 90;
	}
	else
		if(yh < 0 && xw == 0)
		{
			result = 270;
		}
		else
			if(xw > 0 && yh == 0)
			{
				result = 0;
			}
			else
				if(xw < 0 && yh == 0)
				{
					result = 180;
				}
				else
					if(xw == 0 || yh == 0)
					{
						result = 0;
					}
					else
					{
						res =  (float)yh/xw;
						result = (atan(res) * 45) / 0.785398163;
					}

               //Correct the angle
	if(result < 0)
	{
		result = -result;
	}

	if(xw < 0 && yh > 0)
	{
		result = (90-result)+90;
	}

	else
		if(xw < 0 && yh < 0)
		{
			result = 180+result;
		}
		else
			if(xw > 0 && yh < 0)
			{
				result = (90-result)+270;
			}

 

As you see is really messy(the rest of the code is worse :)). To use this you have to set rotation of your entity(Y axis) to "result" like this "YourEntity.SetRotation(Vec3(0,result,0));". So have another way to do this?

 

Thanks. Sorry about my english.

Link to comment
Share on other sites

I'm not sure to have understood correctly your question, but if you have to face a model towards another one I think the easier way is with PointEntity?

 

You may create a Pivot and move it together with your mouse move coordinates (may be X and Z), then call PointEntity from your character mesh to the pivot.

  • Upvote 2

?? FRANCESCO CROCETTI ??

http://skaredcreations.com

Link to comment
Share on other sites

I'm not sure to have understood correctly your question, but if you have to face a model towards another one I think the easier way is with PointEntity?

 

You may create a Pivot and move it together with your mouse move coordinates (may be X and Z), then call PointEntity from your character mesh to the pivot.

 

Really thanks, i didn't knew about this Pivot

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...