Loading...
Logo
Processing Forum
Hey Guys

I am kind of stuck I am trying to get a 3d rectangle to rotate in order to face a specific x, y, z coordinate. (Note: This x, y and z coordinate changes as the simulation runs)

I have already looked at  http://www.andrewnoske.com/wiki/index.php?title=Computer_graphics_-_common_problems which shows how to calculate the necessary rotation in order for the 3d object to face a target but this is using C++ where an object is rotate around its centre.

In processing this is not the case as using rotateX() . .  rotates the whole grid. So my question is:

Is there a simply way to rotate an object to a specific direction given the coordinates of another target object?
or
Is there some way to adapt the above values from the link which are used to rotate an object around its centre so that they can work in processing where the rotation changes the whole grid.

Sadly my math is not what it used to be.

Any help would be appreciated.
Thanks!

Replies(5)

When you post in Processing Implementations, you should indicate which language you target... A solution in JRuby can look quite different from one in Jython or even in Processing.js!
Hi
I can help with the maths, but the website you link to says everything I would have said (except I'd've added diagrams).
Do you understand processing's rotate() enough to achieve your aims in 2D? If not, try that first. And say if you want the maths expanding.
string
PS I winced at "3D rectangle" - assume you mean something like a playing card.
I just can give you the code for 2D but with help of the website posted above you should be able to adapt it:

Copy code
  1. // size of your rectangle
  2. int s = 10;
  3. PVector p, t;

  4. void setup() {
  5.   size(400, 400, P3D);
  6.   p = new PVector();
  7.   t = new PVector(width / 2, height / 2);
  8. }

  9. void draw(){
  10. background(0);
  11. noStroke();
  12. p.set(mouseX, mouseY, 0);
  13. float angle = atan2(p.y - t.y, p.x - t.x);
  14. pushMatrix();
  15.     translate(p.x, p.y, p.z);
  16.     rotate(angle);
  17.     rect(0, 0, s, s);
  18. popMatrix();

  19. ellipse(t.x, t.y, 10, 10);
  20. }
> In processing this is not the case as using rotateX() . .  rotates the whole grid.

not true, it only rotates the stuff you draw after applying the rotate. and you can use pushMatrix() and popMatrix() to stop it affecting anything else. see the reference (and above code)

also, rotating an object around its centre requires you to translate it to the origin before, rotate it, then translate it back to where it was (or, more simply, apply the rotation, draw the shape *at the origin*, translate into place)


Whoa, dude, let's not confuse him even further.
 
It's translate, rotate, and then draw "at the origin".
 
Copy code
  1. void setup() {
  2.       size(400, 400);
  3.       smooth();
  4.       rectMode(CENTER);
  5. }
  6. void draw() {
  7.       pushMatrix();      
  8.       translate(width/2, height/2);
  9.       rotate(QUARTER_PI);
  10.       rect(0, 0, 50, 50);      // Draw shape at "origin"
  11.       popMatrix();
  12. }
Personally, in 2D, I prefer resetMatrix() when I'm just translating from the original origin over and over again. No need to "push" anything.
 
This tutorial might help: 2D Transformations.