0

I have program in which the object (a triangle) follows the mouse as it moves around and rotates to the direction its moving. What do I have to do to make it so the object stays still, until I click it, drag it to a position and once I release the mouse, it starts to move to that position?

#include <GL/glut.h>
#include <math.h>
# define ANIMATION_STEP (1000/300)
# define PI 3.1415926535897932

struct Globals {
    centre_x, centre_y, rotate;
    float length;
    float mouse_x, mouse_y, speed;
    int animating;
} globals;

void init(void){
    // Starting position of the triangle
    globals.centre_x = 100;
    globals.centre_y = 100;
    globals.rotate = 0.0;
    globals.mouse_x = 300.0;
    globals.mouse_y = 300.0;
    // Animation speed in pixels per second
    globals.speed = 300.0;
    // size of the triangle
    globals.length = 27;
    globals.animating = 1;
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity(); 
    gluOrtho2D(0.0, 1000.0, 0.0, 700.0);
}

void triangle(void){
    glBegin(GL_POLYGON);
    glVertex2f(0.5, 0.0);
    glVertex2f(-0.5, -0.5);
    glVertex2f(-0.5, 0.5);
    glEnd();
}

void display(void){
    glClear(GL_COLOR_BUFFER_BIT);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    glTranslatef(globals.mouse_x, globals.mouse_y, 0.0);
    glRotatef(globals.rotate, 0.0, 0.0, 1.0);
    glScalef(globals.length, globals.length, 1.0);
    triangle();
    glFlush();
    glutSwapBuffers();
}

float limit(float x, float min, float max){
    if (x < min) {
        x = min;
    }
    if (x > max) {
        x = max;
    }
    return x;
    }

void timer(int v){
    // Computing elapsed time for smooth animation.
    int time = glutGet(GLUT_ELAPSED_TIME);
    float angle;
    glutTimerFunc(ANIMATION_STEP, timer, time);
    if (globals.animating) {
        int delta_t = time - v;
        float delta_x, delta_y, length, step_size;
        // Compute vector from current location to mouse
        delta_x = globals.mouse_x - globals.centre_x;
        delta_y = globals.mouse_y - globals.centre_y;
        // Compute length of the vector
        length = sqrt (delta_x*delta_x + delta_y*delta_y);
        // If the triangle is close to the mouse, then no motion is required.
        step_size = globals.speed * delta_t / 1000.0;
            if (length > step_size * 0.55) {
            delta_x = delta_x / length;
            delta_y = delta_y / length;
            globals.centre_x += delta_x * step_size;
            globals.centre_y += delta_y * step_size;
            angle = atan2(delta_y, delta_x);
            globals.rotate = angle * 180.0 / PI;
            // Keep the triangle inside the world window.
            globals.centre_x = limit(globals.centre_x, 0.0 + globals.length/2, 1000.0 - globals.length/2);
            globals.centre_y = limit(globals.centre_y, 0.0 + globals.length/2, 700.0 - globals.length/2);
        }

        glutPostRedisplay();
    }
}

void mousemotion(int x, int yc){
    globals.mouse_x = x;
    globals.mouse_y = 700 - yc;
    glutPostRedisplay();
}

main(int argc, char** argv){
    glutInit(&argc, argv);
    glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB);
    glutInitWindowSize(1000, 700);
    glutInitWindowPosition(10, 10);
    glutDisplayFunc(display);
    glutTimerFunc(ANIMATION_STEP, timer, 0);
    glutPassiveMotionFunc(mousemotion);
    init();
    glutMainLoop();
    return 0;
}

I have investigated processMouse() where if state == GLUT_DOWN then it records the current position and mouse press cordinates, but the best ive been able to get is that it immediately teleports to the mouse click. Can someone please explain what I would need to do to click on it, drag, release, then move to position?

genpfault
  • 51,148
  • 11
  • 85
  • 139
Alonzo Robbe
  • 465
  • 1
  • 8
  • 23
  • see [simple low level Drag&Drop example in C++](https://stackoverflow.com/a/20924609/2521214) just tweak it to your needs. – Spektre Apr 15 '18 at 12:26
  • @Spektre this does not help me at all. it might be C++ but it doesnt look like that at all to me. I cant just tweak it because i dont even know what i am looking at. i appreciate the help, and might be wrong in thinking its not C++ but even if it is, it doesnt make sense to me – Alonzo Robbe Apr 15 '18 at 23:42
  • It is simple and commented example on how **Drag and Drop** style editors are done ... Which is in my opinion what are you trying to do (for single object instead of list) so the only difference is you have one object and rendering with **OpenGL** instead of **GDI**. So absorb the architecture of that example and incorporate it into your code. For example `viewer` is `view` matrix and its inverse from your `ModelView` Your triangle is `Atom` class etc ... – Spektre Apr 16 '18 at 07:01
  • Possible duplicate of [How to create a rubber-esque drag and move of 2D object in c++ glut](https://stackoverflow.com/questions/49870730/how-to-create-a-rubber-esque-drag-and-move-of-2d-object-in-c-glut) – Spektre Apr 17 '18 at 07:25

1 Answers1

-1

in opengl, how to change mouse...

You don't. Mouse does not exist in OpenGL. Maybe you should change the question to "How to process mouse events in windows/GLUT/SDL/whatever-framework?" for which there are dozens of duplicates.

Neithy
  • 139
  • 4