Help getting position on a texture

Started by cloak, January 31, 2013, 03:51:50 PM

Previous topic - Next topic

cloak

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:


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

EgonOlsen

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

#2
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.

EgonOlsen

Quote from: cloak on February 01, 2013, 08:15:36 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

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.