Getting Position of a sprite in cocos2d - iphone

I am trying to get a each position of a sprite(curve),not only the center point,from starting point to end point.My sprite will rotate by 30deg when i tap on the screen.so i can't use sprite.position.x-sprite.contentSize.width/2 or some thing like that.Is there any way to get the position of a sprite or is there any other way to do this.
My need is i have different curve path.when i join those curve path,the new sprite should move along the curve path.

I think you are looking for convertToWorldSpace function of CCNode.

If I correctly understood you, you want to get sprite coord(not center, bottom left corner) rotated by 30 degrees? For x axis use formula: newX = X + cos(30) * pathLong. For y use the same but replace cos by sin and x by y of course :).

Related

Automatically calculating new position of camera after we increase our chessboard size but want it still to stay in shot

Say my camera is rotated around the X axis 60 degrees and looking down on a 9x9 block chess board. As we adjust board size, I want to zoom out the camera. Say for arguments sake the camera's position is (4,20,-7) and like this the whole board is visible and taking up the full screen.
If I adjust my board size to say 11x11 blocks I will now need to zoom out the camera. Say I want to maintain the same 60 degree angle and want the board to fill as much of the screen as it did before. What should the camera's new position be and how do you calculate it?
The X part is easy since you simple give the camera the same X position as the middle of the board. I'm not sure about how to calculate the new Y and Z positions though.
Any advice appreciated. Thanks.
edit: and if i wanted to change the angle of the camera as well as zoom out, is that possible to calculate? this is less important since i'll probably stick with the same angle, but i'm interested to know the maths behind it anyway.
Transform.Translate() method will move the transform according to the rotation. So you don't have to worry about the direction where your camera is looking at, just
yourCamera.transform.Translate(Vector3.forward * moveAmount);
will move your camera forward, which means zoom in. If you want to zoom out, just change the sign of the value to minus.
When I didn't know this, I used Mathf.Sin() and Mathf.Cos() to calculate each y and z world coordinates, which sucks.

Unity3D - Relative position on y-axis independent of rotation?

