How to implement a 2D fair clamp in a game/physics engine? - game-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.

Related

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.

Sprite Kit - Adding two physics bodies to one SKNode

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.

Detect multiple bodies in Kinect?

I am working with kinect in openframework using the ofxKinect addon, which is great and plenty fun!
Anyway I am looking for some pointers or a direction when dealing with multiple bodies on the screen. I was thinking of making a rect around each detected body and when the rects intersect something could happen, an effect or anything.
So what I am looking for are ideas or something that could point me to the right direction of detecting multiple bodies when using a kinect.
Right now based on the depth image I get from the kinect I go through each pixel and create a bunch of smaller rectangles with a padding and group them in a larger rectangle bound if they are separate from another rectangle group. This is not ideal as it only deals with the pixel values and is not really seperating bodies from eachother and is not giving me the results I am looking for.
So any ideas would be greatly appreciated!
If you want to use ofxKinect a quick solution would be to threshold on depth and assume bodies and no other objects will be within a depth range. This should make it easy to use the OpenCV's contour finder to isolate the outlines of the bodies and get the bounding rectangles. If the rectangles intersect(and ofRectangle already does the math you), trigger the reaction you need. Also don't forget to do that once if the effect isn't showing already, otherwise you will trigger the effect multiple times per second while the two bodies' bounding rectangles intersect.
You could try something a bit more hardcore and using ofxCv(not just ofxOpenCV) to tap into the HoG functionality. This is slow in itself and not ideal with the depth map, but hopefully you can run in every few seconds just to detect a person and the depth, then keep tracking that movement.
Personally, if you want to track people with the Kinect I recommend using ofxOpenNI as if already provides the scene segmentation feature and even if you don't track the skeletons you can still get useful information like the pixels pertaining to each body and they're centre of mass. I'm guessing Microsoft KinectSDK has a similar feature and there should be an oF addon, but it's windows only.
ofxKinect/libfreenect does not offer any people detection features, so you will need to roll your own.

top down game - checking, drawing enemy's line of sight area with obstacles

Examples of what i'm going to need:
I'm using cocos2d to draw a CCTMXTiledMap, on those tiles i'll have to draw the LOS triangle.
How would i test if the player is within that triangle, taking obstacles into account?
How would i draw the line of sight area like in the examples above?
BTW, i wasn't sure if this should have been posted here or on gamedev, don't be mad.
You may wish to look at point-in-polygon algorithms such as the ray casting algorithm described here.
You can break up the triangle to account for obstacles, or just make a more complex polygon. You should be able to find an implementation to suit your needs online.
You may also want to take a look at this article for some inspiration. You can maintain a tree like structure, a root triangle (or fulcrum) that can be used to determine whether a point is in general line of sight, with the children (triangles) taking obstacles into account. That way you can quickly eliminate more complex checks.
In the image below the dark blue dots are quickly eliminated from further checking as they do not fall within the root viewing triangle.

Objective-c -> Isometric grid

Sorry, but i do have a question which is a little different from the norm. I want to make a game display which appears to be isometric, so every cell is:
http://upload.wikimedia.org/wikipedia/commons/4/42/Rhombus_01.png
in shape. I am a bit unsure how to do this effectively, though I am currently using a 2D array
(M1[5][8] = {{0, 2, 1, 3...},{...}}
to relay which images to position where. My main issue is making sure that the tiles end up where they are supposed to. Currently i have them 45px high by 60px wide. I would like to ask if anyone could give me a hand setting things up so that the level screen appears somewaht isometric. Thank you for you time :)
Cocos2d can support isometric maps. To get started with Cocos2d, there is nothing better than Ray Wenderlich's multiple part tutorial! It doesn't cover isometric projections, but you'll still need to know the basics of cocos2d.
Note that Cocos2d is open source and, thus, you could study the source to see how it implements isometric projections if you decide to roll your own solution.