Detecting accurate collision of two views in ios programming? - ios

I am making a "air hockey" type of game, I need to detect the collision of handle and puck in that. I am using the CGRectIntersectRect function for that but it is not accurate as when the frames from the edges collide, a collision is detected when the circles of puck and handle are not yet collided.
I hope I am making my point clear, so, any guidence on how I can achieve a more accurate collision of two circular images ?

I'd give a shot to UIKit Dynamics collision detection engine. See example here.
Unfortunately it's iOS 7+, so if you plan to support iOS 6, you need to look for another solution.

Taking the tip from #H2CO3, I implemented a function of my own that takes the center of circle 1 (x1,y1) and centre of circle 2 (x2,y2) and sum of radius of two circles (r) as parameters.
Calculates the distance between (x1,y1) and (x2,y2), if the distance, say d, is less than r then the circles are intersecting.
Have a look at the implementation, works perfectly:
-(BOOL)WhenCircleDistanceIsLessThanRadiusX1:(int)x1 Y1:(int)y1 X2:(int)x2 Y2:(int)y2 R:(int)r
{
int d = (int) sqrt(pow((x2-x1),2) + pow((y2-y1),2));
return(d<=r);
}

Related

How to determine the rotation angle?

I'm trying to implement a russian roulette game and want it to brute-force the solution for it. Here is my problem. I'm going to hard code the relative angles of the numbers on the wheel (eg. there are 36 numbers and each number would have 10 degree offset to each other, the one on the top, 12 o'clock position, will have the 0 and the next 10 and vice versa). I will rotate the wheel randomly and then determine the rotation of it based on some values that I can calculate (startPosition to finishedPosition). The wheel is an ImageView. Is there a way to actually do this? For example, get the top left x,y pos for its start and end, then by some formula to calculate how much it rotated. Or is there a better way to do this? There is not much of a source code to show it, so this is more like a mathematical question rather than a swift one. Any feedback is much appreciated.
To calculate rotation, you need coordinates of three points: start location sx, sy, end location ex, ey of the same point after rotation and center of rotation cx, cy
Then you can find angle using atan2 function
rot_angle = atan2((ex-cx)*(sx-cx)+(ey-cy)*(sy-cy), (ex-cx)*(sy-cy)-(ey-cy)*(sx-cx))
Note - I used argument order (x,y) from here, while most languages use reverse order (y,x), so check what order you really need (I have no experience in IOS languages). Also result value might be in radians or in degrees (above link doesn't specify it clearly)
Your question doesn't make much sense. If you rotate the wheel randomly, calculate the random value as an angle. If you want to change the previous rotation by some random angle, then do the math on the starting rotation and ending rotation. That is just adding and subtracting angles (modulo 2π). Then you will know how far it is rotated, and not have to calculate it.
Assuming you're talking about a roulette wheel, and not "Russian Roulette" (In American English at least, that term involves pointing a loaded revolver at your head) you'll need to track both the wheel rotation and the ball rotation. To apply the rotation to the wheel, you'll just take the image of the wheel and rotate it on the Z axis around it's x/y center point.
To plot the ball, you'll need to use trig to calculate the center of the ball based on the radius of the track the ball follows and the angle. But again, always track the angle, and then convert the angle to an x/y center point for the ball to plot it. Don't forget the angle and then have to convert back from the ball position to its angle. That's silly.

Moving a drawing

This is an example of a drawing for the program I use
DrawCircle(mousePos.x, mousePos.y, mousePos.z, 650, ARGB(255, 255, 0, 0))
pretty simple x,y,z location radius of the circle and color, in this example it will draw a circle around my mouse and if my mouse moves the circle moves with it as is should, however what I would like to do is know how to draw a circle at lets say stationary position x,y,z and make the circle move from said position to new position a,b,c at x speed. sure I can just disable the draw at the starting point and redraw it at the destination point but I want the circle to visually move from point a to point b at speed x and I'm not sure what math I would need to be able to do this, furthermore if I was to draw a line how could I rotate that line in place so it looked like lets say helicopter blades spinning? Any help is appreciated thank you.
Not sure about LUA per se, but the solution to your problem is based around vector mathematics. LUA may have provide transformation functions to move a point in 3D space... not sure. As for the rotor blade question, if you are plotting the rotor blade in a 2D plane, you simply need a bit of trigonometry. There are lots of examples on the web, e.g.: trig example

Defining a 3D scene from a photo of a circle

Given a photo containing a circle, for example this photo of a fountain:
is it possible to define the 3D position and rotation of the fountain in relation to the camera?
I realise we have to define the scale, so lets say the fountain is 2m wide (the diameter of the circle consisting of the inner rim of the fountain is 2m).
So assuming the circle is a perfect circle, and defining the diameter to 2m, is it possible to determine how the circle and the camera relate spatially? I dont know any camera matrix or anything, the only information i have is the picture.
I specifically want to determine the 3D coordinates of a given pixel on the rim of the fountain.
What would be the math and/or OpenCV code to do this?
Circle with perspective is an ellipse. So you basicly you need an ellipse detector.
This algorithm should work:
Detect all ellipses in the given image.
Filter ellipses that you think they are not a circles in origin. (This is not possible using just 1 Camera so you have to depend on previous knowledge. Something like that you knows that you are taking a photo for a circle).
mmm I stopped typing here and bring a paper&pen and started figuring how to estimate the Homography and it is not that easy! you should deal with the circle a special case of an ellipse and then try to construct a linear system of equations. However, I made quick googling :
https://www.researchgate.net/publication/265212988_Homography_estimation_using_one_ellipse_correspondence_and_minimal_additional_information
http://www.macs.hw.ac.uk/bmvc2006/papers/306.pdf
Seems very interesting topic, I am going to spare sometimes on it later!

Detection of pattern of circles using opencv

I have to detect the pattern of 6 circles using opencv. I have detected the circles and their centroids by using thresholding and contour function in opencv.
Now I have to define the relation between these circles in a way that should be invariant to scale and rotation. With this I would be able to detect this pattern in various views. I have to use this pattern for determining the object pose.
How can I achieve scale/rotation invariance? Do you have any reference I could read about it?
To make your pattern invariant toward rotation & scale, you have to normalize the direction and the scale when detecting your pattern. Here is a simple algorithm to achieve this
detect centers and circle size (you say you have already achieved this - good!)
compute the average center using a simple mean. Express all the centers from this mean
find the farthest center using a simple norm (euclidian is good enough)
scale the center position and the circle sizes so that this maximum distance is 1.0
rotate the centers so that coordinates of the farthest one is (1.0, 0)
you're done. You are now the proud owner of a scale/rotation invariant pattern detector!! Congratulations!
Now you can find patterns, transform them as suggested, and compare center position & circle sizes.
It is not entirely clear to me if you need to find the rotation, or merely get rid of it, or detect if the circles actually form the pattern you linked. Either way, the answer is much the same.
I would start by finding the two circles that have only one neighbour. For each circle centroid calculate the distance to the closest two neighbours. If the distances differ in more than say 10%, the centroid belongs to an "end" circle (one of the top ones in your link).
Now that you have found the two end circles, rotate them so that they are horizontal to each other. If the other centroids are now above them, rotate another 180 degrees so that the pattern ends up in the orientation you want.
Now you can calculate the scaling from the average inter-centroid distance.
Hope that helps.
Your question sounds exactly like what the SURF algorithm does. It finds groups of interest and groups them together in a way invarant to rotation and scale, and can find the same object in other pictures.
Just search for OpenCV and SURF.

Box2d - actionscript, java, andengine - body position

I've lost in tonnes of information for box2d and it's ports.
But I have very simple question, for which I can't get any informations anywhere.
How to properly calculate body position. Let say I have sprite on screen in position (10, 20).
Why every tutorial for box2d computing this different ?
For example:
- processing (from processing.org) using function coordPixelToWorld and coordWorldToPixel for calculate beetwen world and screen - which is a little compilcated function;
- andengine have similar function for converting beetwen world and screen;
- actionscript - here I don't understand why but every tutorial has getting screen coords and dividing it by scale function.
I ask this questions because all of the above have one common point: screen coords for 0,0 are in left top corner. Is box2d written different for every port ?
I will be grateful for explanation.
Update
I don't have very big troubles with box2d actionscript. My problemy is: why when I'm setting body position to (0, 0) it's displayed in left top corner of window in short words. In any other box2d ports (processing, jbox2d, andengine box2d extension) if I set body position to (0, 0) it's displayed in the center of window. I know rules about pixel per meters etc.
The reason for the variation is that Box2D in all of its forms, uses meters as its unit of measurement. The reason for this is that it is intended a simulation of real-world objects.
Box2D does not need to have a visual representation at all. When you create a box2D simulation, it is up to the developer what level of detail to show in the graphic render of box2D. So there will be a multiplier, representing the conversion of meters to pixels. In the case of Box2D flash, the default for this ratio is 30px:1m.
So the reason every toturial has a conversion function, is that Box2D uses meters, and computer displays use pixels.
It converts meters to pixels, and pixels to meters.
In actionscript the centre of a DisplayObject is usually top-left, but Box2D bodies' centre coincides with the barycentric centre (the actual middle of the shape, not the top-left corner). Also the Box2D units are not exactly pixels, but you can easily convert between the two.
I recommend having a look at the Box2DFlash Manual, especially the part on Units and UserData. Ideally you would want to read the whole manual to get your head around Box2D better, which will give you a lot more control.
If you're not interested in the nitty gritty then give the World Construction Kit a try. It should make it easier to setup Box2D worlds.

Resources