Ignoring Collisions Between Bodies in Box2d (iphone) - iphone

I have 3 types of bodies. they are
1) MouseJointBody (It is moving with mouseJoint only)
2) 2 MovingBodys (It is moving continuously in the world)
3) StaticBody (It is fixed in constant position)
I need collision between these bodies:
1) MouseJointBody with MovingBodys and StaticBody and world bounderies
2) StaticBody only with MouseJointBody
3) MovingBodys only with MouseJointBody and world bounderies
4) MovingBodys collide with each other
but don't need collide with StaticBody and MovingBody. I set filter data for these bodies like below code:
StaticBody:
fixDef.filter.categoryBits=0x0004;
fixDef.filter.maskBits= 0x0002;
MovingBodys:
fixDef.filter.categoryBits=0x0004;
fixDef.filter.maskBits= 0x0002;
MouseJointBody:
fixDef.filter.categoryBits=0x0002;
fixDef.filter.maskBits= 0x0004;
Everything working fine but my MovingBodys are not collide with eachother and MouseJointBody and MovingBody not collide with the world box. these two are going out of the world. please tell me the solution for this one.

In Farseer that is a proyect baseb in Box2D exist CollisionCategory but in Box2D to resolve different
please, review the follow discussion
the idea is to assign that body type is each, and that bodies can collide only.
in the discussiontreated a similar case, A hits B, C hits D, but A does not hits A

I think, when you use filters, you need apply filters to walls.
And set maskBits of all.
MovingBodys exemple :
fixDef.filter.categoryBits=0x0004;
fixDef.filter.maskBits= 0x0002 | 0x0006;
(0x0006 is the categoryBits of wall)

Related

Spritekit contact detection crash(EXC_breakpoint)

