Cocos2d magnifying glass - objective-c

This has already been asked at Add a magnifier in cocos2d games
But I didn't quite understand the answer. I am using the same tutorial Let's Spot It is using but I'm not sure where to put madhu's code. I also don't know what the runAction method looks like.
Thanks

Hmm... Cocos2d CCLens3D, makes the area that is set by the programmer to popup.. Please look at the example provided by cocos2d..
the codes:
id lens = [CCLens3D actionWithPosition:ccp(size.width/2,size.height/2) radius:240 grid:ccg(15,10) duration:0.0f];
[self runAction:lens];
self is the layer where your image should be..
ccp(size.width/2, size.height/2) should be changed to ccp(yourPosition.x, yourPosition.y), means the positions which you want the popUp to be at.. Radius is the size of the circle, duration is how long you want it to be, 0.0 meaning infinite.. grid just use the same values..

Related

Objective-C Check if two objects collide

I'm doing a lot of things now when I'm learning to programing in Objective-C.
I want now to check, if two object have meet. If their positions are the same.
I have done this with this:
if(CGRectIntersectsRect(picture1.frame, picture2.frame))
But that is not the best solution, because my pictures are transparent (.png), but the picture inside is smaller and the shape of picture is not a square. So there are a lot of pixels around which I don't want to be part of this picture.
Is there a way to write like picture.frame-30 or something? Or someone have a better solution?
You can make "picture.frame - 30" object like this:
CGRect rect = picture2.frame;
CGRect innerPicture2Frame = CGRectMake(rect.origin.x+30, rect.origin.y+30, rect.size.width-60, rect.size.height-60);

iOS - Want to animate sin x graph

Very excited for my first post on SO!
I would like to create an animation like this in xcode
I am not having much luck with what CABasicAnimation has to offer.
Feels like I may need one of those fancy physics engines...(?)
How would one properly begin do this?
Thanks!
I would probably use NSBezierPath to create such an animation. I would create a path which I extended along the sine curve on each iteration, and stroke the path appropriately. If you give us more details about what you're attempting to do, we can give you more details about how to do it.
Exactly what you want.TFAnimation allows you to use customize timingFunction by set a closure to timeFunction of TFBasicAnimation.
The demo in github is exactly a Sin animation.It use a CAAnimationGroup combined linear CABasicAnimation in position x and TFBasicAnimation of Sin timeFunction in position y.

How should I design displaying a dynamic map? (Coordinates + Lines)

