Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

AppGameKit Classic Chat / How to get mouse coordinates in 3d on a big invisible plane

Author
Message
CoreGrid
7
Years of Service
User Offline
Joined: 16th Feb 2017
Location:
Posted: 14th Jul 2017 21:23
So i already managed to get the mouse x y an z coordinates, here is the code (used some snippets, not written by me):

// mouse x and y
int mX = agk::GetPointerX();
int mY = agk::GetPointerY();

// mouse to vector
float X3D = agk::Get3DVectorXFromScreen(mX, mY);
float Y3D = agk::Get3DVectorYFromScreen(mX, mY);
float Z3D = agk::Get3DVectorZFromScreen(mX, mY);

// get camxyz
float camX = agk::GetCameraX(1);
float camY = agk::GetCameraY(1);
float camZ = agk::GetCameraZ(1);

// Raytracer startpos
float rStartX = X3D + camX;
float rStartY = Y3D + camY;
float rStartZ = Z3D + camZ;

// Raytracer endpos
float rEndX = 1024 * X3D + camX;
float rEndY = 1024 * Y3D + camY;
float rEndZ = 1024 * Z3D + camZ;

return agk::CreateVector3(rEndX, rEndY, rEndZ);

I get the coordinates without it checking for collisions. But i would like to have a infinite or just huge plane at y=0.
And get the mouse xyz and z position from there. Just settings the rEndY to 0 won't work the x and z coordinates will be off.
Does anyone have a idea how to do this, to get the xyz mouse coordinates on a big invisible plane?
Golelorn
7
Years of Service
User Offline
Joined: 20th Nov 2016
Location:
Posted: 15th Jul 2017 13:30 Edited at: 15th Jul 2017 20:59
https://forum.thegamecreators.com/thread/217477
hendron
8
Years of Service
User Offline
Joined: 20th Dec 2015
Location:
Posted: 16th Jul 2017 00:03 Edited at: 16th Jul 2017 00:11
If I understand your question correctly, and assuming your plane is at 0,0,0 with a normal of 0,1,0, here's a snippet demonstrating one way to do it. This is in Tier1, but I doubt it'd be hard to translate to Tier2.



If your plane has a different normal or position, it'd be a bit more complicated.
Golelorn
7
Years of Service
User Offline
Joined: 20th Nov 2016
Location:
Posted: 16th Jul 2017 04:49
Thank you, Hendron!
hendron
8
Years of Service
User Offline
Joined: 20th Dec 2015
Location:
Posted: 16th Jul 2017 07:57
No problem. Having to do this kind of thing for a project I'm working on too.
CoreGrid
7
Years of Service
User Offline
Joined: 16th Feb 2017
Location:
Posted: 23rd Jul 2017 20:34
Thanks Hendron, i tried some different methods but this one is the easiest to work with.
Here is the code for tier 2

cx = agk::GetCameraX(1);
cy = agk::GetCameraY(1);
cz = agk::GetCameraZ(1);

pxRaw = agk::Get3DVectorXFromScreen(agk::GetPointerX(), agk::GetPointerY());
pyRaw = agk::Get3DVectorYFromScreen(agk::GetPointerX(), agk::GetPointerY());
pzRaw = agk::Get3DVectorZFromScreen(agk::GetPointerX(), agk::GetPointerY());

// get pointer vector y added to camera's y position
pyRelative = pyRaw + cy;

// find distance between camera's y position and pyRelative#
dist = cy - pyRelative;

// calculate scaling value by finding how many times dist# goes into the camera's y position
s = cy / dist;

// find the intersection point by multiplying pointer vector by s# and adding it to the camera position.
// ignoring Y in this case since it should be 0 anyway.
ix = cx + pxRaw * s;
iz = cz + pzRaw * s;

// position the object at the intersection point
agk::SetObjectPosition(test, ix, 55, iz);

Login to post a reply

Server time is: 2024-04-25 18:06:43
Your offset time is: 2024-04-25 18:06:43