Author Topic: Help getting position on a texture  (Read 686 times)

cloak

  • Guest
Help getting position on a texture
« on: January 31, 2013, 03:51:50 pm »
Hi,

I am still very new to JPCT-AE along with anything OGL.

I have a sphere, and I am translating the texture matrix around with touch events to make it appear as if it is moving. I am doing it this way so that I can maintain certain perspectives etc.

The problem I am having is the following. I am trying to get some sort of tapped position on my texture that I have wrapped around this sphere. I am using the following for calculating where I tapped:

Code: [Select]
final SimpleVector worldCoords = Interact2D.reproject2D3D(camera, frameBuffer, motionEventX, motionEventY);
worldCoords.matMul(camera.getBack().invert3x3());
worldCoords.add(camera.getPosition());
   
final SimpleVector directionFromCameraPosition = worldCoords.calcSub(camera.getPosition()).normalize();
final float distanceTo = world.calcMinDistance(worldCoords, directionFromCameraPosition, 10000);

final SimpleVector collisionPointCoords = new SimpleVector(directionFromCameraPosition);
collisionPointCoords.scalarMul(distanceTo);
collisionPointCoords.add(worldCoords);

If I add a primitive Object3d to test and I translate it to collisionPointCoords, it adds it to the world exactly where the collision point should be on my larger sphere.

How would I correlate this collision point vector to a point (xy) on my texture?

Any help would be greatly appreciated,
Thanks

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11748
    • View Profile
    • http://www.jpct.net
Re: Help getting position on a texture
« Reply #1 on: January 31, 2013, 08:30:47 pm »
Interesting question... ;D I haven't tried this myself, but it should work somehow like this:

  • add a collision listener to the sphere to get the polygon that calcMinDistance hits
  • with that polygon, go to the PolygonManager and get the texture coordinate as well as the vertex coordinates in world space
  • texture coordinates are linear in world space, so with your calculated coordinate of the collision point, the transformed vertices and the texture coordinates at these vertices, you should be able to calculate the corresponding texture coordinate
  • multiply the result with your texture matrix

Might work, might be total nonsense...


cloak

  • Guest
Re: Help getting position on a texture
« Reply #2 on: February 01, 2013, 08:15:36 am »
Thanks for the prompt and helpful reply Egon, much appreciated. I will try your suggestion and give feedback.

Why would PolygonManager getTextureUV return null? I checked the javadocs and it doesn't specify. The other examples I have found of this methods' usage do not bother to check for null. What am I missing? getTransformedVertex works as expected, then when I call getTextureUV I am getting null for that polygonID and vertexNumber(s).

Edit: I got getTextureUV to work by removing my call to strip() on my sphere object.
« Last Edit: February 01, 2013, 09:17:04 am by cloak »

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11748
    • View Profile
    • http://www.jpct.net
Re: Help getting position on a texture
« Reply #3 on: February 01, 2013, 10:34:46 am »
Edit: I got getTextureUV to work by removing my call to strip() on my sphere object.
I see. I should mention it in the docs, i guess...

cloak

  • Guest
Re: Help getting position on a texture
« Reply #4 on: February 01, 2013, 03:17:34 pm »
Thanks a ton Egon, a bit of playing around and your suggestion worked perfectly for me ;D Multiplied the textureUV values with my texture matrix.  From there I worked out a mean value for the xy coords (good enough). Then used those values as percentages against my texture size to work out xy.