Perform 2D vertices rotation in update function or render function - rotation

I'm starting to build a 2D game and have some confusion around whether I should perform rotation in the update function or the render function.
The problem is this;
I have a triangle, consisting of three vertices. The triangle has a rotation value in degrees.
If I rotate the vertices in the update function, the triangle rotates forever because each update applies the rotation over and over.
Therefore, I decided to not rotate the vertices in the update function and instead perform the rotation, based on the original vertices, in the render function.
This works, however now I have a different problem. The vertices are not actually where they appear to be. Therefore I can not use the vertices to perform collision detection, etc.
The only idea that I have to resolve this is that I could perform the rotation in the update function but have two sets of vertices; one for the original vertices, one of the rotated vertices. Then use the rotated vertices in collision detection calculations - this smells hacky and inefficient though!
I've put together a codepen demonstrating applying rotation in the render function; https://codepen.io/anon/pen/pPRjLq
Use arrow keys to rotate
So, should I rotate in render or update? If render then how do I keep vertices up to date? If update then how do I prevent infinite rotations?
Any help from experienced people would be greatly appreciated - thanks!

Have the rotation occur in the update method as you are updating the rotation of the actor.
You do not need to remember two vertices either, it would be a better alternative to just have current vertices of the actor and the rotation in radians, original vertices do not need to be remembered.
Thinking ahead, collision detection should not be dependent on the position vectors of an actor, better yet still, have a rectangle/AABB defined for it instead, which would need to be updated/scaled when rotation is applied to the actor.
If you did use a custom AABB for detection it could be set to the position of the actor vector by default, but by separating the constructs it opens up a lot of potential such as changing actor hit bounds x/y/width/length at run-time; it should not be coupled with actor x/y/width/height which should relate to the graphical representation of it.
To stop the infinite rotation you could have a boolean to state if left key up and one similarly for the right key, and something like below;
update(delta) {
if(keyLeftDown) {
// apply rotation as rotation * delta / desiredFPS
} else if (keyRightDown) {
//
}
}
Hope this helps.

Related

3D algorithm for clamping a model to view frustum

What would be an efficient approach for constraining an object so that it's always at least partially intersecting the view frustum?
The use case is that when viewing a model I want to clamp camera panning, as well as model translation, so that the view frustum is never looking at empty space.
One approach I tried was to wrap the model objects in bounding volumes, then enforce the constraint when those fall outside the frustum. I've tried bounding boxes so far, but am considering using a minimal convex hull.
The problem is that that when you zoom in close enough, it's still possible to be looking at empty space within the boundary, as shown in the attached diagram.
This is for a WebGL application, so needs to be fairly efficient in JavaScript, and also for thousand of vertices.
Ideally you would have a aabb tree of your mesh, and then you can recursively project on camea/screen until you get an intersection ?
http://www.codersnotes.com/algorithms/projected-area-of-an-aabb
edit: it's just frustum culling algo against aabtree does anyway, so looking for optimized solution, is looking for optimized frustum culling things
https://fgiesen.wordpress.com/2010/10/17/view-frustum-culling/
http://www2.in.tu-clausthal.de/~zach/teaching/cg_literatur/vfc_bbox.pdf
As general approximation is possible I would try the point cloud. First create a list of points - either by every Nth mesh vertex or every Nth face centre. With N being for example 10. Once you have this point array all you do is check if any of points is in frustum while updating its orientation. If not then this means that user moved or rotated the camera too much and you need to restore last acceptable orientation.
I know that this may seem quite ridiculous but I think that it is fairly easy to implement and frustum checking of vertex is just couple of multiplications per plane. It will be not perfect though.
You can also make frustum a little smaller to ensure that there is some border around the object.

WebGL Change Shape Animation

