Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3215984
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T15:14:30+00:00 2026-05-17T15:14:30+00:00

I am trying to rotate vectors using matrices, but got confused. I thought all

  • 0

I am trying to rotate vectors using matrices, but got confused.

I thought all I needed to do is create a rotation matrix and multiply it to a vector to get the rotated vector.

Here you can see a simple test I’ve done using Processing:

processing text

Use a to increment rotation, and x/y/z to changes the axis.

And here is source:

PVector[] clone,face = {new PVector(50,0,50),new PVector(-50,0,50),new PVector(-50, 0, -50),new PVector(50, 0, -50)};
color faceC = color(255,0,0),cloneC = color(0,255,0);
float angle = 90;
PVector x = new PVector(1,0,0),y = new PVector(0,1,0),z = new PVector(0,0,1), axis = x;
PFont ocr;

void setup(){
  size(400,400,P3D);
  strokeWeight(1.1610855);smooth();
  clone = rotateVerts(face,angle,axis);
  ocr = loadFont("ocr.vlw");
}
void draw(){
  background(255);
  fill(0);textFont(ocr,10);text("a = increment rotation\nx/y/z = change axis\nrotation: " + angle + "\naxis: " + axis,10,10,200,200);fill(255);
  translate(width*.5,height*.5);
  rotateX(map(mouseY,height*.5,-height*.5,0,TWO_PI));
  rotateY(map(mouseX,0,width,0,TWO_PI));
    drawQuad(face,faceC);
    drawQuad(clone,cloneC);
  stroke(128,0,0);line(0,0,0,100,0,0);stroke(0,128,0);line(0,0,0,0,-100,0);stroke(0,0,128);line(0,0,0,0,0,100);
}
void keyPressed(){
  if(key == 'a') angle += 15;
  if(angle > 360) angle -= 360;
  if(key == 'x') axis = x;
  if(key == 'y') axis = y;
  if(key == 'z') axis = z;
  clone = rotateVerts(face,angle,axis);
}
PVector[] rotateVerts(PVector[] verts,float angle,PVector axis){
  int vl = verts.length;
  PVector[] clone = new PVector[vl];
  for(int i = 0; i<vl;i++) clone[i] = PVector.add(verts[i],new PVector());
  //rotate using a matrix
  PMatrix3D rMat = new PMatrix3D();
  rMat.rotate(radians(angle),axis.x,axis.y,axis.z);
  for(int i = 0; i<vl;i++) rMat.mult(clone[i],clone[i]);
  return clone;
}
void drawQuad(PVector[] verts,color c){
  stroke(c);
  beginShape(QUADS);
    for(int i = 0 ; i < 4; i++) vertex(verts[i].x,verts[i].y,verts[i].z);
  endShape();
}

Processing comes with a PVector and PMatrix3D which I’ve used.

Am I not using PMatrix they it should be used or is this a bug ?
Any hints ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T15:14:30+00:00Added an answer on May 17, 2026 at 3:14 pm

    This is the culprit:

    rMat.mult(clone[i],clone[i]);
    

    this is not safe to do, because unlike in C or JAVA, the source is changing as the operation proceeds.

    changing the end of the function to:

    PVector[] dst = new PVector[vl];
    for(int i = 0; i<vl;i++) dst[i] = new PVector();
    for(int i = 0; i<vl;i++) rMat.mult(clone[i],dst[i]);
    return dst;
    

    will resolve the issue.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.