Sprite Kit - Adding two physics bodies to one SKNode - ios7

Is it possible to add two (ore more) SKPhysicsBodys to one SKNode? Something similar to this:
Example from PhysicsEditor
Because the head of the character should collide with a ball, the top should be round. Furthermore the ball mustn't go through the player. Do you have an idea how to accomplish this?

As the physicsBody property on SKNode suggests, there's a one-to-one relationship between nodes and physics bodies.
However, that doesn't mean you have to have one basic shape for every visible sprite. There are a few approaches you can take to accomplish what you're looking for:
Does the top really need to be rounded? You can cover most of the monkey art with a rectangle. (I presume you want a rounded top so collisions bounce in different directions, though.)
Create the "round-ended-rectangle" shape you're after using a polygon. You'll have to pick a number of sides for approximating the curve that fits your app: too many and it'll slow down the physics simulation, too few and it won't behave like a circle when other bodies bounce off.
Every body needs a node, but not every node needs a visible sprite. You can make your monkey out of two nodes: one that holds the art and has the square or round physics body attached, and another node that has the other physics body, attached through a fixed joint,
but with no art.

This is the top hit on Google for this question so i thought I'd update the answers. This is possible (Although may not have been in 2013), as per the answer from Mike S in this post.
You need to use
// Swift
SKPhysicsBody(bodies: <#[AnyObject]#>)
// Obj-c
[SKPhysicsBody bodyWithBodies:(NSArray *)bodies]
So you create a body with other bodies and add that one body to your SKnode.

No, every SKNode can only have one SKPhysicsBody since physicsBody is a single property of SKNode. Like rickster said, your first option is to make the monkey one silo-shaped physics body using the bodyWithEdgeLoopFromRect: class method of SKPhysicsBody.
Your other option is to create two separate SKNodes and attach them with an SKPhysicsJointFixed. rickster's answer is great, but I just wanted to give the class names in case you want to do a little Googling around.

Related

How to implement a 2D fair clamp in a game/physics engine?

I've recently played Nintendo Badge Arcade and I wondered how is the clamp implemented.
The expected behavior, for those who don't know the game can be seen in this video:
http://ytcropper.com/cropped/2-59b6b68872036
I know the items the player can obtain follow the basic physics of any game engine (they have their gravity, their weight, and its shape is related to its sprite) but I don't know how to implement the clamp.
My first question is: What is the ideal game framework to achieve this. Game maker, box 2d...
And my second and most important question is: How is the clamp implemented. Is it a composite object that rotates both bodies until they touch something and then friction comes in?
Thank you.
One way to go is use Box2D and its Revolute joint to simulate a clamp. Basically clamp is 3 dynamic bodies: hull, and two side bodies that are marked as "ghost" for the hull (so that they do not collide). Joints connecting those bodies to hull must be configured to rotate in the right direction and only when a player performs an action. When a prize is hit by clamp you keep them joints working for body not to fall down thanks to mentioned friction.
More detailed information can be found in box2d tutorials, I hope the idea is clear.

GameMaker Studio Physics Object Precise Collisions

Is it possible to have a physics object in GameMaker Studio use precise collisions?
Here's some context for my question. I'm making a pirate game where the player sails around a large ocean with a number of islands. I've been using the physics engine to control the movement of the ship, and that is working well. However, the problem arises when trying to introduce collisions between the ship and the various islands. As far as I can tell, the underlying physics fixtures can only be formed into fairly simple shapes. Specifically, the collision shape editor is limited to 12 points, and only convex shapes. This is a problem, because many of my islands are relatively complicated non-convex shapes, and aren't necessarily a single piece. It would be nice to be able to use the island sprite as a precise collision mask, as would be possible for non physics based objects.
Is there a way to do this, or a possible work-around that I'm missing? Here's an example of one of my islands:
I can see two solutions to your problem.
1 - The easiest, but performance-unfriendly.
In the sprite editor, click "Modify mask". There should be a "precise collision checking" box you can tick. This means that your sprite will be checked pixel by pixel for collisions. As you can guess, this is not performance friendly, but will do exactly what you want.
2 - The one I would recommend.
What you could do is just draw the island sprites, either through the background or via a dedicated object, and then create some simple shape objects (rectangle, circle and diamond), that would be invisible, and place them over your islands in the room editor. (Don't forget that you can stretch them).
These simple shaped objects would be the ones to check for collisions.
I used this technique make a hitbox for complex-shaped clouds in one of my games, so I know it works.
I believe that the island you show us can be fairly well covered with a few ovals and a long rectangle.
Bonus : after doing that graphically, you can copy the creation code of the shapes from the room create event to the island create event to repeat for multiple identical islands. Just don't forget the position/angle offset !
By using the Shape options when defining the collision shape, you can have any kind of Convex polygon as your collision shape. Example:
The spot where you choose the Shape option is in red.
After you select that option, you can just click & drag to add/edit a vertex to the polygon. Just bear in mind, it has to be a convex polygon, GameMaker is very strict about that. You can also remove vertices by right clicking on them.

SKPhysicsJoint: Contacts and collisions not working

On IOS7.1, using SpriteKit, I've created two simple rectangular sprites with corresponding physics bodies. I set up contact and collision bit masks, and all works exactly as I expect: contacts are detected and collisions prevent the two rectangles from overlapping. But when I create an SKPhysicsJointSpring object and join the two rectangles, no contacts are ever detected even though the rectangles sometimes overlap during simulation. (The spring behavior works just as one would expect, though, with visible oscillations.)
In other words, joining physics bodies seems to prevent contact detection and collisions in SprikeKit, which is not the behavior I desire.
Anyone know of any way to fix this? Thanks.
There is no fix, this is built-in behavior of the Box2D physics engine Sprite Kit uses. When you connect two bodies through a joint, they no longer contact and do not collide with each other.
With Box2D you can change this behavior via the collideConnected flag. However no such flag exists in Sprite Kit's physics classes.

How to code a random movement in limited area

I have a limited area (screen) populated with a few moving objects (3-20 of them, so it's not like 10.000 :). Those objects should be moving with a constant speed and into random direction. But, there are a few limitation to it:
objects shouldn't exit the area - so if it's close to the edge, it should move away from it
objects shouldn't bump onto each other - so when one is close to another one it should move away (but not get too close to different one).
On the image below I have marked the allowed moves in this situation - for example object D shouldn't move straight up, as it would bring it to the "wall".
What I would like to have is a way to move them (one by one). Is there any simple way to achieve it, without too much calculations?
The density of objects in the area would be rather low.
There are a number of ways you might programmatically enforce your desired behavior, given that you have such a small number of objects. However, I'm going to suggest something slightly different.
What if you ran the whole thing as a physics simulation? For instance, you could set up a Box2D world with no gravity, no friction, and perfectly elastic collisions. You could model your enclosed region and populate it with objects that are proportionally larger than their on-screen counterparts so that the on-screen versions never get too close to each other (because the underlying objects in the physics simulation will collide and change direction before that can happen), and assign each object a random initial position and velocity.
Then all you have to do is step the physics simulation, and map its current state into your UI. All the tricky stuff is handled for you, and the result will probably be more believable/realistic than what you would get by trying to come up with your own movement algorithm (or if you wanted it to appear more random and less believable, you could also just periodically apply a random impulse to a random object to keep things changing unpredictably).
You can use the hitTest: method of UIView
UIView* touchedView=[self.superview hitTest:currentOrigin withEvent:nil];
In This method you have to pass the current origin of the ball and in second argument you can pass nil.
that method will return the view with which the ball is hited.
If there is any hit view you just change the direction of the ball.
for border you can set the condition for the frame of the ball if the ball go out of the boundary just change the direction of the ball.

How to check the color of a certain part of an image?

I am making a simple game that uses "AI players" (they aren't really AI players). I need to find out if a certain part of the "map" I am using has certain colors, so I can make the "AI players" do certain things. Is it possible to do this?
I don't know if this will help, but a game called "Warcraft 3" uses a very similar thing to determine certain things, such as movement. If you know of this game, it should be a lot easier to understand this question.
I think this may be possible if I put the image into a custom NSView subclass, but I have not yet learned how to check colors there either.
The best way to do this would be not to bother checking the colors of the actual image (which can be an expensive operation if you're checking a lot of individual pixels), but to indicate in your map's data structure the characteristics you want to have, and then take both the color and player behavior from that.
In pseudocode:
// Draw Map
foreach currSquare in listOfSquares:
if map[currSquare].hasPropertyX():
drawSquare(currSquare, blue)
else if map[currSquare].hasPropertyY():
drawSquare(currSquare, red)
// Move pieces
foreach currPlayer in listOfPlayers:
squareIAmStandingOn = currPlayer.square
if map[squareIAmStandingOn].hasPropertyX():
currPlayer.takeActionX()
else if map[squareIAmStandingOn].hasPropertyY():
currPlayer.takeActionY()
Create a NSBitmapImageRep from the NSImage and use colorAtX:y: to get the color.
Check:
NSBitmapImageRep Class Reference