I have an object (A) with another object (B) next to it. I am trying to calculate the "height" of object B, so that i can position another object at that height relative to the position of object A. I know this sounds like gibberish (i'm a bit tired) so i have put diagrams to try and explain.
So in the left image the yellow line represents what i am trying to calculate. I have an position (orange) on the surface of a cylinder (grey) (calculated position using mesh data) which i am trying to use to calculate the radius of the object (black line). To do this i need a position at the center of the object (grey) at the same height (red dot) so i can calculate the direction from one to the other and use the length (.magnitude) as the radius.
My problem is i can't work out, how i can calculate the height (yellow line) without rotation having any effect.
I currently use projectOnPlane however if i rotate the object as seen in the second image, the radius decreases significantly when it should be consistent as the object is not changing size.
Vector3 RadDirection = (Vector3.ProjectOnPlane(orangePoint, grey.transform.up) - Vector3.ProjectOnPlane(grey.transform.position, grey.transform.up));
float radius = RadDirection.magnitude;
Any help would be much appreciated, thanks.
**UPDATE: The grey block in the diagram is a vector3 position rather than a game object. The radius calculation i am trying to do happens during runtime so i can't parent an object to the grey and review the inspector.
**UPDATE 2: Sorry, something i should have mentioned. The object i'm doing this on will not always be a perfect cylinder, it could be something such as a wine glass, where i need to calculate the radius of the glass not the stem. Another example could be a chemistry beaker which normally tapers to a point, so i would need to calculate the radius at the height of the orange point. Sorry i should have put that in the question.
Here's a diagram to illustrate what i mean in update 2. Again the orange dot is acting as a visual representation of a Vector3 position on the surface of the object's (in this case a beaker) mesh.
**Update 3: I appear to have solved the issue and so i have posted my answer below but at the time of writing i can't accept it (have to wait 2 days) and so cant close/answer the question. I would like to thank everyone that contributed and tried to help me solve this problem. I hope i can help you all someday :)
You can use transform.TransformDirection() or transform.InverseTransformDirection()
To get the height take the result of the subtraction and set the X, Z coordinate to zero:
Vector3 height = orangeBox.position - greenPoint.position;
height.x = 0;
height.z = 0;
Complete solution:
Vector3 direction = orangeBox.position - greenPoint.position;
direction = greyBox.transform.InverseTransformDirection(direction);
direction.x = 0;
direction.z = 0;
height = direction.magnitude;
redPoint.position = greenPoint.position + greyBox.transform.up * height;
I appear to have solved my problem using the following:
public static Vector3 ProjectPointOnLine(Vector3 linePoint, Vector3 lineVec, Vector3 point)
{
//get vector from point on line to point in space
Vector3 linePointToPoint = point - linePoint;
float t = Vector3.Dot(linePointToPoint, lineVec);
return linePoint + lineVec * t;
}
I found this here, it has lot's of other useful looking functions:
http://wiki.unity3d.com/index.php/3d_Math_functions
I basically pass in the origin of the beaker (green point) as the "linePoint", the upwards direction of the beaker "lineVec" with beaker.transform.up and finally i pass in the world vector3 point on the surface of the beaker mesh (orange point) it returns back a point in the middle of the beaker at the same height as my orange dot. I then just subtract the one from the other and take the magnitude as the radius. The radius value calculated is correct in that's it the value i was expecting and only changes after the sixth decimal place during rotation which gives plenty of accuracy as i only need three or four decimal places.
I'm happy to provide further details or help if anyone else needs help doing this.

How to change gravity direction on Sprite Kit?

I am using Sprite Kit in Xcode and I was wondering how to change gravity direction.As default gravity direction to "X" you can imagine on below axes graphic.What about if I would like to change to "Y".
My goal is giving to object the falling effect.Its like falling from hight point and touching the ground than getting respond with physics!
(Could be dices on board game)
//Default gravity direction is X
SKSpriteNode *myNode =[SKSpriteNode spriteNodeWithImageNamed:#"ball"];
myNode.physicsBody=[SKPhysicsBody bodyWithCircleOfRadius:self.frame.size.width/2];
[self addChild: myNode];
Thanks in advance!
You can apply a vector to the Physics World of your scene using this code
self.physicsWorld.gravity=CGVectorMake(0,-10);
In SpriteKit, X and Y are the default coordinates that you see on the screen, and the Z coordinate is the order in which the objects are positioned (the zPosition). Since SpriteKit uses a 2D game engine, you do not have a third dimension, Z, to utilize. You can change the gravity between Y and X (Top/Bottom and Left/Right of screen respectively), but not between the Z coordinate. If you want to recreate a "dice falling" effect, I would recommend you create a Sprite called Dice scaled to a large amount, and once you add it to the scene you scale it down in x amounts of seconds.
[self runAction:[SKAction scaleBy:negativeFloatHere duration:3]];
This will make the dice appear to be falling, and you might want to add some spinning animations for it if you with. If you want to use the 3D engine, go try out Metal or SceneKit

Cocos2D iPhone - screen coordinates x sprite's internal coordinates

I am new to Cocos2D on iPhone. I see that Cocos2D uses a coordinate axis where 0,0 is at the bottom left corner and X positives are to the right and Y positives up.
Now I have created a sprite and added several sprites as subsprites of this one. For my surprise the subsprites appear mirrored in Y!!! The point 10,10 will be on the upper left corner of the sprite!!!
I can simply flip the sprite in Y to make it follow the same screen coordinate, but then the content will be reversed.
Is this a bug or what?
thanks.
Without seeing any example code is a shot in the dark, but I think you need to use Anchor points.
Each sprite has an anchor point of x, y.
ccp(0.5f, 0.5f) would be the center of the sprite.
(0,0) is the bottom left....(1.0f,1.0f) is top right etc.... Over 1.0 goes outside the sprite.
Child nodes (CCSprite) will use their anchor point on the parent node coordinates.
MySprite.anchorPoint = ccp(0.5f,0.5f);

Rotate UIImageView and still get x/y click position

I have subclassed UIImageView to create a soccer ball.
The ball bounces and moves along an x & y axis, and depending on where you touch the ball, the ball will move as you'd expect a soccer ball would.
My problem is that I would like to rotate the Ball(UIImageView), but still know the x & y positions from it's original position.
I am rotating it with the following code:
ball.superview.transform = CGAffineTransformMakeRotation(M_PI+(ball.center.x*.015));
ball.transform= CGAffineTransformMakeRotation(M_PI+(ball.center.x*.015));
When I rotate it, the x & y position also rotate. Can I somehow get the x/y distance from the centre of the UIImageView? Any other ideas?
Thanks,
James.
I think if you set the anchor of your CALayer of the UIIMageView to be the center of the UIImageView youll be ok, right now its set to the upper left corner and so are expiriencing the x and y moving.
Why are you rotating the superview also? If you don't do that the center x,y will not be affected.
Why not just use ball.center as the position. This way, rotations will have no effect on the position (assuming your image is correctly centered).