Scalable Vertex Transformations

Started by
4 comments, last by RastaRunna 10 years, 8 months ago

So I've been working on the collision detection portion of my 2D physics engine (just finished implementing GJK with simplex caching), however throughout the process I've been curious about the most efficient and scalable means for transforming my polygon vertices. Currently I transform only the cached support vertices of my simplex into world coordinates at the beginning of GJK (to get an updated copy of the simplex assuming the polygons have moved between frames), and then I transform each newly added support vertex into world coordinates. This equates to roughly 12 or so transformations per candidate collision pairs.

Though this seems more scalable than transforming all vertices of each polygon per frame, doing it all up front would allow me to take advantage of parallelization (whether on the GPU or not).

Any suggestions on when to transform vertices; as needed vs batched in parallel (pre-computation and perhaps again at render).

Thanks!

Advertisement

Clarifying my question a little further. The root of the question really is, what is the best way (with respect to performance and/or best practice) to manage shape vertices in my physics engine. Store them all in local coordinate space (relative to the attached shape body) and then transform to global (or other coordinate spaces) as needed. --OR-- Store them all in global coordinate space and update *each* once, maybe twice(since they'll need to be updated anyways at render, why not do it all up front).

The first is definitely a lazy approach, taking advantage of the notion that perhaps some vertices don't even need to be updated per frame, and seems scalable in that not ALL vertices on a given shape are even used (with exception to rendering).

The second, while eager, allows parallelization by throwing all vertices and their respective transformations at the GPU which would be really quick, though somewhat wasteful (since some vertices aren't needed in given algorithms). Possibly quicker than sparse computations on the CPU? Additionally, this would clean up my internal code (such as GJK) without messy transformations everywhere.

I realize this is probably best determined with some benchmarking / tests and depends on what I'm using it for. Though I'm highly skeptical that no one has run into this question before.


Clarifying my question a little further. The root of the question really is, what is the best way (with respect to performance and/or best practice) to manage shape vertices in my physics engine. Store them all in local coordinate space (relative to the attached shape body) and then transform to global (or other coordinate spaces) as needed. --OR-- Store them all in global coordinate space and update *each* once, maybe twice(since they'll need to be updated anyways at render, why not do it all up front).

If I understood you correctly, then:

1. If a mesh is animated incrementally, then the errors made due to definite resolution numbers will accumulate, and that different from vertex to vertex. That means that the shape of the mesh will be lost over time. So usually, animated meshes start from a fixed form and get fully transformed in each frame.

2. Not animated meshes don't suffer from the problem mentioned above. So *they* can be stored w.r.t. the global co-ordinate system. This is in fact the reason why static and dynamic geometry is distinguished.

Thanks haegarr for the reply.

1. ... . So usually, animated meshes start from a fixed form and get fully transformed in each frame.

So does this generally tend to happen all up front pre-computation (or pre-use) of each of the vertices which would allow me to batch the transformations onto say the GPU; or only as the transformations are needed (during computation), which would likely not end up hitting all vertices, but also will have redundancies and not be as parallelized.

2. Not animated meshes don't suffer from the problem mentioned above. So *they* can be stored w.r.t. the global co-ordinate system. This is in fact the reason why static and dynamic geometry is distinguished.

Really good point; I'll definitely adopt this.


So does this generally tend to happen all up front pre-computation (or pre-use) of each of the vertices which would allow me to batch the transformations onto say the GPU; or only as the transformations are needed (during computation), which would likely not end up hitting all vertices, but also will have redundancies and not be as parallelized.

Yes, incremental computation will suffer from imprecision regardless whether it is done on CPU or GPU and also regardless whether it is done at runtime or in a pre-process step. (ATM I don't see why this should depend on data parallelization or such, so be patient with me if I tell you things you already know ;))

If you apply transformations incrementally

vn := vn-1 * Tn = ( ... ( ( v0 * T1 ) * T2 ) * ... ) * Tn

then the original mesh (symbolized by the vertex position v0) exists only at the beginning of the transformation chain. Each step transforms the mesh like you want plus a bit of inaccuracy (what you obviously don't want). After some steps (how many depends on several factors and cannot be predicted exactly) the shape of the mesh starts to look deformed, even if the transformations (intentionally should) only rotate and translate it.

If you instead do a single step transformation with concatenated incremental transformations

vn' := v0 * Tn' = v0 * ( ... ( ( T1 * T2 ) * ... ) * Tn )

where, please notice, Tn' is tagged to distinguish it from Tn, then the computation always uses the original mesh for transformation.

Please notice that matrix multiplication is associative, so that the formulas for vn and vn' are mathematically (i.e. with indefinite precision) the same.

However, now the overall transformation Tn' is computed incrementally, and because this way of computation suffers from inaccuracy, you seem not to have won anything. But in fact you have won something, because there are mathematical rules that the transformation must obey, and these rules can be used to reduce the inaccuracies to a minimum. See, you don't have this possibility with the mesh, but only with the transformation.

This is pretty interesting. I think the method I'm going for is more like the second one you mentioned (single step transformation with concatenated incremental transformations). However the root of my question really is:

Assuming then that I have the original mesh at the beginning of each frame, and some transformation Tn' which I would like to apply to it. There are different times throughout the current frame at which I need to apply that transformation to a given vertex in order to use it. For example, the transformation could be applied to every vertex and stored in a separate structure at the beginning of each frame to allow all other computations throughout the frame (such as collision detections, physics integration, and rendering) to use this same vertex. Or the transformation could be applied each time it is needed for a given vertex (via the same process of updating vertices, transform the original by current transform to get a transformed copy). Given these two times for updating, which is generally done. The first, irrespective of accuracy allows for parallelization, the second depends on the parallelizability of the context in which it is used.

This topic is closed to new replies.

Advertisement