I'm creating a 3D globe with a map on it which is supposed to unravel and fill the screen after a few seconds.
I've managed to create the globe using three.js and webGL, but I'm having trouble finding any information on being able to animate a shape change. Can anyone provide any help? Is it even possible?
(Abstract Algorithm's and Kevin Reid's answers are good, and only one thing is missing: some actual Three.js code.)
You basically need to calculate where each point of the original sphere will be mapped to after it flattens out into a plane. This data is an attribute of the shader: a piece of data attached to each vertex that differs from vertex to vertex of the geometry. Then, to animate the transition from the original position to the end position, in your animation loop you will need to update the amount of time that has passed. This data is a uniform of the shader: a piece of data that remains constant for all vertices during each frame of the animation, but may change from one frame to the next. Finally, there exists a convenient function called "mix" that will linearly interpolate between the original position and the end/goal position of each vertex.
I've written two examples for you: the first just "flattens" a sphere, sending the point (x,y,z) to the point (x,0,z).
http://stemkoski.github.io/Three.js/Shader-Attributes.html
The second example follows Abstract Algorithm's suggestion in the comments: "unwrapping the sphere's vertices back on plane surface, like inverse sphere UV mapping." In this example, we can easily calculate the ending position from the UV coordinates, and so we actually don't need attributes in this case.
http://stemkoski.github.io/Three.js/Sphere-Unwrapping.html
Hope this helps!
In 3D, anything and everything is possible. ;)
Your sphere geometry has it's own vertices, and basically you just need to animate their position, so after animation they are all sitting on one planar surface.
Try creating sphere and plane geometry, with same number of vertices, and animating sphere's vertices with interpolated values of sphere's and plane's original values. That way, on the start you would have sphere shape and in the end, plane shape.
Hope this helps, tell me if you need more directives how to do it.
myGlobe.geometry.vertices[index].position = something_calculated;
// myGlobe is instance of THREE.Mesh and something_calculated would be THREE.Vector3 instance that you can calculate in some manner (sphere-plane interpolation over time)
(Abstract Algorithm's answer is good, but I think one thing needs improvement: namely using vertex shaders.)
You make a set of vertices textured with the map image. Then, design a calculation for interpolating between the sphere shape and the flat shape. It doesn't have to be linear interpolation — for example, one way that might be good is to put the map on a small portion of an sphere of increasing radius until it looks flat (getting it all the way will be tricky).
Then, write that calculation in your vertex shader. The position of each vertex can be computed entirely from the texture coordinates (since that determines where-on-the-map the vertex goes and implies its position) and a uniform variable containing a time value.
Using the vertex shader will be much more efficient than recomputing and re-uploading the coordinates using JavaScript, allowing perfectly smooth animation with plenty of spare resources to do other things as well.
Unfortunately, I'm not familiar enough with Three.js to describe how to do this in detail, but all of the above is straightforward in basic WebGL and should be possible in any decent framework.

OpenGL ES/real time/position of any vertex that is displayed?

I'm currently dealing with OpenGL ES (2, iOS 6)… and I have a question
i. Let be a mesh that has to be drawn. Moreover,
ii. I can ask for a rotation/translation so that the point of view changes.
So,
how can I know (in real time) the position of any vertex that is displayed?
Thank you in advance.
jgapc
It's not entirely clear what it is you are after, but if you want to know where your object is after doing a bunch of rotations and translations, then one very easy option, if you perform these changes in your program code instead of in the shader, is to simply take the entire last row or column of your transformation matrix (depends if you are using row or column major matrices) which will be the final translation of your object's center as a coordinate vector.
This last row or column is the same thing as multiplying your final transformation matrix by your object's local coordinate center vector, which is (0,0,0,1).
If you want to know where an object's vertex is, rather than the object's center, then multiply that vertex in local coordinate space by the final transformation matrix, and you will get the new coordinate where that vertex is positioned.
There are two things I'd like to point out:
Back-face culling discards triangles, not vertices.
Triangles are also clipped so that they're within the viewing frustum.
I'm curious as to why you care about what is not displayed?

2D geometry outline shader

I want to create a shader to outline 2D geometry. I'm using OpenGL ES2.0. I don't want to use a convolution filter, as the outline is not dependent on the texture, and it is too slow (I tried rendering the textured geometry to another texture, and then drawing that with the convolution shader). I've also tried doing 2 passes, the first being single colorded overscaled geometry to represent an oultine, and then normal drawing on top, but this results in different thicknesses or unaligned outlines. I've looking into how silhouette's in cel-shading are done but they are all calculated using normals and lights, which I don't use at all.
I'm using Box2D for physics, and have "destructable" objects with multiple fixtures. At any point an object can be broken down (fixtures deleted), and I want to the outline to follow the new outter counter.
I'm doing the drawing with a vertex buffer that matches the vertices of the fixtures, preset texture coordinates, and indices to draw triangles. When a fixture is removed, it's associated indices in the index buffer are set to 0, so no triangles are drawn there anymore.
The following image shows what this looks like for one object when it is fully intact.
The red points are the vertex positions (texturing isn't shown), the black lines are the fixtures, and the blue lines show the seperation of how the triangles are drawn. The gray outline is what I would like the outline to look like in any case.
This image shows the same object with a few fixtures removed.
Is this possible to do this in a vertex shader (or in combination with other simple methods)? Any help would be appreciated.
Thanks :)
Assuming you're able to do something about those awkward points that are slightly inset from the corners (eg, if you numbered the points in English-reading order, with the first being '1', point 6 would be one)...
If a point is interior then if you list all the polygon edges connected to it in clockwise order, each pair of edges in sequence will have a polygon in common. If any two edges don't have a polygon in common then it's an exterior point.
Starting from any exterior point you can then get the whole outline by first walking in any direction and subsequently along any edge that connects to an exterior point you haven't visited yet (or, alternatively, that isn't the edge you walked along just now).
Starting from an existing outline and removing some parts, you can obviously start from either exterior point that used to connect to another but no longer does and just walk from there until you get to the other.
You can't handle this stuff in a shader under ES because you don't get connectivity information.
I think the best you could do in a shader is to expand the geometry by pushing vertices outward along their surface normals. Supposing that your data structure is a list of rectangles, each described by, say, a centre, a width and a height, you could achieve the same thing by drawing each with the same centre but with a small amount added to the width and height.
To be completely general you'd need to store normals at vertices, but also to update them as geometry is removed. So there'd be some pushing of new information from the CPU but it'd be relatively limited.

