Jump to content

want to make a horizontal compass


havenphillip
 Share

Recommended Posts

I can't tell from your post if you have a horizontal compass working or not, which is somewhat independent from finding the angle between a player and a target in relation to the player's direction. If you are asking to get the horizontal compass working, calculate the relative angle, and also make the target show up on the compass as well, then this will be a fairly large discussion. And it's another reason why the techass forum is garbage, if this will be a lengthy discussion about multiple things regarding drawing a hud, calculating the relative angle, etc instead of one just concise question about one facet.

In any case, if you are just wanting to calculate the relative angle, then you have to calculate the angle between the player and the target based on their global positions, determine the Y-rotation of the player, and then subtract the two.

Example script:

Script.player = "" --entity "Player Entity"
Script.target = "" --entity "Target Entity"
Script.angle = 0

function PerfectAngle(pos1,pos2)
	local dx, dz
	dx = pos2.x - pos1.x
	dz = pos2.z - pos1.z
	return Math:ATan2(dx,dz)
end

function NormalizeAngle(angle)
	if angle <= 0 then
		return angle + 360
	else
		return angle
	end
end

function Script:UpdateWorld()
	self.perfectangle = PerfectAngle(self.player:GetPosition(true),self.target:GetPosition(true))
	self.angle = self.player:GetRotation(true).y - self.perfectangle
end

function Script:PostRender(context)
	context:SetBlendMode(Blend.Alpha)
	context:DrawText(string.format("Angle between Player to Target: %.0f",NormalizeAngle(self.angle)),2,22)
	context:SetBlendMode(Blend.Solid)
end

Place a pivot into a scene and attach this script. Drag the Player and Target entities into the Script's Property dialog and run the game.

SetPlayerTarget.jpg.ee220d8cbe7754fd3ca1dd50d04aa5c5.jpgangle.thumb.jpg.e98fc300c25a452bc04b557c24bd27bd.jpg

  • Upvote 3

Win7 64bit / Intel i7-2600 CPU @ 3.9 GHz / 16 GB DDR3 / NVIDIA GeForce GTX 590

LE / 3DWS / BMX / Hexagon

macklebee's channel

Link to comment
Share on other sites

Oh awesome! That's exactly what I was looking for. I only need to do this and some PostRender() stuff but this is right.

function NormalizeAngle(angle)
    if angle <= - 180 then
        return angle + 360
    elseif angle >= 180 then
        return angle - 360
    else
        return angle + 0
    end
end

Here it is. Working perfectly:
 

UI Elements.zip

  • Upvote 1
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...