Getting position after using CCScaleBy - iphone

I'm scaling my sprite in cocos2d. While using the method CCScaleBy, it's scaling the sprite but I can not get the position at which it's scaling. If i use CCMoveBy I can get the position.
Can anyone help me with it??
Thank you,
Anks

CCMoveBy alters the position of the sprite. CCScaleBy alters the scale property but not the position.
The translation (position) and scale transformation is done around the anchorPoint.
You could calculate the resulting coordinates of the sprite using the position, scale, rotation and anchorPoint.

Related

rotate a particle system in world scope unity

I created this particle (show in gif file) ,by rotate particle I want to new particles spawn in new direction and rotation, but previous stay in their initial rotate and direction, and this is my particle settings:
You should set Simulation Space to Local. World space particles aren't affected by Transform's rotation after they are emitted.
In the particle system's Renderer settings, set the render mode to Mesh. Pick the Quad, or your custom mesh, and then set its render alignment to velocity.
Set the start speed to a non-zero value, such that the particle will now emit towards your transform rotation direction. (If you want to keep the particle stationary, but follows the rotation of your transform, set the particle speed to a extremely small value, like 0.0001)
Make sure the simulation space is set to "World"
With this, the particle rotation should now align with the velocity of your particle. To make the particle faces the camera, enable "3D start rotation" and adjust the rotation angles until it matches your desired orientation.
And now, you can change your particle system transform's rotation to change the orientation of your particle. Existing particles will stay at its original rotation, while newly emitted particles will orient towards the transform rotation.

Unity2D - uniform positioning of transforms with different sizes

I have a rectangle (sprite) and I need to place different game objects (sprites) inside that rectangle but so they are all "aligned" by their bottoms.
For the life of me, I cannot make it work in Unity.
Say that my box has a height of 5.
I want to place the different size objects so they are all "resting" at the 2.5 y axis inside the box.
Does anyone know how I can do that since transform.position measures from the center of the GameObject?
Thanks!
Don't use transform.position, use RectTransform properites, as they take anchor points into account. In particular you need to set the anchor position for the sprite in the prebab / inspector and then use RectTransform.anchoredPosition to position it.

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

Physics vs graphics fighting each other (box2d)

so I have a ball (sprite subclass) that can be dragged around the screen and updates it body to tell the world its position.
The problem is, when I apply a force to the ball while the ball is being touched (thus being controlled by a finger), I have two pieces of code fighting against each other to update the sprite's visual position.
The world stepper is trying to follow through the physics and update the ball's position. The touch methods however are also trying to update the ball's body and graphic position. Ideally, when touched I would like to kill off any physics affecting the ball.
So what exactly should I be trying to do to consolidate this? This is also creating issues for collisions and filtering. Any help appreciated. Thanks
Do you want the ball to have an accurate physics simulation? If not, you could always use the SetTransform method of the b2body class like this.
CGPoint ballLocation = ;//use the touched location for this
b2Vec2 vecBallLocation = b2Vec2(ballLocation.x / 32, ballLocation.y / 32);//calculate this on your own
b2Body * ballBody = ;//replace this variable with your ball's b2Body;
ballBody->SetTransform(vecBallLocation, ballBody->GetAngle());
Do not set the ball sprite's position manually. Leave this up to the physics simulation. Your code will get really messy over time if you have to keep track of what sprites you set the position of manually and which ones use the physics simulation to update their positions. This way is also much easier.
When the physics object is being dragged, set its velocity and angular velocity to 0,0 before running the physics step simulation. You may also want to temporarily get the physics object's position, then step, then set the body's position back, then apply whatever touch dragging offset you got from moving the finger to the body.

stick a sprite to another when it collide with it

so I have a sprite that is created every second and that is moving at a random position (sprite1) and another sprite that has a fixed position (sprite2). I would like that when sprite1 collide with sprite2, sprite1 is like sticked to it (it stops moving and is sticked to it) . How can I do this please ? sorry for my english I'm french :/
p.s : sprite2 is rotating with accelerometer, so if sprite1 collide with it I would like that it rotate too :)
I think, you can try to use box2d to do this. It will help to detect collisions and to manage rotations, movement, etc.
I think, you can do it simply in Cocos2d.
1) First set the rect for sprite1 and sprite2 using CGRectMake(x,y,width,height)
2) As you told sprite1 is moving at random position and sprite2 is fixed to particular position, you can check them collide by using CGRectIntersectsRect([sprite1 bounds],[sprite2 bounds]).
3) if it intersects, set sprite1.position = sprite2.position
Note: you said sprite1 is rotating, rect can be fit only to the regular bodies. if you want exact collision or physical properties for sprite better you can go for box2d.
If you don't want to use Box2d (which can handle circle collisions), you can try something like this:
1.) Detect collision, is the distance between the two circles center point (x,y), less than the sum of the two circles radius.
2.) Make the Sprite1 stick to Sprite2, Stop the movement of Sprite1, and save the relative delta (x,y) to Sprite2, then whenever Sprite2 moves or rotates apply the same delta movement and rotation to Sprite1.