Home > Blockchain >  How to scale without changing object translated position
How to scale without changing object translated position

Time:06-06

I have a problem to scale an object without changing translated position. I tried to apply translate and scaling using keybind for my car but when i click the keybind for scaling, only my body repositioned it self but the car's tire scaled at the fixed position i translated. Below is my code:

#include <windows.h>
#include <gl/glut.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>

const float steps = 100;
const float angle = 3.1415926 * 2.0 / steps;
float m = 0;
float s = 1;

void wheel(float x, float y)
{
float w;
glBegin(GL_POLYGON);
glColor3ub(0, 0, 0);
for (int i = 0; i < 360; i  )
{
    w = (i*3.1416 / 180);
    glVertex2f(x   0.04 * s * cos(w), y   0.04 * s * sin(w));
}

glEnd();
}

void display() {

glClear(GL_COLOR_BUFFER_BIT);

glBegin(GL_QUADS);
                                        glColor3ub(48, 51, 64);
glVertex2f(-1, 1);                      glColor3ub(48, 51, 64);
glVertex2f(1, 1);                       glColor3ub(11, 11, 16);
glVertex2f(1, -0.1);                    glColor3ub(11, 11, 16);
glVertex2f(-1, -0.1);

glEnd();

//[2] Floor
glBegin(GL_QUADS);
                                        glColor3ub(51, 25, 0);
glVertex2f(-1, -0.1);                   glColor3ub(51, 25, 0);
glVertex2f(1, -0.1);                    glColor3ub(102, 51, 0);
glVertex2f(1, -1);                      glColor3ub(102, 51, 0);
glVertex2f(-1, -1);

glEnd();

//[3-1] Road
glBegin(GL_QUADS);
                                        glColor3ub(26, 26, 26);
glVertex2f(-1, -0.2);                   glColor3ub(26, 26, 26);
glVertex2f(1, -0.2);                    glColor3ub(40, 40, 40);
glVertex2f(1, -0.9);                    glColor3ub(40, 40, 40);
glVertex2f(-1, -0.9);

glEnd();

//[3-2] Road
glBegin(GL_QUADS);
                                        glColor3ub(248, 210, 16);
glVertex2f(-0.9, -0.52);                glColor3ub(248, 210, 16);
glVertex2f(-0.7, -0.52);                glColor3ub(248, 210, 16);
glVertex2f(-0.7, -0.58);                glColor3ub(248, 210, 16);
glVertex2f(-0.9, -0.58);

glEnd();

//[4] Star
glBegin(GL_POLYGON);

glEnd();

//[5] Building
glBegin(GL_POLYGON);

glEnd();

//[6] Car
glBegin(GL_POLYGON);
                                        glColor3ub(65, 105, 225);
glVertex2f(m   (s * 0.50), s * -0.7); 
glVertex2f(m   (s * 0.52), s * -0.6);
glVertex2f(m   (s * 0.80), s * -0.6);
glVertex2f(m   (s * 0.86), s * -0.7);
glVertex2f(m   (s * 0.86), s * -0.8);
glVertex2f(m   (s * 0.44), s * -0.8);
glVertex2f(m   (s * 0.44), s * -0.7);
glVertex2f(m   (s * 0.50), s * -0.7);

glEnd();

wheel(m   0.52, -0.8);
wheel(m   0.76, -0.8);

glFlush();
}

void key(unsigned char key, int x, int y) {
switch (key)
{
case 'A':
case 'a':
    m = m - 0.1;
    
    break;

case 'D':
case 'd':
    m = m   0.1;
    
    break;

case 'Q':
case 'q':
    s = s   0.1;

    break;

case 'E':
case 'e':
    s = s - 0.1;

    break;
}
glutPostRedisplay();

}


int main(int argc, char* argv[]) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(1000, 600);
glutInitWindowPosition(0, 0);
glutCreateWindow("Moving Car");
glutDisplayFunc(display);
glutKeyboardFunc(key);
glutMainLoop();
return 0;
}

I don't know how to make it scale at a fixed position after translating, I did try applying translate in the scaling part but it doesn't work.

CodePudding user response:

If I'm understanding you correctly, both the car body and the wheels are scaling and translating relative to the origin, but the position of the wheels relative to the car body is fixed at (0.52, -0.8) and (0.76, -0.8), regardless of the size of the car. This means it will look wrong when you scale the car.

In that case, what you need to do is include the scale factor in your calculation of the wheel position, like so:

wheel(m   s * 0.52, s * -0.8);
wheel(m   s * 0.76, s * -0.8);

Then the wheels will be at (0.52, -0.8) and (0.76, -0.8) in "car-space coordinates", if you will. In other words, when the scale factor is 1, the wheels will be at these coordinates (in world space), but as the scale factor increases, so will the distance of the wheels from the origin (in world space).

CodePudding user response:

You have to scale the complete object (car and wheels) before any other transformation. Use glTranslatef and glScale to multiply the current matrix by a general scaling matrix, glPushMatrix to save the matrix on the matrix stack and glPopMatrix to restore the matrix from the matrix stack.

e.g.:

void wheel(float x, float y)
{
    float w;
    glBegin(GL_POLYGON);
    glColor3ub(0, 0, 0);
    for (int i = 0; i < 360; i  )
    {
        w = (i*3.1416 / 180);
        glVertex2f(x   0.04 * cos(w), y   0.04 * sin(w));
    }
    glEnd();
}
glPushMatrix();
glTranslate(m, 0, 0);
glScalef(s, s, 1);

glBegin(GL_POLYGON);
glColor3ub(65, 105, 225);
glVertex2f(0.50, -0.7); 
glVertex2f(0.52, -0.6);
glVertex2f(0.80, -0.6);
glVertex2f(0.86, -0.7);
glVertex2f(0.86, -0.8);
glVertex2f(0.44, -0.8);
glVertex2f(0.44, -0.7);
glVertex2f(0.50, -0.7);
glEnd();

wheel(0.52, -0.8);
wheel(0.76, -0.8);

glPopMatrix();
  • Related