So I want to have a view (NSView, NSOpenGLView, something CG related?) which basically displays a map. Such as:
http://dump.tanaris4.com/map.png
Obviously that looks horrible, but I did it using an NSView, and it draws SO slow. Clearly not designed for this.
I just need to allow users to click on the individual (x,y) coordinates to make changes, and zoom into a certain area (to see it better).
Should I go the OpenGL route? And if so - any suggestions as to how to get started? (I was able to follow the guide to draw a triangle, so that's good).
I did find this post on zooming in an NSView: How to implement zoom/scale in a Cocoa AppKit-application
My concern is if I'm drawing over 6000 coordinates and the lines connecting them, this isn't efficient at all.
I don't think using OpenGL would be of any good here. The problem does not seem to be the actual painting, but rather the rendering strategy. You would need a scene graph of some kind to dynamically handle level of detail and culling.
Qt has all this packaged in a nice class class QGraphicsScene (see http://doc.qt.nokia.com/latest/qgraphicsscene.html for reference, and http://doc.qt.nokia.com/main-snapshot/demos-chip.html for an example).
Some basic concepts you should consider using:
http://en.wikipedia.org/wiki/Scene_graph
http://en.wikipedia.org/wiki/Quadtree
http://en.wikipedia.org/wiki/Level_of_detail
Try using core graphics for this, really there is so much that could be done. Watch the video Practical Drawing for iOS Developers from WWDC 2011 and it should give an over view of what can be done with CG.
I believe even CoreGraphics will suffice for what you want to achieve, and that should work under a UIView if you draw the rectangle of your view completely under the DrawRect method of your UIView (you must overload this method). Please see the UIView Class Reference. I have a mobile application that logs points on the UIMapKit, kind of like Nike+, and it certainly works well for massive amounts of points/line segments. There is no reason why this simple approach cannot work for you as well.

Translate Colors to Image?

Im not sure how else I should approach it, but if I was to (in my mac application) have a grid of NSViews, which the user can change the colour of each, is it possible to then translate this, so now I have been given a colour for each pixel by the user, make this into an exportable image?
I honestly can't think of how else to do this. I don't want to go ahead an realise I have taken a rather foolish path.
The idea is I will have a grid of squares which the user can paint, a colour in each square, a square representing a pixel in the final image. So they paint with like a paint bucket filling each one, then export it into an actual image file.
Any help much appreciated, thanks.
A grid of NSViews sounds really heavy for what you're doing. Why not write one single custom view that checks the mouse position and modifies the data appropriately? Then you'd write a custom drawing method to fill the custom view, and you could use the same exact draw method to write to an NSImage which you could export.
You'll need to do a bit o' math. For each "pixel", call -set on the appropriate NSColor, then use NSBezierPath's -fillRect method. It may help you to get out a pencil & paper to figure out the math for the rect origins & sizes.
Check http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/CocoaDrawingGuide/Introduction/Introduction.html for help if you've never done custom drawing before. It's really not that bad, just takes a little reading. :)

cocoa collision detection question

I get the concept of the NSIntersectionRect for collision detection but I can't seem to think of how to implement it for my project. It's nothing fancy, click a button and a view subclass is called and places a circle in the window at a random location. Click within the that view and the circle pulses (this makes it the active view). If you have an active view, clicking anywhere outside it (but not on another circle) will move that view to the click point.
I'm using [activeView animator setFrame: NSMakeRect(x, y, w, h)] to move the active view. Can I use this for collision detection or do I have to go with CABasicAnimation? Basically what I am looking to do is detect collisions with other circles (no physics needed at this point, just stop the movement) and/or with the bounds of the app window.
If someone could nudge me in the right direction (tutorial link, code snippet) I'd appreciate it.
Edit: Based on the well detailed answer below I need to be a bit more clear. I'm confused on where to implement the collision detection. The animator method of a view class is one line of code. How would I iterate through every static circle on the screen to run a collision check? Which is why I am wondering first, if I need to go with CoreAnimation, OpenGL or something like Chipmunk and then if I could get a nudge or assist that would be great.
Later .. in answer to your recent questions:
Which is why I am wondering first, if I need to go with CoreAnimation, OpenGL or something like Chipmunk and then if I could get a nudge or assist that would be great.
Answer - you absolutely definitely do not need OpenGL :) Next, you definitely do not need a physics library like Box2D or Chipmunk ... you could go that way if you wanted to, but, it would be a huge amount of unnecessary work. To be clear: until you are totally familiar with using DrawRect, things like Chipmunk are useless for you anyway, so just forget that.
Core Animation will not really help you. To be clear, you possibly want to interrupt an animation as it is happening. Is that correct?
I'm confused on where to implement the collision detection. The animator method of a view class is one line of code. How would I iterate through every static circle on the screen to run a collision check?
Bad news... if you actually want to interrupt the animation, if there is a collision, forget about Core Animation. Core Animation will let you send it from A to B as "one unit" of animation. It will NOT let you stop it in the middle (in case of a collision). So, that's that.
To be clear, that is what you want to do right? You set the circle in motion, and IF it hits something along the way, you want it to stop. Is this correct? if so, completely forget about Core Animation and throw away any work you've done so far.
You are going to have to dive in to "real" programming (whatever that means) and start using drawRect. Are you up for it?!
At this point I might just mention: consider buying a copy of Corona (it's like $100 -- I'm sure the demo is free). You can do everything you are describing in, literally, five minutes using Corona. (1/10th the time taken to write this post??) I always recommend this to people who are iPhone Curious. If you don't really want to spend 6 to 18 months becoming a gun iPhone programmer - just click to Corona for iPhone and in a fraction of the time it's taken you to use Stack Overflow, you can have your circles bouncing wildly on the iPhone screen.
So, failing that, you're gonna have to get to work and learn how to launch a timer (NSTimer) and use drawRect in a UIView.
Craate a new class called MyFirstView (.h and .m file) that is a subclass of UIView (not UIViewController, which is for wimps!). You'll need a drawRect method in MyFirstView and proceed from there!
Original answer..
I'm not sure I understand what you are saying, but to be clear:
You want to detect a collision between two circles. In your case, all the circles are the same diameter. Is that correct?
If so, fortunately it is very easy to do.
Write a routine that gets the distance between two CGPoints. (If you don't know how to do that I included it below.)
Next step, you must know the DIAMETER of your circles. Let's say it is 50.0 for the example.
Next, here is a routine that checks if two circles are colliding:
static inline bool areTwoCirclesColliding( CGPoint aa, CGPoint bb )
{
return ( distanceBetweenTwoCGPoints(aa,bb) < 50.0 );
}
(Note... if you are new to Objective C, note that the above is totally valid code. Simply paste it in at the top of your file. OK?)
Finally, you just have to check all your circles, one against the other, to see if any are colliding.
If you have a simple fixed number of circles, let's say three or so, just write out all the lines of code to check if any of the combinations are colliding, hence:
areTwoCirclesColliding(a,b)
areTwoCirclesColliding(a,c)
areTwoCirclesColliding(b,c)
If you have an array or some sort of list of circles, you just have to go through them all and check that each one is not touching any other. In pseudocode it might be something like this...
for n in 1 to numberCircles
oneCircle = circles[n]
for m in n+1 to numberCircles
anotherCircle = circles[m]
if ( areTwoCirclesColliding(oneCircle,anotherCircle) )
.. break, you found a collision
Alternately you could write it like this, makes no difference..
for each aa in circles
for each bb in circles
if (aa != bb) if (areTwoCirclesColliding(aa,bb)) .. break, found collision
{Aside - For the record it is utterly unimportant that you check each pair twice in that pseudocode, no problem.}
It is impossible for me to write the actual code for you as i have no idea what structure you are using, sorry.
Of course if your circle is an object, you could sensibly make it test itself against all the other circles, same idea. If you have an SQL database of circles, test them all against each other.
So fortunately you can see it is one (1) line of code to check if two circles are colliding. And it's about 3 or 4 lines of code to check all your circles for collisions. So fortunately about 5 lines in total!
So, that is an incredibly simple tutorial on video game physics, part 1.1.1.1 !!!! Hope it helps! If that is not what you were trying to achieve, it was a complete waste of typing! :)
For the record here's a routine to get the distance between two CGPoints:
static inline float rawDistance(float x, float y, float p, float q)
{
return sqrt( ((x-p)*(x-p)) + ((y-q)*(y-q)) );
}
static inline float distanceBetweenTwoCGPoints( CGPoint a, CGPoint b )
{
return rawDistance( a.x, a.y, b.x, b.y );
}
(Note... if you are new to Objective C, note that the above is totally valid code. Simply paste it in at the top of your file. OK? It's exactly like using any everyday function supplied by Apple such as x=CGLayerGetContext(), for example. Enjoy!)
Later .. and by popular demand, for an object, Circle...
-(bool)isTouchingOtherCircle:(circle)c
{
return areTwoCirclesColliding(self.center, c.center);
}
-(bool)isTouchingAnyOtherCircle
{
for oc in yourCircles
if (oc != self)
if ( [self isTouchingOtherCircle:oc] )
return false;
return true;
}