Move the box2d body to center of screen - cocos2d-iphone

I am having an circle in the center of the world. I add some balls to the world in the form of b2Body. Now i want to move or throw the ball to the center of the screen. The effect should be like the balls are colliding with the circle.
The ball are positions randomly, so they can be at any were on the screen and the need to travel to center of the screen to the circle
Can any one tell me how to do this because i have no idea to move the b2Body object.
I want blue circle to attract red circles. Or in other words i want red circles to move to blue circle.

Finally got the solution of my problem. I use the concept of radial gravity.
http://www.vellios.com/2010/06/06/box2d-and-radial-gravity-code/

In this case, I am not sure about this way but you can try this way. I guess you are adding b2body by touching on the screen. Now i don't know what is the type of your B2body.
There are two ways to move the b2body.
check this link:http://www.cocos2d-iphone.org/forum/topic/21620
From this link,
I am guessing, in your game ,body is b2static body,What you can do is Move your sprite to center of the screen, with respect to that change the position of the corresponding body in tick method.
And you need to stop moving the sprite when it hits the center ball so stop moving corresponding sprite when it hits the center ball.
This may be the possible way if i understood your question.

Related

QPainter - draw objects along a circle

I would like to draw those black dots around the whole circle with regular spacing in between each other (rough image below). Those black dots should sit directly on the circle. Is there an easy way to do it somehow with painter.drawArc() function or something like that?
No, there isn't. I am afraid you will have to use... math!
Actually, you could get away without that. For sweeping around a circle in particular there is an easy way to do it by transforming the painter. You initially transform the painter to the center of the circle, and draw the first dot at the circle's 12 o'clock position. That means drawing it into negative y space. Then you simply rotate the painter by 360 / numOfObjects degree and draw the same circle again.
They key thing here is to transform the painter to the center and draw the object offset, because otherwise it will be more complicated to calculate the position and angle yourself. The angle doesn't play a role here, since you are drawing dots, but it will make a huge difference if you draw something other than dots. This way you can easily sweep the circle by rotating the painter around its origin.
To put it in pseudo code:
draw big circle
move painter to big circle center
degree = 360 / numOfObjects
while numOfObjects--
draw dot at 12 o'clock
rotate painter by degree

How to rotate a rect in SDL2?

I plan on making a game, and I want to create some background animations for said game. One of these animations is a rotating rectangle. I've looked all over, and I cannot find any form of math or logic that allows me to rotate a rectangle (SDL_Rect to be specific, but you might have already known that).
I can't figure out the math for myself, I really don't have any working code for this, so I can't show anything.
Essentially I'm looking for some type of logic that I can apply the rectangle's coordinates so that whenever the main game loop loops, it will rotate the rectangle some amount of degrees.
You can't rotate an SDL_Rect. If you look at its definition, it's made of coordinates for the top-left corner, the width and the height. There's no way to represent a rectangle with sides that aren't parallel to the coordinate system's axes.
SDL_RenderCopyEx supports drawing rotated textures, though.

Attaching a Rotated Rectangular body to a Sprite

In a game I am programming in Cocos2D and Box2D, I have a sprite that is in the shape of a hill, and I am trying to attach a body to it so it can respond to physics. My idea is to use a rotated rectangle shape so that it aligns with the slope, but I need to be able to move the sprite so that the body stays appropriately lined up. I can make the body and sprite work together in the case of a circle, but I can't figure out how to make it work for the case of the rotated rectangle. Could someone explain the code to do this?
Yes I agree to user1634707. CocosBuilder will be one potential choice.

Angle reflect in Cocos2d?

I am making a game in Cocos2d. I have a ball that will be shot at a flat surface (the top of the screen) how can I make it so the ball will travel, hit the surface, then reflect the angle and travel that direction? Does that make sense? Please tell me if it doesn't, and I will clarify. Thanks!
EDIT:
Here's an illustration of what I want
Here
You could build the game using box2d (in cocos2d). Then you will have that "effect" for free.
Once you launch a ball at an angle, say 50 degrees, add (cos(50)*speed) to his X position, and (sin(50)*speed) to his Y position.
When you detect the ball's y position has reached the surface's y position, just change the angle to -50.
But you must know that it only works if you want a reflection angle on a top surface, where it hits the top surface and bounces down.

Rotating a cocos2d sprite on an anchor point outside of the sprite

Currently the setanchor function only sets the anchor within the sprite frame. Is there any (easier) wway to change the anchor point to a coordinate on screen space? My purpose is to allow the sprite to rotate around a circle.
TIA for any suggestions made.
Use the Node hierarchy to your advantage. Create a new Node (which won't be visible unless you want it to be) and add your sprite to the node as children. Position the sprite child somewhat away by giving it a position of 100, 100 for example.
Now if you rotate the node using the rotation property, instead of the sprite, the sprite should rotate with the node, making it appear as if it goes around in a circle. The node itself will be the centerpoint of the rotation.
I've added this Q&A to my cocos2d FAQ:
http://www.learn-cocos2d.com/knowledge-base/cocos2d-iphone-faq/learn-cocos2d-public-content/manual/cocos2d-general/14826-how-to-rotate-a-sprite-in-a-circular-motion