I have an OpenGL scene set up like so:
The scene has a single camera. The camera cannot rotate but it can zoom and pan.
The camera is looking directly at a textured quad.
I can get the raycasting result that's generated from the mouse cursor position extending to the quad.
I need the camera to center on the mouse cursor position when I press a keyboard button. How do I do this? FYI the quad most remain in place but the camera can move around.
Related
I'm trying to get my cursor sprite to try and appear on top of my mouse cursor but for some reason it is not appearing on top of the mouse cursor.
Code:
cursorSprite.setPosition(sf::Mouse::getPosition().x, sf::Mouse::getPosition().y);
As you can see, I'm simply using setPosition() with my sprite, and setting it at the x and y positions of the mouse. However, this is not working and the cursor sprite is appearing at a different location to my actual mouse cursor location.
Why? Could it be something to do with how I'm setting up my window?
window(sf::VideoMode(800, 600, 32), "SFML Test", sf::Style::Default)
There are two main coordinate systems that should be considered when running SFML app (as well as pretty much every other application) in windowed mode: screen coordinates and windowed coordinates.
You are getting wrong results because sf::Mouse::getPosition() return click position in screen coordinates and you want click in window coordinates.
You can manually transform screen coordinates to window coordinates, but it is much better to use this interface provided by SFML:
sf::Vector2i pos = sf::Mouse::getPosition(window);
The pos will be in window coordinates.
How will i get a application x y position relative to the client screen?
I tried but all unsucessful so can anyone help.
RECT pta;
GetWindowRect(hWnd,&pta);
POINT Rpt = { pta.left, pta.top };
ScreenToClient(hWnd, &Rpt);
But this doesn't work.
I want to set my cursor position to middle in the window of my app
If I understand right, you want to call the SetCursorPos() windows API call to center the mouse cursor to your window. That function takes screen coordinates.
GetWindowRect() returns the window top and left coordinates already in the screen coordinates, so no transform is necessary.
To get to your window's center coordinates, you just need to add half of your window's width and height to the top-left point's coordinates. Then you can call SetCursorPos().
I am using Qt Graphics Framework for displaying an image. I have opened a raw image in subclassed QGraphicsScene in QGraphicsView using addPixmap(). I have added zoom feature by using scale function and drag mode is set as scroll hand drag. Now, I need to get the pixel coordinates within the scene on mouse hover such that the x and y value show the pixel in the image (drawn by pixmap) the mouse is currently pointing to. I tried using pos() but it didn't work.
Here is the code from Widget.cpp:
img = openImage(dirPath2.toLocal8Bit().data(),
sizeX,sizeY,file_heade,scan_heade,bpp,sign);
QPixmap x = QPixmap(sizeX,sizeY);
x.convertFromImage(img,Qt::AutoColor);
scene->addPixmap(x);
ui->disp_img->setDragMode(QGraphicsView::ScrollHandDrag);
GraphicsScene.h:
class GraphicsScene : public QGraphicsScene {
public:
GraphicsScene(QWidget *parent) : QGraphicsScene(parent){}
};
(preferably the pixmap coordinates but even that doesn't happen and if the values change when zoomed I will use scale factor to get the original values)
I suggest you start by reading about Qt's Graphics Coordinate System.
There are various layers of coordinate systems and you need to think about those with which you dealing with. At the top layer is the screen (or view), which is where the mouse coordinates reside.
The next layer from the view is the graphics scene. Graphics items, such as the QGraphicsPixmapItem which you added with addPixmap, reside here. The graphics scene can be visualised as a world of items, each with there own position and orientation.
Moving to the last coordinate system is an item's local coordinate system. If, for example, we take a rectangle, it may have local coordinates of (-5, -5, 10, 10) for (x, y, w, h). This item is then placed in the scene at some position. If its position is the origin of the scene (0,0), then the item's local coordinates would read the same as its scene coordinates.
However, if we move the rectangle +5 units in x-axis, its local coordinates are the same, but its scene coordinates would now be (0, -5, 10, 10).
Likewise, the view (QGraphicsScene) is a window into the scene and can be looking at the whole scene, or just part of it. As the view's top left coordinate is (0,0), it may map onto (0,0) of the scene, or may not, depending on what area of the scene the view is looking at.
So, by getting a mouse position you're starting in the view's coordinates and need to convert to the scene's coordinate system. Fortunately, Qt provides lots of useful functions for this at every level.
To convert the mouse coordinates from the view to the scene, you can use the view's mapToScene function.
Using the scene coordinates you can then get an item and map that to the local coordinate's of the item with the item's mapFromScene.
This is continue of my previous question -> Draw mouse pointer icon?
I want to draw mouse in specific window on the desktop, i already know how to draw the mouse and how to track the movement of the real mouse.
But i fail to convert screen coordinates, here is example what i want to do:
When the REAL mouse is in the upper left corner 0,0 the DRAWN mouse to be in 0,0 of my specific window, and when the REAL mouse is in the down right corner 1600,900 the DRAWN mouse to be in 700,400 of my specific window.
I will re-explain if someone is not understanding exactly the problem.
Thanks in advance.
You need to scale the mouse position w.r.t your window dimensions.
Let DX and DY be the desktop size. Let WX and WY be your window size. Let (Dmx,Dmy) be the coordinate of the original mouse position w.r.t the desktop. Then the position of your mouse within your window according to your requirement would be (Dmx/DX * WX, Dmy/DY * WY). When coding, please remember to make sure the division happens with floating point numbers.
I have a GUI application where you can drag widgets around in a QGraphicsView/Scene. You can also pan and zoom while you're not dragging a widget.
What I want:
I want to make it so that when you're dragging an item near the edge of the screen that it starts to pan in that direction and keeps panning even if the mouse isn't moving, but the mouse still needs to be in the top/left/right or bottom 10% of the screen.
What I've tried:
Inside the dragMoveEvent I check if the mouse is on the edge of the screen. If so I start a Qtimer with 15msec timeout. On timeout a function is slotted to pan the scene in the right direction by updating the center. This approach WORKS, BUT when i leave the mouse still on the edge the panning is choppy and if the mouse is moving around inside the edge then the panning is smooth as it should be.
Bottom Line:
Why is the panning only choppy/laggy when the mouse isn't moving? How do I fix it so that the panning is smooth like when you move the mouse around inside the edges?
What happens when you increase/decrease your timeout?