I set up a game where the player controlls a ship and shoots the incomming enemies. When One Bullet and ONE enemy make contact, there is no crash but when two bullets hit 2 different enemis i got a crash in the following func.(EXC_Breakpoint) (all bullets has the same physicCategory and all enemy has the same physicsCategory.
How can I solve this?
Thanks!
if body1.categoryBitMask==PhysicsCategories.Bullet && body2.categoryBitMask==PhysicsCategories.Enemy && (body2.node?.position.x)!<self.size.width{
All Sprite-Kit collisions are between 2 bodies - you can’t have 2 bullets hit 2 enemies. If it looks as though 1 bullet has hit 2 enemies at the same time, what you will actually get is a call to didBegin() for the bullet and one enemy and another call for the bullet and the other enemy. However, if you do removeFromParent() for the bullet for the first collision, then you may have problems for the second collision as the bullet node will be nil, although the bullet’s physics body will still be there.
Search on SO for “Sprite Kit multiple collisions” as this is a common problem and there are several ways to handle it. The easiest way to describe is instead of doing removeFromParent for nodes that should be “destroyed”, add them to an array (or more properly, a set). Then in didFinishUpdate, iterate over this set and remove all the nodes that are in it.

Unity3D: Letting a BoxCollider2D collide with the floor 'earlier'?

Pre-info:
I'm making a 2D game in Unity which behaves like Castle Crashers, where the player can move around forwards and backwards like in a sidescroller, but also up and down, kind of like a topdown game - but it's still a 'sidescroller'.
In Unity I'm using Rigidbody2Ds and Boxcollider2Ds for physics.
However, when wanting to simulate things like dropping items, creating gibs or any other object that needs to fall to the 'floor', this gets tricky.
The objects that need to fall to the floor don't know where the floor is, so they'll fall forever.
Question
Can Boxcollider2Ds be set to collide with an individual infinite x-axis?
Object A should collide with the red axis and Object B should collide with the blue axis.
Is this possible?
You could use layers. And in project settings -> Physics2DSettings set them not to collide with each other. There is a hard limit of 32 layers and first 8 are used by system (you can still use them for this) this leaves you with 24 discreet layers - change layer of your objects when they change their position on Y axis. The gameplay might feel awful.
Use 3D physics. tilt your camera 45 degrees on X axis, set projection to ortho, and draw 2D sprites on top of invisible 3D physics objects - then you will have real 2D plane to walk and jump on.
Don't use box2d at all: write your own - simple physics library, you need it only for jumping and falling, right ?

How to make a sprite have gravity?

xcode 5 iOS7 sprite kit.
My wish is to make a sprite that has its own gravity.
Like a planet. If another sprite comes within a certain range of it, it will slowly pull the other sprite closer.
I have two sprites. One moving and one stationary. When the moving sprite gets in a given distance of the stationary sprite the stationary sprite gravity should slowly pull the other sprite towards it. This way the moving sprite would change its path in a soft curve.
My idea would be to calculate the distance from the stationary object to any other object and if close enough start pulling and if the moving object gets out of range ageing, then stop pulling.
Would probably need to research some vector calculation.
Thats my immediate thoughts.
Is this possible and how? Does it already exist?
A friend of mine did this for his physics dissertation. multibody gravity simulation. So yeah you can but you need to be willing to learn some maths. Apparently there is a clever optimisation to make it run decently nlog(n) rather than n^2). you probably want to ask this on the physics version of stack overflow to get a good answer. I have the code at home ... will post it later but you will want an explanation - i used it in an xna app. Its badass once you get it working - although if you want naturally orbiting objects then you will want to code them using parametric equations for easy and cool orbits. Simply because its hard to solve and with time even using double will result in some errors (the good implementations also work out the error and adjust - again will post later). But the real problem is solving for stable orbits. You then use the algorithm for free moving bodies such and player objects / npcs. Although solving accurate movement for npc in a changing field is v hard.
you want to look at this question: Jon Purdys answer is the one you want
multi body physics - gravity
and this one (which is linked from above) ...
https://gamedev.stackexchange.com/a/19404
There is not a dead-simple way of doing that in any platform as far as I know maybe except for some game engines/platforms that export for different platforms (cocos2d, construct 2 etc).
What you need is a physics engine whether you write one (which is a complicated but fun thing to do) or use an available one.
Luckily I've found a link describing the usage of the famous 2D physics engine "Box2D" on iOS.
This one explains how you can include Box2D in an openGL application (but you can apply this to other environments (sprite kit) I think altough I'm not an iOS guy);
http://www.iforce2d.net/b2dtut/setup-ios
Anyways, you now know what to look for...
For iOS 8.0+ : you have SKFieldNode : radialGravityField()
For iOS 8.0- : one solution could be : 3 to 9
add your sprite ( planet ) with its physics
add an invisible SKNode ( the gravity zone ) with its own physics, as a child of your sprite, but with a radius much more than your sprite's one
you have a little explanation about invisible node here : https://developer.apple.com/documentation/spritekit/skphysicsworld
both, your sprite and the invisible node are centered in a zero gravity world
we look for contact and not collision ( set correctly your bit masks )
when any object get in contact with the invisible node ( gravity zone ), first you remove any moving action or impulse from it and then we add to this object an SKAction to move it toward the center of your sprite ( planet )
at second contact between this object and your sprite ( planet ), you remove all actions again from the object with a velocity = zero

Rope breaks using Revolute joints

i am working on rope physics. i have implemented one rope using Revolute joints ..
I'm making rope connecting dynamic bodies (via b2RevoluteJoints).i successfully created it.Now i also attach one dynamic body at the end of rope . & i m releasing my this rope with this body downwards , now somehow my joints between my dynamic rope bodies are stretched & then breaks , I want my rope looks smoother.
my joints are like this
b2RevoluteJointDef revoluteJointDef;
revoluteJointDef.bodyA = referenceBody;//provided by testbed
revoluteJointDef.bodyB = lastLink;
revoluteJointDef.localAnchorA = startPos;//world coords, because m_groundBody is at (0,0)
revoluteJointDef.localAnchorB.Set(0,0);//center of circle
world->CreateJoint( &revoluteJointDef );
revoluteJointDef.bodyA = lastLink;//the last added link of the chain
revoluteJointDef.bodyB = chainBase;
revoluteJointDef.localAnchorA.Set(0,linkWidth);//the regular position for chain link joints
revoluteJointDef.localAnchorB.Set(0,linkWidth);//a little in from the edge of the circle
world->CreateJoint( &revoluteJointDef );
Is there any way to make strong joints ??
can we achieve this by working on Density of bodies & gravity of world ??
please help .....
I had a similar problem (but with the centrifugal force of swinging a larger object at the end of the rope). To solve the problem of the breaking chain/rope you need to make the larger object less dense, but this then introduces a knock on problem - now your "larger" dynamic object gets held correctly by the rope but does not interact with the rest of the world as it should.
To solve this problem I did something a bit hacky and implemented my own collision handler which would apply a collision impulse to the thing that my larger object collided with as if it were heavier. So...
make your object at the end of the rope very low density so that the rope holds
implement some supplemental collision handlers for your object to make it seem like your object is more dense to other objects (sensor = true may help with this as well).
It is not strictly in the spirit of the physics simulator, as we should be able to set our parameters and let the rules take care of everything - basically it would be nice if the revolute joints were unbreakable.

Objects that defy gravity but collide with others in Box2D. How?

newbie to iPhone game development and Box2D here.
I'm developing a game in which I can move (drag) otherwise stationary objects that can collide with other objects.
How do I make these objects stationary in a Box2D world with gravity while maintaining their ability to collide with dynamic bodies?
One random thought is to exert a force equal to gravity on these objects all the time. Any better or simpler approaches? Will static bodies help?
Static bodies will collide with dynamic objects but not other static objects. That sounds like what you want, but it's not clear from your description what the "other bodies" in question are.
You cannot disable gravity on a per-object basis; exerting a force equal to the opposite of gravity will usually work but can, due to rounding errors, accumulate small velocities. A better approach is to set gravity to 0 and manually apply a gravitational force on the objects you do want affected by gravity.