[SOLVED] How do I find the angle between 2 positions (radians)

Hello again,

Anyone know how I could find the angle between the positions of 2 entities? The goal of this for me is to find the positions of these 2 places, create an angle (in radians) of these places, and then apply a force (using p2.js’s applyForce() method) and then have one of the entities get sent flying (lol).

Anyone have any idea on how to do this?

USING: p2.js (NOT ammo.js)
WORKS?: YES (almost there now :D)

IMPORTANT NOTE: I know I said angle between positions, but I need this angle to work in an x and y interface (meaning there will be 2 values I have to get, so I think the answer might have to do with positions between x values and positions between y values respectively)

Chhes :cheese:

In order to solve this, @yaustar was able to find a piece of code using sub2 which got the Vec3 I needed to use in order to have the direction.x and direction.y for the applyForce() method to have it implemented. If this solution does not satisfy the reader please either:

  • read the comments
  • reply for some help, ill be glad to share what yauster helped me with!

Hey, not sure what you mean by two angles, for getting the angle between two positions check the following thread, it may be of help:

yea I already checked that, thanks for that advice, but if you remember the

I want this type of angle for my code except I can get that same TYPE of angle for 2 positions, instead of using up.x and up.y (I would want something like angle.x and angle.y)

Can you draw a diagram to explain this please? It’s impossible calculate an angle between two positions because they are just points.

Did you mean an angle between two vectors?


From what I remember from my uni math days, you need to look up the dot and cross product - give that a google!

Having said that, the engine probably abstracts this away with some convenience function, but I’m not sure what those are.

This is a diagram of what I need, thanks for your help so far though @Leonidas and @yaustar

Chhes :cheese:

Where’s the angle on the vectors that you would like to calculate?

yauster I do not know if the values I am looking for are even angles, I just need to find the positioning where I can make an entity go flying when hit, so if you know what the this.entity.forward does, I am looking for values SIMILAR to this, but NOT based on an entity looking up. I am looking for a value similar to that, but between two entities.

I’m not sure what logic you want to work out here?

What do you mean by ‘between two entities’? The purple dots? What is important between the two entities?

Where do the vectors in the diagram comes from?

Can you explain what the original problem is and strep through the logic/behaviour that you want?

Alright, sorry for my bad explaination. Lets start at the beginning

I have a player, and an item. When this item is used, and the player is touching this item (code for this works), then the item will fling the player based on their positions. the purple dots in the diagram are basically the positions of an item, and a player. Pretend those are the 2D vectors of the item and player, and I need to be able to launch the player away from the item (note that this is NOT the facing angle of the item, as some items may be attacking from behind). This means that based on the position of the item, and the player, the player gets knocked away from the item based on its position.

A player is at 30 degrees from an item. the item hits the player, and now at 30 degrees the player gets launched AWAY from the item. This would be the knockback that I am going for.

**The way that I want this coded is NOT with an actual angle, because I need to have an x and y force applied on the player. Here is my DASH code (NOT the knockback) which is an example of the TYPE of script I am using.

this.entity.script.p2Body.body.applyForce(this.entity.up.x * 50, this.entity.up.y * 50);

For knockback, I will be using this applyForce() method since it works very well for the type of knockback I am aiming for.

As for the TYPE of values I am looking for, I am not ENTIRELY sure, but I know I am looking for values similar to those of this.entity.up because those values are able to launch the player with an apply force by applying different forces to the X and the Y. I need to have TWO values (one for the X, and one for the Y) which both are multiplied by knockback values I have tested in order to launch the player at a given direction.

Lets answer your questions now, ‘between two entities’ basically means the direction in terms of x and y from one of the purple dots to another.
The vectors in the diagram are just examples of where the player and item could be situated, and if that item hits the player then the player will go flying in the direction away from the item, BASED on the ‘between two entities’ explained above.
The problem has been explained above, and please ask me if you need more help, I know helping people on this forum is a challenge, so be sure to ask for more and more details if you still require more. Sorry for not being clear enough.

Chhes :cheese:

To clarify

The orange object is moving towards the red object and when it collides, you want the red object to fly off in the direction of the collision?

Well, you are VERY close, I created an animation demo to show you in order to clear this confusion.

NOTE: I am pretty sure the physics are VERY similar to what you described in your image compared to the animation I am showing you, I think the main difference is the way they collide.

Please, just ask if you need more details, I am happy to provide more and more information as necessary

Thanks so much,
Chhes :cheese:

In the animation, what are you expecting to happen? The item hits the red object and the red object to fly down?

Well, in the animation here is what happens in a break down:

  • item collides with blue player
  • red player’s controller presses left mouse button (use item)
  • item plays animation AND any blue players touching it get launched
  • the angle of the launch is based on the positions of the two object (for example, if the blue player was not in this perfect situation shown above, but instead a little to the right of the item, then the blue player would be launched slightly to the right for its angle)

If you need more details just ask, happy to give more

Chhes :cheese:

So the angle is based on the item and the blue player?

In which case, it sounds like

normalise (bluePlayerPos - itemPos)

To get the direction as a unit vector for the launch direction

alright, I happen to have no idea how to write this, could you explain how I could get the:

  • normalize: x
  • normailize: y

Thank you so much!
Chhes :cheese:

var direction = new pc.Vec3();
direction.sub2(playerEntity.getPosition(), itemEntity.getPosition());
direction.z = 0; // assuming the game is played on the XY plane

Untested code but you get the idea.

1 Like

so wait, does this give me 2 values?

Chhes :cheese:

No, it gives you a pc.Vec3 and you can get the x, y, z components

do you think I will be able to do this with a p2Body.body.position?

Or would it be better to do in a way you have shown?