When it is in center, the positions match, however when I go to any side, the positions start to get away slowly
This is how I calculated the world position:
This is how I calculated the world position:
I am not sure if I am getting this right..
There is a widgetcomponent in world and you are converting its mouse position to widget.
First the mouse pos X Y in viewport scale so you have to compansate its coordinates to widgetscale and recalculate mouse pos.
ViewportDimension/WidgetDimension = ScaleFactor
MousePos * ScaleFactor = CompansateMouseCoordinateWidget.
If you want to compansate also the drawdebug sphere, thats origin is center so its different than the mouse pos, which is top right anchor 0.0.
Can you briefly explain your hierarcy and what is debug shere doing? Since the title of thread says,
Real mouse follow , virtual mouse? I don’t see a real mouse here only mouse pos which is in 2d Viewport space. I can help solve it if I can understood problem space correctly.
Sorry, the real mouse is actually in the place of a debug sphere. I want my real mouse to follow the virtual mouse (cannot be the other way around because virtual mouse is clamped and kept inside the widget). But for some reason my real mouse (debug sphere) has an offset the more I go from the center.
Thats ok we can easy do that however if you are looking for your widget cursor follow system cursor than it is more complicated. Ex: Like a finger of character pointing from world to widget buttons position. Cause that time we need to also detect button position in the world with respect to camera and angle of widget etc.
However if you just want to do this debug sphere follow thing you can do like this.
So I created and actor with a widgetcomponent, attach actor to player to give a gameplay element like your (A tablet) nothing much. I also added a arrow component to additionally track the position of world coordinates.
On my widget there is nothing much again just a container with a fake mouse overlay
As Script
First I scale down widget to viewport but you don’t really have to do this.
After I get mousecontainerfake → get cached geometry → transform to absolute location → subtract half of widget size so its origin topleft-> Make A Vector → WidgetComponent(TransformPOS Vector)
I pass mouse position additionally back to owner actor to move the arrow, I passed owner actor in the beginning when creating widget already for ease of access.
and it gives me result as below.
However if your System Cursor to match also the widget thats another thing. Which we have to get cursor raw position, raycast to widget and find location on widget then do something. Think the widget interaction component also does something like that.
Let me know if it answers your question.
Snippet
https://dev.epicgames.com/community/snippets/XgwQ/unreal-engine-umg-widget-element-to-world-position
Hey, thanks for your dedication to my problem, however, I want my real mouse to be teleported to exact location of my fake mouse which is driven by mouse delta. I am not sure I understand how to get the exact location of the virtual mouse? So I just want the debug sphere (real mouse) to follow virtual mouse exactly. Or even better, jus restrict the real mouse to the 3d widget bounds. Is that possible?
Everything is possible if you define the problem from the beginning right. You cannot reposition your cursor again since you are getting position from mouse pos and setting mouse position again through as loop will make it fail/stuck.
Since your mouse is in Screen Space and your widget is in World Space:
There are multiple ways to solve it, however I cheated a bit to go traditional and remember some parts of linear algebra again (thanks for it)
I made 4 scene components on edges so i can access coordinate system with ease without bounds etc.
InWidget I made a trace from mouse pos to world. Considered this position as mouse input on widget.
However need to solve UV coordinate of Widget on the point on impact. After solving UV in 0-1 coordinate space multiplied with widget size and positioned fake cursor there. So now everything matches.
Result
not perfect but thinks its more than enough. A scaling calculation should be done depending on the usage. Let me know and happy developing.
PS:
UV calculation in a nutshell.
Origin TopLeft
Get Right Direction (TopRight-TopLeft)->Normalise
Get Bottom Direction (TopLeft-BottomLeft)->Normalise
ImpactPoint-Origin(Topleft) * DotProductNormals
Divide them with coordinate lengths for Right and Bottom
UV Coordinate 0-1 of impact point.
Multiply its dimensions with UV
Result is coordinate in WidgetSpace