Forum

> > CS2D > Scripts > [Lua Question] Calculate Aim Angle
Forums overviewCS2D overview Scripts overviewLog in to reply

English [Lua Question] Calculate Aim Angle

10 replies
To the start Previous 1 Next To the start

old [Lua Question] Calculate Aim Angle

EngiN33R
Moderator Off Offline

Quote
So I have a question. For example, I have two objects, one being P (player) and second being O (object). I want to know the angle the player has to aim to look at the center of the object. Please, if you can - just write the function, I'll reverse engineer it later.

old Re: [Lua Question] Calculate Aim Angle

Starkkz
Moderator Off Offline

Quote
I use a function called FindAngle that i done i some mins. (Learning how it works)

1
2
3
4
5
6
7
8
9
10
function FindAngle(fx,fy,tx,ty)
	-- Angle found
	local Angle = math.deg(math.atan2(fy-ty,fx-tx) - (math.pi/2))

	-- Fix the Angle
	if Angle < -180 then Angle = Angle + 360 end

	-- Return it
	return Angle
end

It returns the angle using the CS2D Rotation in degrees, (-180 to 180)

And the positions are in pixels. (I didn't test if works with tiles, i think yes)

old Re: [Lua Question] Calculate Aim Angle

Lee
Moderator Off Offline

Quote
Meh, tiles kind of do matter if you want the center of the other player. If for example, a player is touching only 17 pixels of one tile and 15 pixels of another, then the arctan will return the angle to the center of the tile rather than the player.

old Re: [Lua Question] Calculate Aim Angle

EnderCrypt
User Off Offline

Quote
yes, but we talk about using tile x,y or using player x,y

its like if i used x: 1000 and y: 1000 (player position)
and
its like if i used x: 10 and y: 10 (a tile position)

both calculations would be correct, just return different result

and ofcoutrse it would return center of tile, as you then use tile in calculation, not x y of player

old Re: [Lua Question] Calculate Aim Angle

Lee
Moderator Off Offline

Quote
Exactly, the tile is a 32x32 square, meaning that there's an upper bound of 32*1.4 degrees of error in the calculation. In other words, if you want to have the player within the calculation, but you use the tile information, assuming that you scale everything to proportion, then there's only a 1 in 45.25th chance that the calculation will be deterministically the same as what you are looking for...
To the start Previous 1 Next To the start
Log in to reply Scripts overviewCS2D overviewForums overview