Morph an OpenGL sphere

This is about how to create an OpenGL irregular sphere. I've searched the web, but all the documents are telling how to create a regular sphere.
The effect I need is to simulate a bubble, and when the user touch the bubble, it should act on the touch, and the sphere bubble should change its shape on the touch position. Say, concave the touch part.
I can't figure out a feasible way to do this kind of simulation. Should I change the vertex position of touch part ? Or can I use a shader to implement this effect ?
At the same time, I don't know how can simulate the concave realistically, is there any math procedure to describe such a process ?
Thanks !
First, you'll want to use a geodesic-style sphere rather than one create via lat / long vertices. That will deform more predictably.
From there, there are several ways to do it. One way I could think of would be to create graph where each node indexes into a vertex in your mesh, and each node contains links to its neighbors. Then, when a vertex is pressed, it can "pull" its neighbors in with it. A cheap way would be to simply relocate the pressed vertex and then pull neighbors toward the new position, maintaining the original distance (very simple vector math). Then, repeat for those neighbors until the distance each neighbor is pulled reaches a sufficiently small threshold.
Once complete, the mesh will likely have to be reuploaded to GPU.
When I morph an object I just use an animation from the start vertex to the end vertex. The animation can have about 200 frames or so. I'm not sure how I can caclculate the steps from the start vertex to the end vertex. Maybe there is some trigonomic function? In your example I would create a sphere with the button and use it as a target frame. I'm not sure how a shader can help you here.

Resources