Collision of two sprites - cocos2d-iphone

I'm detecting collisions in my game using sprite.boundingBox. It worked fine for me until I introduced a (car wheel) as another sprite that collides with other sprites as well. Clearly the wheel's boundingBox is not a rect and that's why it cannot detect intersections with other rects (collisions). Any help on how to detect the collision between the wheel and the other sprites?

A boundingBox as the name implies is always a rect which binds your sprite on all four sides.
If rectangle collision detection is all that you require for your game, then you can use the method that you have been using so far and it should work.

Related

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.

Move the box2d body to center of screen

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.

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.

What collision detection method to use with hand drawn surface?

I have a lunar lander type game. I don't use any physics engines.
My lander keeps falling if you do not use thruster and eventually lands on the ground. Ground is hand drawn, it is not a line, more like curve, and the land can be of any configuration or color.
How do I properly use collision detection and its results?
Well it depends on what you want to do. I would recommend one of the following:
Use physics engines. They are there for something. You could create different shapes what was drawn. You could mix in a rectangle if theres a straight line, or a lot of circles for curves, etc.
Use your own custom circle collision detector. You represent the lander with a bounding box sized circle. Then, for each of the handdrawn lines, create a bunch of adjacent circles representing the line. When you check your lander position, you are just basically looping through the circles representing the lines and checking for collisions. Incoming pseudocode
for (CollisionCircle* circle in collisions)
{
if (circle.collidesWith(lander.collisionCircle))
{
// 1. Calculate edge distance from lander to circle (position + radius distance)
// 2. Remove distance from lander position to fix position.
}
}

CCRenderTexture size and position in Cocos2d-iphone

I was trying to use CCRenderTexture for pixel perfect collision detection, as outlined in this forum posting:
http://www.cocos2d-iphone.org/forum/topic/18522/page/2
The code "as is" works, and I have integrated it with my project
But I am having trouble doing some of the other things discussed:
If I create the renderTexture to be any size less than the screen size, the collision detection doesn't work properly - It seems to show collisions when the sprites are close (<15px) to each other but not actually colliding.
Also I have trouble changing the location of the render texture. Regardless of the position I specify, it seems to go from bottom left (0,0) till the width & height specified. I followed this post:
http://www.cocos2d-iphone.org/forum/topic/18796
But it doesn't solve my problem. I still get bad collisions like specified above. Also, the first post I mentioned on the list contains comments by many users who have resized their textures to 10x10, and repositioned them off screen.
Does anyone have any sample code, so I can see what I am doing wrong? I just use the boilerplate code:
CCRenderTexture* _rt = [CCRenderTexture renderTextureWithWidth:winSize.width height:winSize.height];
_rt.position = CGPointMake(winSize.width*0.5f, winSize.height*0.5f);
[[RIGameScene sharedGameScene]addChild:_rt];
_rt.visible = YES;
I use cocos2d-iphone 1.0.1
You need to move the sprites you intend to draw into the region of the renderTexture before calling draw or visit. Moving the renderTexture does not change the position of _rt.sprite.
The intersection rectangle must be in the region of the renderTexture, otherwise you get inaccurate collisions.
It seems that you cannot change the position of _rt.sprite.
The solution that I use is to determine the origin (x,y) of the intersection box, and offset both the colliding sprites by that much. This will ensure that the intersection rectangle will have its origin at 0,0. Then I calculate the intersection rectangle again (after ensuring the origin of the intersection rect is 0,0) . Then I follow the instructions in the forum posting.
When determining the dimensions of the render texture, I ensure that they are at least as large as the intersection rectangle, and I ensure that the intersection rectangle is fully inside the render texture. This way there are accurate collisions. If even part of the intersection box is outside the render texture i get inaccurate collisions, so before drawing into the render texture, make sure you move the sprites you intend to visit so that the intersection box is entirely within the render texture.
Remember to move the sprites back after you're done. :)