 ## Ambiera Forum

Discussions, Help and Support. Ambiera Forum > CopperCube > Help with CopperCube How to get angle between 2 object GuestGuest Quote 2022-10-13 12:38:44

I want to create Doom style sprite. How do we get the angle between the enemy facing direction and the player position in JavaScript? Thank RoboGuest Quote 2022-10-14 08:12:43

If you want the sprites to always face the player simply use the inbuilt billboard - which will always face the player. GuestGuest Quote 2022-10-14 09:51:21

Thank, but I also need to view it form different angle (8 direction), so probably need some math here.

I have take a look at "8 direction sprite" action by Just in case and it's seem what I need, but it work a little weird, maybe need a little tweek.

Also "look at target" behaviour does have some math about angle and rotation. okeokeRegistered User Quote 2022-10-15 09:03:03

I think you just need a normilized angle between camera and your enemy rotations.
So you get rotations first:
var camRot = ccbSetSceneNodeProperty(camNode, 'Rotation');
var enemyRot = ccbSetSceneNodeProperty(enemyNode, 'Rotation');

then calculate the difference. since y axis is up you only need it:

var diff = enemyRot.y - camRot.y;

//normilize, so it's always between 0-359 degress:
diff = (diff % 360 + 360) % 360;

//now you pick an animation based on this diff value:

if (diff >= 330 || diff < 30) { // .. forward }
if (diff >= 30 || diff < 60 ) { //.. right-forward } and i.e. GuestGuest Quote 2022-10-15 13:30:32

Thank, I'll give it a try. SamGradyGuest Quote 2022-10-15 17:55:35

Use Pythagoras' theorem for find angle between two points, i always use it and this theorem help find distance to. samgradyRegistered User Quote 2022-10-15 21:23:31

so i create function like this:

`function findAngle(x1,z1,x2,z2){  var A = (x2 - x1);  var B = (z2 - z1);  var angle = Math.atan(A / B);  if(B < 0) angle = angle + Math.PI;  return angle * 57.29; }`

or

`function findAngle(x1,z1,x2,z2){  if((z2 - z1) < 0) return (Math.atan((x2 - x1) / (z2 - z1)) + Math.PI) * 57.29;  else return Math.atan((x2 - x1) / (z2 - z1)) * 57.29;}`

i know this code based on 2-dimensional space but it's totally do what i need, return angle between 2 object, i made it by myself so you can find some better way.

"Thank, but I also need to view it form different angle (8 direction), so probably need some math here."

you can setup direction of enemy then check angle to player and change texture to one of your 8 image

P.S.
there is one drawback, the angle is returned in the range from -90 to 270
`angle = angle * 57.29;if((angle ) < 0) angle = 360 + angle ;return angle ;`

add this and from 0 to 360

 Posted by: (you are not logged in) Enter the missing letter in: "Internat?onal" (you are not logged in) Text:

### Possible Codes

 Feature Code Link [url] www.example.com [/url] Bold [b]bold text[/b] Image [img]http://www.example.com/image.jpg[/img] Quote [quote]quoted text[/quote] Code [code]source code[/code]

### Emoticons             