Code Monkey home page Code Monkey logo

Comments (4)

jwdunn1 avatar jwdunn1 commented on September 28, 2024

This sketch may help...especially the screenToWorld() method in the DwCoordinateTransform class. https://www.openprocessing.org/sketch/496458

from p5.easycam.

nevahid avatar nevahid commented on September 28, 2024

Wow... thank you very much... i'm diving into it to find my solution... in my own solution i could get the mouse position right when there was no rotation, but if i rotate the scene, 3d coordinates differ from mouse coordinates...
thanks again... i will check that right now :-)

from p5.easycam.

nevahid avatar nevahid commented on September 28, 2024

@jwdunn1
hi again dear james, well i tried to figure your project, but it was way different that p5.easycam... anyway i looked at your conversion method and they both looked identical algorithmically... i will post my code here, maybe i've missed something. thanks for your kind help:

the function getResultMouse is the main method... toRealProject(mx,my) get the mouse X and mouse Y position and produce a ray in that direction.. and the getRayPoint(ray) takes the ray and gives the amount of ray (t) where the ray meets a specific plane (here y=-100)... and then getResultMouse takes the ray and the t and gives out the answer... if i look forward ( i mean at (0,0,-1)) everything works great and as expected, but when i rotate the screen, there seems i'm missing something there as the projected vertice is not under mouse coordinates... please help me :-(

function toRealProject(mx,my)
{
	let x=(2*mx)/width -1;
	let y= 1- (2*my)/height;
	let z=-1;
	let vecRayClip=[x,y,z,1];
	let PMatrix=RENDERER.uPMatrix.copy();
	let invPM=matrix_invert(PMatrix);
	let vecRayEye=multiply(invPM,vecRayClip);
	let vecRayEyeNew=[vecRayEye[0],vecRayEye[1],-1,0];
	let VMatrix=RENDERER.uMVMatrix.copy();
	let invVM=matrix_invert(VMatrix);
	let multEYEVM=multiply(invVM,vecRayEyeNew);
	let w_inv = 1/multEYEVM[3];
	let vecRayWorld=createVector(multEYEVM[0]*w_inv,multEYEVM[1]*w_inv,multEYEVM[2]*w_inv);
	vecRayWorld.normalize();
	return vecRayWorld;
}

function getRayPoint(ray) {
	let normal = createVector(0,1,0);
	let fixedpoint= createVector(0,-100,0); //seems this is better!!
	let mousepos= mainCam.getPosition();
	let denom = p5.Vector.dot(ray,normal);
	let vecmf= p5.Vector.sub(fixedpoint,mousepos);
	let nom= p5.Vector.dot(vecmf,normal);
	return nom/denom;
}

function getResultMouse() {
	let thisvecray=toRealProject(mouseX,mouseY);
	let t=getRayPoint(thisvecray);
	let tn=p5.Vector.mult(thisvecray,t);
	let campos=createVector(mainCam.getPosition()[0],mainCam.getPosition()[1],mainCam.getPosition()[2]);
	let answer= p5.Vector.add(campos,tn);
	return answer;
}

from p5.easycam.

nevahid avatar nevahid commented on September 28, 2024

hi to those people reading this... i found the solution, and it was unbelievably strange!! i just commented part the inverted the view matrix... so i just multiplied the view matrix into my vecRayEyeNew... and now everything is working as expected!!! sooo strange...

from p5.easycam.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.