# How do I work out one object's relative position to another?

I’m pretty sure you can do something like… multiply one object’s coordinate space by the other, and then see what it’s position is… but I can’t quite remember or seem to work out what to google let alone the best blueprint nodes to use!!

How would I work out a characters relative position to a cube in the level for example?

To elaborate, in the following picture (sorry about the quality…) I want to find the players (P’s) RELATIVE y offset. RELATIVE to the cube’s own coordinate system and rotation. I can’t do it in world space because no matter what orientation I place the box I want to get left and right across it as a y value.

Hey there im not quite sure if i get you right but could it be that you are looking for Get Actor Location, which gets you the LocationVector of your desired object?

If you mean with relative Position between Character and cube the vector between them you could substract bot actor location and its returning the vector. F.e. From Cube(Point A) to Character(Point B) you could substract B-A and you get the direction vector (DirV). The relative positionnow would be: PointA + DirV = PointB

Was that you intention?

kind regards

I want to translate one object into the coordinate space of the other.

I found this solution to my problem. In this graph I’m getting the location of the actor (1.) relative to the position of 2. I would still like a more flexible solution involving two transforms.

3 Likes

It’s not a problem specific to computer graphics, it’s a matrix multiplication issue. Anytime you describe something’s transform with a matrix, you can do this. it’s very simple, I think you just multiply one by the other or something. I just can’t remember how.

In computergraphics there is a thing called modelviewmatrix (f.e. in OpenGL) and if you draw an object you transform from the current matrix with a x,y,z transformation. Then you can overlay the current "world"matrix with a new one, which has the latest transformation/rotationmatrix as root.

F.e. you first place a cube somewhere in world space, there is the first transformation worldcoordroot + cube location = new location in world root. Overlay this and everything drawn next will be relative to new location + new drawn object = next new location.

When i got you right, you want to set f.e. the players location with x,y,z added from the cubes matrix transformation right?

To do this i would get the baseobjects transform as base. From this point you can do any kind of math operation and the result seems to have the relation you wish.

The trick is: Draw the first object (or spawn or whatever), this is your base and set the next object which shall behave relative to your base to the exact same position as the base and then add the additonal transformation. However this is how it works for me when i did play around with computergraphics

maybe this helps or maybe i still got you wrong

oh sorry i mixed the overlay behaviour.

Actually if you spawn something into the world this will alter the modelview, so if you spawn the next it takes the altered modelviewmatrix as base.

I assume to work correctly inside the editor, each time an object is put into the world there is an overlay of the worldmatrix (PushMatrix) and after that there is a (PopMatrix) this results that every drawn object is relative to the worldcoordinate space.

To be honest i really would like to know how epic does this magic for real, because i dont know if im right with this.

However i dont think set up a new coord space is not the right thing to do here

best regards

like that?

Yes I think so! but what’s the algebraic shorthand? and how would I achieve this in blueprints? Thanks so much for your help.

ill try it with that, and sorry i cant test that right now

"To elaborate, in the following picture (sorry about the quality…) I want to find the players (P’s) RELATIVE y offset. RELATIVE to the cube’s own coordinate system and rotation. I can’t do it in world space because no matter what orientation I place the box I want to get left and right across it as a y value. "

Inside the Player you get a reference to your cube, i think like you did in the picture before.

Call GetActorLocation from Cube (Vector 1)
Call GetActorLocation of your PlayerActor (Vector 2)

Then substract them. Vector2 - Vector 1 → and you will get a Vector which has the direction Cube to Player
That means break down to the y value of that vector you would have the amount of y units you need to go to get to the player´s y-origin position (the mentioned offset)

Otherwise substracting Vector 1 - Vector2 and break down the result to y component you will get the y value of how many y units you need to go to be at cube´s y-origin position

maybe that helps

Hey, i see this question is still open. Is your problem solved? Did my answers help you out? Or do you still have some problems with this?

best regards

that still sounds like you are in world space with that solution, not in the local coordinate space of the box, like the question wanted.

you used 2 positions as inputs, without using the rotation of the box at all. so you didn’t use enough information to transform into the box’s local coordinate space. before you can subtract those vectors, you need to rotate the players position around the box, with an equal and opposite, “inverse rotation” of the box.

im sure there is a way to do it from scratch, using nothing more than a mess of "sin(yaw) * cos(roll) " nodes, but without blueprints supporting matrices, you might want to do this in c++.

another way to state the problem from the box’s view, is:
you want to rotate the players position around the box, with an opposite rotation than the box, then subtract the vectors to find the distance and take its Y component as your answer.

to do that rotation, usually you multiply by the inverse of a rotation matrix for each axis of rotation, in the same order the engine uses to rotate the cube. if you don’t use the same order, gimbal lock can throw the cube’s rotation and the resulting data out of sync.

a hacky solution might be to have an invisible actor that you move to your player and parent to your cube, then you find its relative location to its parent, and take the Y component as your answer. to constantly update this, you would have to (unparent, move, parent, test) for every update, but if you only need this info rarely, than this hack might work.

imagine trying to do this kind of math in blueprint, with nothing but basic trigonometry nodes: