Getting absolute position of CCSprite in cocos2d - objective-c

In my game, I have a CCSprite that orbits another CCSprite, much like an electron orbiting a nucleus. I have the electron as a child of the nucleus, to make animation much simpler. All I have to do is rotate the nucleus and the electron follows suite quite nicely.
However, my problem comes from wanting to have the orbit animation appear a little snazzier, by either adding something like a particle system trail, or a ribbon effect following the path of the electron. I cannot simply add a particle system to the electron itself, because the particles don't follow correctly, since they are being rotated by the nucleus as well. If I add the particle system to self, then they appear correctly, but not in the same position as the object they are supposed to be trailing.
My question is this:
Is there a way to get the scene position of an object, say the electron, as opposed to only having access to it's position relative to it's parent?
Thanks.

Yes there is!
Each CCNode and its descendants has the ability to get a position relative to the scene:
CGPoint worldCoord = [mySprite convertToWorldSpace: mySprite.position];
This worldCoordinate will be relative to the scene as opposed to the parent node!
Hope this helped! ^_^

A later edit:
When you do:
[aSprite convertToWorldSpace:position];
You are actually getting the global coordinates of position in aSprite's coordinate system. If you want to translate aSprite's position to global space you need to ask for it's parent to do the translation for you, because the sprite.position is already in it's parents coordinates system.
Hope it explains it
Original Answer:
For me this solution did not work
I had a 2 level hierarchy where mySprite is a child of a CCSprite that is a child of the scene.
Bottom line: This code fixed the problem for me:
CGPoint worldCoord = [[mySprite parent]convertToWorldSpace: mySprite.position];
This is the hierarchy structure that required my solution:
myScene -> mySpriteParent -> mySprite
mySprite.position:29,254
mySpriteParent.position:533,57
Solution1 - wrong result:
[mySprite convertToWorldSpace: mySprite.position]:91,418
Solution2 - right result:
[[boxSprite parent] convertToWorldSpace:boxSprite.position]:253.5,275.5
Perhaps this solution will help someone
And maybe someone will explain why this solution works and not the other

Related

Changing the hitbox of an image

I've been looking for a solution for quite a while now, but I can't help finding one. The situation is as following: I created an SKSpriteNode with an image, by the method touchesBegan I want to interact with it, like if one touches the image, something should change. The problem is that the hitbox around this image is square-shaped and not adjusted to the shape of the image. Has anyone got any clue how I can solve this problem? I tried doing it by changing the physicsBody like this
CGFloat offsetX = node.frame.size.width * node.anchorPoint.x;
CGFloat offsetY = node.frame.size.height * node.anchorPoint.y;
CGMutablePathRef path = CGPathCreateMutable();
CGPathCloseSubpath(path);
node.physicsBody = [SKPhysicsBody bodyWithPolygonFromPath:path];
Seems like the touchesBegan method doesn't use the physicsBody shape but the actual shape, I tried my best explaining it, sorry if I confused you.
Thanks in advance!
Julian
In the Xcode level editor, under the physics definition of your SKSpriteNode, change the body type to alpha mask (see image). This makes the physics boundary conform to the actual shape of your sprite rather than the rectangular bounding box.
Sadly, this solution only applies if your sprites are managed in an .sks file since level editor seems to have logic that calculates your sprite's outline and turns it into a path used to initialize the physics body with a body type of 'alpha mask'. (Too bad there's currently no 'bodyType' property on SKPhysicsBody because it would be really convenient to be able to set this programmatically instead of only being able to do so in the .sks file... Apple?)
The way you're attempting to create and initialize an SKPhysicsBody with a CGpath is on the right track I think... But since it's currently an empty path (having no points or lines) I'm guessing it won't have any effect.
Is there any way you could approximate some points & lines to represent a rough outline path for your sprite? Then, you could use those points to create a CGPath and then use that to initialize an SKPhysicsBody.
If not, a short-term compromise could be to initialize your SKPhysicsBody using init(circleOfRadius:) and use a circle as the body type which could seem less clunky than a box. And you could maybe play around with the circle radius to get a feel for the most natural radius for your collisions.

Tearing graphics in SCNView

