reset password
Author Message
maximf
Posts: 31
Posted 12:59 Apr 26, 2009 |

how can i change the mouse coordinates into the world coordinates so when i click on a target it will be recognized as the same coordinates for the mouse and the target

semory
Posts: 4
Posted 15:26 Apr 26, 2009 |

Screen coordinates for lower left is (-1.0, -1.0)
Screen coordinates for for upper right is (1.0, 1.0)

So you have your (mouse_x, mouse_y) in screen coordinates...

Now you get your world coordinates for  lower left and upper right portion of the screen... how? I did my mouse control a different way but I would try base.camLens.getFilmSize() or other functions from the global object base.camLens (type is Lens*). From this object and knowing the current camera position, base.camera.getPos(), you should be able to compute the world coordinates of the screen corners. Then you do the ratio math from there. Too bad there isn't a corresponding get function to base.camLens.setFrustumFromCorners() in Panda3D. You have to do the calculation yourself.

Last edited by semory at 15:26 Apr 26, 2009.
maximf
Posts: 31
Posted 17:50 Apr 26, 2009 |

i'm sorry but i dont seem to understand how to get to the right coordinates, when doing getFilmSize i get (1.0,0.75)  and the camera pos is (0,0,0) , but i still dont understand how it helps me to get the same coordinates to i will find if i clicked right on the target or not

semory
Posts: 4
Posted 18:29 Apr 26, 2009 |

After you get the coordinate conversion done, shoot a collision ray from the point in the same direction the camera is facing. If you get a collision, you hit a target.

Easier way to do this homework is to parent a collision ray to the camera, facing the same direction... wherever camera ray points, collision ray will follow (see roaming ralph example). Then use mouse to control heading and pitch of camera (see  below).

 

 

 

Last edited by semory at 18:57 Apr 26, 2009.
maximf
Posts: 31
Posted 19:21 Apr 26, 2009 |

i understand the idea, but i just cant get it to work, i rather just do the formulas instead of collision rays , what i do is randomize numbers for x,y,z that i use for setPos of the target

and then

  WC=base.camLens.getFilmSize()
  WCx = WC.getX()/2
  WCy = WC.getY()/2
 xWC=(self.x*2*WCx)/(2*(self.x+self.z))
 yWC=(self.z*2*WCy)/(2*(self.x+self.z))

 

when for those i just follow the formulas that were written on the image, but the numbers still dont come out the same, i cant figure out where i went wrong

semory
Posts: 4
Posted 19:30 Apr 26, 2009 |

could you post a picture of whatcha got so far? for any perspective projection i don't see how you could avoid using collision rays. it would help to see a pic.

maximf
Posts: 31
Posted 19:40 Apr 26, 2009 |

 

thats the environment, and the target is moving around in it using random functions

 

 

semory
Posts: 4
Posted 20:31 Apr 26, 2009 |

ha ha ha panda3d has functions built in just for this. and i see you started...

http://www.panda3d.org/wiki/index.php/Clicking_on_3D_Objects

you should try it. that

pickerRay.setFromLens(base.camNode, mpos.getX(), mpos.getY())

sets the collision ray from the mouse position automatically for you, rather than having to calculate it ourselves from the lens parameters like we were trying to do.

just create the ray in the constructor, set a mouse1 event, and in the mouse1 event set the ray using the setFromLens function they talk about. then traverse the collision objects and check for a hit. the reason they use getTag and findNetTag is because one model loaded multiple times in the scene will yeild collision entries with the same name (the name of the model), and you can't tell them apart, so you have to tag each model with a unique name when you create them (I used bandit1, bandit2, bandit3 and the model name is bandit).

Last edited by semory at 20:43 Apr 26, 2009.
maximf
Posts: 31
Posted 20:43 Apr 26, 2009 |

thats works, mazal tov

 

thanks!