Sign in to follow this  

[XNA] ray picking and model scaling problems

Recommended Posts

Hi all,

At the moment I have the ray picking implemented as in from the xna triangle picking sample. The thing is that this is for use in a level editor so scaling, rotating and translating of my objects are necessary. I understand the need for inversing the model transform and transforming the ray by it so that it is moved into model space as apposed to moving each triangle of the model. When I was only translating an object it worked fine but now if I add scaling to the objects world transform the inversing of the ray behaves very strangely. Obviously I do a quick bounding sphere check first and to get around this I literally only passed the translate matrix of the object to inverse the ray and then tested against a bounding sphere proportionate to the objects scale. This made the ray pass the bounding sphere check correctly but now doesn't work correctly against the object as I presume its doing the triangle checks against the original sized model.

The question is do I multiply all of the vertices by the world transform to get them into the correct scale etc before ray testing or is there something I'm missing out about inversing the ray with the full world transform, including scaling and rotation, instead of just the translate. If I were to multiply each vertex surely that takes away the whole point of inversing the ray to make it faster in the first place?

Any help would be much appreciated.

Share this post

Link to post
Share on other sites
Not to worry, after sitting down and fiddling with it the entire morning I finally cracked it! I still cant believe the windows triangle picking example doesn't take into account the user wanting to scale or rotate on object.

Anyway, for anybody experiencing this problem in the future this is how I went about it:

[list][*]Inversing the ray by an objects world matrix that includes scaling(possibly also rotation) messes it up when it comes to doing a quick test against a bounding sphere. To overcome this create a new ray called boundingSphereRay or something and then transform it by the inverse of the objects translation matrix only.[*] The bounding sphere must then be transformed with uniform scaling, I found the 'TransformBoundingSphere' function in the picking sample(not the triangle picking sample) very useful for this. Although make sure that the center position of the sphere is set to to account for the rays inverse. Just to note I found it a good idea to add a translate boolean parameter to the function, false to have the returned sphere at and true to translate the sphere to the objects position for use with the bounding frustum test.[*]Finally you inverse your original ray by the objects world matrix(scaling and all) and test against the triangles.[/list]

Hopes this helps anybody in need and feel free to post if struggling.

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this