Author Topic: calcMinDistance returns 1.0E12  (Read 698 times)

Offline javamak

  • byte
  • *
  • Posts: 13
    • View Profile
calcMinDistance returns 1.0E12
« on: November 24, 2015, 12:35:31 pm »
Hello,

I am new to the forum and new to 3D programming as well. Trying to get good understanding of JPCT. What is wrong with the below code.

Code: [Select]
float distance = world.calcMinDistance(cam.getPosition(), obj.getTransformedCenter(), 10000 );
The obj is a cube and visible on screen.
Cam position = (0.0,0.0,-170.0)
ob center = (-3.973643E-8,0.0,0.0)

Thanks.

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11748
    • View Profile
    • http://www.jpct.net
Re: calcMinDistance returns 1.0E12
« Reply #1 on: November 24, 2015, 01:12:07 pm »
The returned value is the COLLISION_NONE-constant. For this to work, the object has to have a proper collision mode set.

Code: [Select]
obj.setCollisionMode(Object3D.COLLISION_CHECK_OTHERS);

Then, obj.getTransformedCenter() isn't a direction vector. The actual direction vector is the (normalized) vector from the camera's position to the center, like:

Code: [Select]
SimpleVector dir=new SimpleVector(obj.getTransformedCenter());
dir.sub(cam.getPosition());
dir=dir.normalize();

Offline javamak

  • byte
  • *
  • Posts: 13
    • View Profile
Re: calcMinDistance returns 1.0E12
« Reply #2 on: November 24, 2015, 03:34:00 pm »
Thanks for the quick reply it works. Didn't know subtracting the two vectors gives the direction. Where can I learn such things? any recommendation?

Thanks.

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11748
    • View Profile
    • http://www.jpct.net
Re: calcMinDistance returns 1.0E12
« Reply #3 on: November 24, 2015, 05:30:10 pm »
It's basic vector math. Usually, you think about these positions as points in space in this context, but you can also see them as a vector from the origin to that point. Just draw this scene in 2D in paper and you'll see, why the difference vector is the vector from the camera's position to the center of the object. If you normalize it, you have your needed direction vector.

Offline javamak

  • byte
  • *
  • Posts: 13
    • View Profile
Re: calcMinDistance returns 1.0E12
« Reply #4 on: November 25, 2015, 05:39:10 am »
Thank you.  :)