1

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.

1
  • Your description isn't really clear, and relationship of your code to your description is even less clear. I assume you want to keep some point at a fixed location, and the distance of every other point from that fixed point is multiplied by a scaling factor. One approach is to translate all points so the fixed point (after translation) becomes (0,0), apply the scaling (which won't affect the origin), and then translate everything back (apply a translation that is the negative of the first) so the fixed point moves back to its original location. Commented Jun 6, 2022 at 3:01

2 Answers 2

1

Each glVertex is transformed by the current model view and projection matrix when it is specified.
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();
Sign up to request clarification or add additional context in comments.

2 Comments

oh my gosh, that's the most effortless thing i haven't seen in a while, i tried soo many things but this. But i still don't understand about matrices thing, is it like you put all the pointer into a single matrices and make it as one entity where you can scale, translate and rotate the all the shape/pointers in the matrices without having to manually do it?
@AMIRULASYRAAFSAIFULAIZAM Each glVertex is transformed by the current model view and projection matrix when it is specified.
1

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).

1 Comment

actually thats not what i meant, the wheel part is already in the fix position as what actually wanted it to do but the car part tend to scale and reposition itself. I should've not done the transformation manually without using matrices. Anyway, thank you for answering my question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.