Code below shows a bunch of spheres laid out in a grid on the xz plane. below the mouse cursor is a larger sphere on that plane. Change angles using arrow keys and pan left/right/up/down with WASD. as you can see, no matter how you move, the sphere remains under the mouse cursor, just as I want it to be. Now uncomment lines 22 and 23 to enable orthographic view. Now the cursor is way off from where it should be. I used a modification of the code from here
https://forum.thegamecreators.com/thread/55835#msg587148. the code has been simplified as the plane's normal is 0 on the x and z and 1 on the y, so dot product of vector A with normal is just the y component of A and as the plane origin is at 0,0,0, then d will always be 0 making calculations simple.
I don't believe that my code is in error as it works fine in normal 3D mode, only breaks in orthographic mode.
// Project: OrthoProblem
// Created: 2018-04-07
#constant CameraDistance 10.0
#constant rotatespeed 60.0
#constant movespeed 15.0
// show all errors
SetErrorMode(2)
// set window properties
SetWindowTitle( "3DStuff" )
SetWindowSize( 1024, 768, 0 )
SetWindowAllowResize( 1 ) // allow the user to resize the window
// set display properties
SetVirtualResolution( 1024, 768 ) // doesn't have to match the window
SetOrientationAllowed( 1, 1, 1, 1 ) // allow both portrait and landscape on mobile devices
SetSyncRate( 0, 0 ) // 30fps instead of 60 to save battery
SetScissor( 0,0,0,0 ) // use the maximum available screen space, no black borders
UseNewDefaultFonts( 1 ) // since version 2.0.22 we can use nicer default fonts
//SetCameraFOV(1,0)
//SetCameraOrthoWidth(1,8)
scale as float
Type TVector
x as float
y as float
z as float
endtype
frametime as float
SetCameraPosition(1,0,10,0.0)
setcamerarotation(1,90,0,0)
for x = -2 to 2
for y = -2 to 2
SetObjectPosition(CreateObjectSphere(0.1,5,5),x,0,y)
next
next
type TPivot
x as float
z as float
azimuth as float
polar as float
endtype
camerax as float
cameray as float
cameraz as float
Pivot as TPivot
Pivot.x = 0.0
Pivot.z = 0.0
Pivot.azimuth = 90.0
Pivot.polar = 89.99
moved as integer = 0
moveangle as float
pointer as TVector
PointerObject = CreateObjectSphere(.5,5,5)
do
frametime = GetFrameTime()
if GetRawKeyPressed(27) = 1 then exit
if GetRawKeyState(40) = 1 then Pivot.polar = Pivot.polar - rotatespeed*frametime
if GetRawKeyState(38) = 1 then Pivot.polar = Pivot.polar + rotatespeed*frametime
if GetRawKeyState(37) = 1 then Pivot.azimuth = Pivot.azimuth - rotatespeed*frametime
if GetRawKeyState(39) = 1 then Pivot.azimuth = Pivot.azimuth + rotatespeed*frametime
if Pivot.polar < 45.0 then Pivot.polar = 45.0
if Pivot.polar > 89.99 then Pivot.polar = 89.99
if Pivot.azimuth < 0.0 then Pivot.azimuth = Pivot.azimuth + 360.0
if Pivot.azimuth >= 360.0 then Pivot.azimuth = Pivot.azimuth - 360.0
moveangle = 0.0
moved = 0
if getrawkeystate(83) = 1
moved = 1
elseif GetRawKeyState(87) = 1
moved = 1
moveangle = 180.0
endif
if GetRawKeyState(65) = 1
moved = 1
moveangle = moveangle - 90.0
elseif GetRawKeyState(68) = 1
moved = 1
moveangle = moveangle + 90.0
endif
if moved = 1
Pivot.x = Pivot.x + cos(Pivot.azimuth+moveangle)*movespeed*frametime
Pivot.z = Pivot.z + sin(Pivot.azimuth+moveangle)*movespeed*frametime
endif
cameraz = CameraDistance*cos(Pivot.polar)
cameray = CameraDistance*sin(Pivot.polar)
camerax = cameraz*cos(Pivot.azimuth)
cameraz = cameraz*sin(Pivot.azimuth)
SetCameraPosition(1,camerax+Pivot.x,cameray,cameraz+Pivot.z)
SetCameraLookAt(1,Pivot.x,0.0,Pivot.z,0)
MX = GetPointerX()
MY = GetPointerY()
GetMousePlaneCollision(Pointer,MX,MY)
SetObjectPosition(PointerObject,Pointer.x,Pointer.y,Pointer.z)
print(str(Pointer.x)+":"+str(Pointer.y)+":"+str(pointer.z))
Sync()
loop
function GetMousePlaneCollision(Pointer ref as TVector,MX as float, MY as float)
p as TVector
v as TVector
//get the screen vector
v.x = Get3DVectorXFromScreen(MX,MY)
v.y = Get3DVectorYFromScreen(MX,MY)
v.z = Get3dVectorZFromScreen(MX,MY)
//get the origin point
p.x = v.x + GetCameraX(1)
p.y = v.y + GetCameraY(1)
p.z = v.z + GetCameraZ(1)
//extend the vector past the plane
v.x = v.x * 800
v.y = v.y * 800
v.z = v.z * 800
remstart c code
//p->x p->y p->z is the start point of the line
//v->x v->y v->z is the vector from start point to end point
//p+v gives the end point
//check the points distance from the plane
dist1 = normal.x*(p->x+v->x) + normal.y*(p->y+v->y) + normal.z*(p->z+v->z) + d;
dist2 = normal.x*p->x + normal.y*p->y + normal.z*p->z + d;
// >0 means the point is on the visible side of the plane
// <0 means the point is on the back side of the plane
// if start point is behind the plane, or the end point is in front
// of the plane, ignore the point (can't collide with back of polygon)
if (dist2<-0.00001 || dist1>0.00001) return false;
//use dot product to work out the distance along the line to the intersect point
dist1=-(normal.x*v->x) - (normal.y*v->y) - (normal.z*v->z);
if (dist1!=0) dist1=dist2/dist1;
else dist1=0;
//work out the point of intersection with the plane
intersect.x = p->x + v->x*dist1;
intersect.y = p->y + v->y*dist1;
intersect.z = p->z + v->z*dist1;
//check if this point lies in the triangle
if (!pointInPoly(intersect)) return false;
return true;
remend
dist1 as float
if (v.y) <> 0
dist1 = p.y / - v.y
else
dist1 = 0.0
endif
Pointer.x = p.x+v.x*dist1
Pointer.y = p.y+v.y*dist1
Pointer.z = p.z+v.z*dist1
endfunction