Write a Circular UIGestureRecognizer - uigesturerecognizer

I'm looking to create a jog wheel in an iPhone / iPad application. I know that you can subclass UIGestureRecognizer to write your own recognizers. Does anyone know how (mainly the maths behind it) to create one that would detect a circular movement, perhaps in combination with a pan gesture?
Thanks

this question isn't easy. I spend some time thinking of a possible solution:
I think what you need are some key properties you have to set:
The center of the circular movement (in this case no problem, because you know the center of the jog wheel)
A corridor in which the movement should happen.
so you need the inner radius and the outer radius.
Now you have something like this (unfortunately I haven't got enough reputation so only the link: http://img17.imageshack.us/img17/4416/bildschirmfoto20100721u.png
Now the maths behind this starts:
First of all you arrange the corridor in four quarters:
From 0° to 90°
From 90° to 180°
From 180° to 270°
From 270° to 360°
For each quarter you have to figure out when the finger is moving (let's say that the 0°-line is from the center point straight to the top):
if the finger is in the first quarter you know if the x changes to the left that the rotation must be anti-clockwise. If the x changes to the right the rotation must be clockwise.
Apply this logic for all quarters. Now you know if the jog wheel is moved clockwise or anti-clockwise. You have to make sure, that the finger is never leaving the corridor (if you test this logic and the movement stops because of leaving the corridor, make the corridor bigger - Thanks to CrystalSkull for his comment: Use 44px as a minimum width for the corridor to apply to the Human Interface Guidelines).
Sumary
So now you can conclude that you need a center point and a corridor the finger can move in.
You have to figure out in which quarter the finger is in and find out (using the x-value) if the rotation is clockwise or anti-clockwise.
I hope this helps you a little bit.

Related

Fluid is not touching container

I'm trying to create a fluid simulation on blender 2.92 but after the bake of the fluid, this one is not laying on the floor. It's floating at around 10cm above and also, it stays at 10cm from the walls.
Space between fluid and floor
Space between fluid and walls
Low res render
Does anybody know what I'm setting wrong ? The obstacle is inside a cube which has a solidify modifier applied and set up as Effector / Collision.
Any help will be appreciated
Did
Without having much to look at other than what you had provided, it's the resolution of your domain. When you look at your domain box, there should be a small "cube" in the corner. If you move the cube down under your floor to where the top of the cube is touching the floor, this will fix the gap between the floor and water. Just keep in mind, when you increase that resolution, that cube will get smaller. Meaning, you will need to adjust the position of it yet again. For the walls, you will need to widen your domain until the side of the cube is on the outside of the wall.
Think of the cube as a "safety boundary/perimeter" or something like that. Hopefully that all makes sense?

How can i handle a camera direction parallel to the y-axis my raytracer

I'm working on my raytracer and it seems I can't manage to handle the case where the direction vector of my camera is parallel to the vector (0,1,0).
I think it is linked to my way to compute the vector up and right for camera but I can't manage to find a work around.
Here is how I do it:
cam_up = vector_cross(cam_dir, {0, 1, 0});
camp_right = vector_cross(cam_right, cam_dir);
Can somebody enlighten me?
You have the correct formula for calculation of an orthogonal axis from a single cameraOut vector. However, as has been stated this formula will not account for the camera roll, which could be any direction in the plane perpendicular to the camera direction. This will be apparent when moving a camera across the pole (y-axis) as there will be undesireable behavior (yes it will be correctly aimed, but no doubt the roll won't be desired).
For more information, look into gimbal lock.
The roll itself is not really incorrect, however in reality for this camera transition to be smooth and appear correct (rather than suddenly flip or spin as it's direction becomes 0,1,0), you need to correct any roll incurred. This is a rotation about the cameraOut axis and ideally should be relative to the previous cameraAlong. This means in order to maintain the correct roll (or perceived correct roll) you need to consider the camera POSE (position and orientation) from the previous frame and ensure the roll is mitigated. Of course, if the camera doesn't move (i.e. your rendering a frame with a static camera position) you do not have a previous camera state so the position cannot be calculated and instead must be explicitly defined as part of the scene definition.
Personally I store an entire orthogonal axis for a camera so the orientation and roll is always clearly defined. This is only for completeness, to be honest you don't need to store the entire axis, 2 vectors cameraOut and cameraAlong (the third one being cameraUp) are enough. cameraAlong is dependant on the handed-ness of your coordinate system (e.g. for initial camera position say position (0,0,0) in left hand coordinate system, the cameraAlong direction will be in the right direction in relation to the viewer, for right hand system the cameraAlong would be the other way around. The cameraUp and cameraOut would are the same in both coordinate systems).
Hope this helps.
P.S This isn't ray tracing specific and the same principles apply for OpenGL/DirectX or any 3D representation.

Collision detection on diagonal lines in cocos2d

I'm working on a small iPhone/iPad game with the Cocos2d framework. Basically the idea of the game is very simple, there a ball which is moving at a certain speed. The user can draw a line so that the ball bounces in the opposite direction.
Now I've already have the ball moving and the user can draw a line also. (When he draw a line it becomes a member of an array and dissapears a few seconds later.)
But the question is how to detect a collision between the ball and a line? The line doesn't need to be horizontal or vertical, it can be diagonal also. It all depends on how the user draws this line.
I've the coordinates of the line: see the answer to my previous question: CGRect with an angle
Can you guys point in me right direction?
Thanks in advance!
You can use box2d to detect collisions. Actually, if your ball and these lines will have bodies in b2World, you will be able not to check collisions by yourself. All changes, bounces, etc. physics will done for you. You will only have to synchronize object's view position (also you can synchronize angle) according to the position of it's body in physical world.
You can use the Pixel perfect collision so when ball and line collide it is not check the rect but it check the pixel collision...
in this forum you got that you want..
http://www.cocos2d-iphone.org/forum/topic/18522/page/3

How to change the rotation order in cocos3d

I'm a fresh in cocos3d, now I have a problem.
In cocos3d, I want to rotate a node. I got the angles in x axis, y axis, z axis, then I used the property:rotation to rotate, like this:
theNodeToBeRotated.rotation = cc3v(x,y,z);
But I found out it didn't rotate as I expected, because the document said the rotate order is y-x-z.
I want to change the order to x-y-z. Can anyone let me know how?
You might need to clarify further regarding the following: "it didn't rotate as I expected"
OpenGL ES (and ergo, cocos3D) uses the y-axis as up so the rotation order is still x-y-z. If you are importing a model, you then need to take into account the 3D editor's co-ordinate system and adapt accordingly.
If you are not used to working with three-dimensional representations, the leap from 2D to 3D can be a significant hurdle. Within Cocos3D:
the x-axis is positive on the right and negative on the left
the y-axis is positive upwards and negative downwards
the z-axis is positive moving towards you and negative moving away from you
Envisage those three lines of axis, or even better, a piece of string.
If you are rotating around the x-axis, hold the string horizontally from left to right: the object would rotating towards you or away from you.
If you are rotating around the y-axis, hold the string vertically from feet to head: the object would rotate as if like a revolving door.
If you are rotating around the z-axis, hold one end close to your chest and the other end as far away as possible: the object would rotate similar to a clock face.
-- Update
I heavily wouldn't recommend changing the rotation order as it is the OpenGL standard to use Y-X-Z. If you wish to modify it, take a look at CC3GLMatrixMath and look for kmMat4RotationYXZ - there is also kmMat4RotationZYX. If you want to have X-Y-Z, you would need to construct your own rotation matrix and update accordingly in CC3GLMatrix and CC3GLMatrixMath.
As a reference, you also have the OpenGL Red book - it should have some suggestions for you.

Drag+Drop with physical behaviour

I'd like to implement a dragging feature where users can drag objects around the workspace. That of course is the easy bit. The hard bit is to try and make it a physically correct drag which incorporates rotation due to torque moments (imagine dragging a book around on a table using only one finger, how does it rotate as you drag?).
Does anyone know where I can find explanations on how to code this (2D only, rectangles only, no friction required)?
Much obliged,
David
EDIT:
I wrote a small app (with clearly erroneous behaviour) that I hope will convey what I'm looking for much better than words could. C# (VS 2008) source and compiled exe here
EDIT 2:
Adjusted the example project to give acceptable behaviour. New source (and compiled exe) is available here. Written in C# 2008. I provide this code free of any copyright, feel free to use/modify/whatever. No need to inform me or mention me.
Torque is just the applied force projected perpendicular to a vector between the point where the force is applied and the centroid of the object. So, if you pull perpendicular to the diameter, the torque is equal to the applied force. If you pull directly away from the centroid, the torque is zero.
You'd typically want to do this by modeling a spring connecting the original mouse-down point to the current position of the mouse (in object-local coordinates). Using a spring and some friction smooths out the motions of the mouse a bit.
I've heard good things about Chipmunk as a 2D physics package:
http://code.google.com/p/chipmunk-physics/
Okay, It's getting late, and I need to sleep. But here are some starting points. You can either do all the calculations in one coordinate space, or you can define a coordinate space per object. In most animation systems, people use coordinate spaces per object, and use transformation matrices to convert, because it makes the math easier.
The basic sequence of calculations is:
On mouse-down, you do your hit-test,
and store the coordinates of the
event (in the object coordinate
space).
When the mouse moves, you create a
vector representing the distance
moved.
The force exterted by the spring is k * M, where M is the amount of distance between that initial mouse-down point from step 1, and the current mouse position. k is the spring constant of the spring.
Project that vector onto two direction vectors, starting from the initial mouse-down point. One direction is towards the center of the object, the other is 90 degrees from that.
The force projected towards the center of the object will move it towards the mouse cursor, and the other force is the torque around the axis. How much the object accelerates is dependent on its mass, and the rotational acceleration is dependent on angular momentum.
The friction and viscosity of the medium the object is moving in causes drag, which simply reduces the motion of the object over time.
Or, maybe you just want to fake it. In that case, just store the (x,y) location of the rectangle, and its current rotation, phi. Then, do this:
Capture the mouse-down location in world coordinates
When the mouse moves, move the box according to the change in mouse position
Calculate the angle between the mouse and the center of the object (atan2 is useful here), and between the center of the object and the initial mouse-down point. Add the difference between the two angles to the rotation of the rectangle.
This would seem to be a basic physics problem.
You would need to know where the click, and that will tell you if they are pushing or pulling, so, though you are doing this in 2D, your calculations will need to be in 3D, and your awareness of where they clicked will be in 3D.
Each item will have properties, such as mass, and perhaps information for air resistance, since the air will help to provide the motion.
You will also need to react differently based on how fast the user is moving the mouse.
So, they may be able to move the 2 ton weight faster than is possible, and you will just need to adapt to that, as the user will not be happy if the object being dragged is slower than the mouse pointer.
Which language?
Here's a bunch of 2d transforms in C

Resources