Hi, I have a SCNView with some nodes, when rotating I get some strange tearing, the nodes on top have a higher rendering order, changing this seems to have no effect.
Is there anything I can do to get rid of the white lines?
It's like it's fighting for position??
as said above, it looks like you're experiencing z-fighting because your colored objects and white object lie in a same plane.
You can avoir this
By slightly offsetting your geometries, but this trick does not work in every situation (the user might notice the gap depending on the point of view)
by changing the renderingOrder of your nodes but don't forget to tweak the writesToDepthBuffer and readsFromDepthBuffer properties of your materials
When use NO.2 solution mnuages introduced:
node.renderingOrder = 100;//Max value to ensure your node render at latest.
//disable deep buffer for rendering
node.firstMaterial.writesToDepthBuffer = NO;
node.firstMaterial.readsFromDepthBuffer = NO;
This is only work for node's geometry locate top hierarchy otherwise will lead to a weird perspective scenario.

touched Image and recovering the touched point's coordinate

I'm working on an iPad application and that's my problem:
I elaborated an algorithm to know if a point is inside a polygon, in an image. So I need when touching the Image, to know the coordinates of the touched point and then do an action using those coordinates (an NSLog to make the example easy), the problem is that I can't use an IBAction on an UIImageView, and so can't recover the point's coordinates. Thanks for any help
I think at first you have to make polygon which fit to your image. And then you can use touchesBegan:withEvent: to get the coordinate of touch point and judge whether the point is inside of polygon or not.
Here is similar question like yours.
How to get particular touch Area?
I think this is a little difficult work, so maybe you would better use cocos2d library which have collision judgement function.
http://box2d.org/forum/viewtopic.php?f=9&t=7487
But also I think iOS is well constructed for handling touch, so this is beneficial effort for you.

Adding the openGL Particle Emitter by Michael Daley inside GLKView

I try to use the great particle emitter which Michael Daley build for his Particle Designer to be working inside a GLKView. I see two ways to get there:
hack the code from his great (but too old) tutorial to work with iOS5.1 inside a GLKView
pay and use his Particle Designer to add the particle effect to a glkview
I tried 1. the whole day but ended alone and lost in the wide lands of openGL. I don't know how or what is important to initialize from the GL-stuff.
I ported the classes Image, Texture2D, Common and ParticleEmitter and instantiated them inside a GLKViewController, ending with an EXC_BAD_ACCESS in line 341 of ParticleEmitter.m:
// Now that all of the VBOs have been used to configure the vertices, pointer size and color
// use glDrawArrays to draw the points
glDrawArrays(GL_POINTS, 0, particleIndex);
and I don't know why or what....
Now I think about buying the Particle Designer and try to implement it inside the GLKViewController and its GLKView.
Is there any body who could help me with 1./2. to solve my problem, adding an openGL particle emitter to a view based application?
thanx!
edit: removed some stupid code
Looks like this one could help a lot...
;-)
I also created a new question here.

How can I create "glass" effect on my own UIViews?

I'm working on an iPhone app that has some non-rectangular UI elements. Currently, I'm subclassing UIView, and in drawRect I'm using a CGPathRef to draw black border and a color-filled interior.
I'd like to make these items look more like "buttons", though, so I'd like to have some of the same sort of "glass effects" that are used on e.g. the icons for an iPhone app (when you don't set UIPrerenderedIcon to true), or in other buttons.
I hunted around, and found this, which seems to be close to what I need:
Gradients on UIView and UILabels On iPhone
But I'm having difficulty figuring out how to clip the gradient to my shape.
It seems like the mask property on the view would be the right place to go, which seems like it would call for me to create a new CALayer object, with the clipping somehow applied to it.
I'm hoping there's some nice convenience function for doing this, though if I need to write something more complicated, that's OK, too. I'm just having difficulty figuring out how to apply the path as a mask. I'm unsure if I need to create a new drawing context and draw the path into it? And then use CGContextClip?
I think I've got a lot of the right pieces figured out, I'm just having difficulty understanding how to assemble them.
Could someone please point me in the right direction? (I'm happy to read more in the docs, just point me in the right direction, please.)
You can create a CAShapeLayer and set its path to your CGPathRef. Then set the mask property of a CAGradientLayer to your shapeLayer.