Author Topic: Setting an object directly right of a player  (Read 808 times)

No matter how the player turns, it'll stay to it's right. I need the equation because I am bad at math, so can anyone tell me how I would do this? thanks.

%player.getForwardVector() returns a length 1 vector along the XY axis in the direction the player is facing.
%player.getEyeVector() returns a length 1 vector in the direction their head is facing. (including looking up, to the side if they use free-look)

Here you go:
Code: [Select]
function getDirectRightPoint(%player,%mag)
{
%curAim = %player.getEyeVector();
%curX = getWord(%curAim,0);
%curY = getWord(%curAim,1);
%curZ = getWord(%curAim,2);
%theta = mRadToDeg(mATan(%curY,%curX));
if(%curX > 0 && %curY > 0)
%angle = %theta;
else if(%curX < 0 && %curY > 0)
%angle = 180 - %theta;
else if(%curX < 0 && %curY < 0)
%angle = %theta + 180;
else if(%curX > 0 && %curY < 0)
%angle = 360 - %theta;
else if(%curX == 0)
{
if(%curY > 0)
%angle = 180;
else if(%curY < 0)
%angle = 360;
else
return false;
}
else if(%curY == 0)
{
if(%curX > 0)
%angle = 90;
else if(%curX < 0)
%angle = 270;
else
return false;
}
%rAngle = %angle - 90;
if(%rAngle < 0)
%rAngle += 360;
%rightPos = %mag * mCos(%rAngle) SPC %mag * mSin(%rAngle) SPC %curZ;
return vectorAdd(%rightPos,%player.getPosition());
}
This code returns a point at a ninety degree angle to where the player is currently looking, presumably to place a brick. By "directly right" I assumed you meant a right angle. The arguments are %player for the player object and %mag, for the distance from the player the object will be.

Important: This code has the point placed at the same height as before. This won't be a problem unless the object you are talking about is way above the player. The object will be at 90 degrees on the XY plane, but when the player leans back they can see the object in periphiral vision. If this is the case, I'll need to append some stuff to the code, but the situation would technically become a bit more complicated than just "to the right of".
« Last Edit: May 12, 2012, 11:47:06 AM by Treynolds416 »

Lol thanks, I should of paid attention more in my math classes...

Oh, I misunderstood the question. The cross product of two vectors is one perpendicular to both, so that of the forward vector and 0 0 1 would be to one side of the player - I don't know which side but should be the same right/left hand each time and easier than the trigonometry.

vectorCross("0 1 0","0 0 1") = "1 0 0" (+Y and +Z -> +X)
vectorCross("1 0 0","0 0 1") = "0 -1 0" (+X and +Z -> -Y)

vectorCross("0 0 1","0 1 0") = "-1 0 0" (if you wanted to generate the opposite hand direction, swap the arguments or scale the vector by -1)
« Last Edit: May 08, 2012, 03:59:24 PM by Space Guy »

Oh, I misunderstood the question. The cross product of two vectors is one perpendicular to both, so that of the forward vector and 0 0 1 would be to one side of the player - I don't know which side but should be the same right/left hand each time and easier than the trigonometry.

vectorCross("0 1 0","0 0 1") = "1 0 0" (+Y and +Z -> +X)
vectorCross("1 0 0","0 0 1") = "0 -1 0" (+X and +Z -> -Y)

vectorCross("0 0 1","0 1 0") = "-1 0 0" (if you wanted to generate the opposite hand direction, swap the arguments or scale the vector by -1)
I figured there was a better way. The if statements are easier to understand, though, which might benefit elm more.

Thanks spaceguy, i'll try that out later.


Did you actually write that code, or was that adapted from something?

If you wrote it, I have a new found respect for you. Hell if I understand trig of that level.

Did you actually write that code, or was that adapted from something?

If you wrote it, I have a new found respect for you. Hell if I understand trig of that level.
I actually did write it <3
Logic/math is one of the few things I'm good at, and manipulating bricks through script is the second best.