udbhavs
udbhavs

Reputation: 98

Processing Trigonometry Code Not Working

My processing code is based on the Oscillation chapter of Nature Of Code by Daniel Shiffman, mainly the examples where he rotates a mover according to the direction it is heading (the mouse). It doesn't work, though I'm pretty sure its one little mistake somewhere. The rectangle just flies outside the screen. Example from book (not full):

void display() {
    float angle = velocity.heading2D;
    stroke(0); fill(175); pushMatrix(); rectMode(CENTER);              
    translate(location.x,location.y);
    rotate (angle); 
    rect(0,0,30,10);
    popMatrix();
}

My code :

void show() {
    angle = velocity.heading2D();
    rectMode(CENTER);
    pushMatrix();
    translate(location.x,location.y);
    rotate(angle);

    fill(255,20,20,150);
    rect(location.x,location.y,carSize,carSize);
    popMatrix();
}

Setup and draw :

Car car ;
void setup() {
    car = new Car();
}

void draw() {
    mouse = new PVector(mouseX,mouseY);
    background(255);
    car.show();
    car.move();
    car.update();
}

Upvotes: 1

Views: 54

Answers (1)

udbhavs
udbhavs

Reputation: 98

I finally figured it out. Instead of

 translate(location.x,location.y);  
 rect(location.x,location.y,40,40);  

It had to be :

 translate(location.x,location.y);  
 rect(0,0,40,40);    

because the translate already takes the origin point to the object's location, and that's where I want to draw it (0,0).

Upvotes: 1

Related Questions