How do I get a PShape location after transforming it?

cameraTarget = createShape(SPHERE, 30);
cameraTarget.rotateX(-mouseX / 200.0);
cameraTarget.rotateY(-mouseY / 200.0);
cameraTarget.translate(200, 0, 0);

I’m making a project and don’t want to replace any code.
I was wondering how I can get the Pshape: cameraTarget’s location?

I’m guessing it’ll look something like this:
float X = cameraTarget.positionX;
float Y = cameraTarget.postionY;

If anyone knows a solution then please help, I’m pretty new to prosessing 4 java script

2 Likes

Hi @TT3overnot,

Welcome to the forum! :wink:

Not replacing code is going to be difficult ahah!

More seriously if you check the reference for the PShape class, you can see all the methods associated to it.

You could use getVertex() to get the position of a vertex in space and if you want the center of the shape, average those locations but the issue is that the PShape stores its transforms in a matrix (PMatrix) and it doesn’t seem you can get the matrix and extract the translation…

An easy solution is to track yourself the location of the shape by applying matrix transformations on an origin point.

1 Like

How exactly do I do that?

Mmh good question :wink:

I know that you can use a PMatrix object but it’s not very well documented except this Java doc.

You can use it like this:

// Create a new matrix
PMatrix matrix = new PMatrix3D();

// Apply your transformations just like PShape
matrix.translate(200, 0, 0);
matrix.rotateX(HALF_PI);

// Will store the position
PVector position = new PVector();

// Multiply an origin point (if your sphere was at 50, 50, 0)
// and apply the matrix transformations on that point
// store it in the position PVector
matrix.mult(new PVector(50, 50, 0), position);

println(position);
// -> [ 250.0, -2.1855694E-6, 50.0 ]

Maybe there’s a better way to get the PShape location but I think this works :wink:

2 Likes

Works well! But how do I get the X, Y and Z to their own variables?
So it would say
“250.0
-2.1855694E-6
50.0”

Rather than “[ 250.0, -2.1855694E-6, 50.0 ]”

The result of the mult operation is an instance of the PVector class which stores a vector.

You can access the individual coordinates since they are attributes:

PVector position = new PVector();
println(position.x, position.y, position.z);
1 Like

My problem is solved, thank you so much friend!

1 Like