Is it possible to explain how bitmasking works for just this simplistic situation:
A big (blue) ball. Contact BitMask 0b0001
A medium (red) ball. Contact BitMask 0b0010
A small (white) ball. Contact BitMask 0b0100
They have no collisions between them, because somehow this is turned off between them. I assume by setting their collision masks to 00 all the way through the 32 bits... but let's leave that for another question, I haven't yet figured out how to turn these off.
Having set each ball to have a unique contact bit mask, what is done that gives contact information when they contact?
How is it possible to know which two balls have contacted each other?
Is it possible to have the contact information only received by the biggest ball in any given contact?
UPDATE:
I'm not necessarily after code. If you need code to explain what's what, how it works, and why whatever does what, go for it.
But what I'm really after is an understanding of how bitmasking works to provide logic that permits the determination of "who" was involved in any contact.
The reason I've given each ball size a different bitmask is that I thought this might help determine the bigger ball in any given contact between two balls. I could be wrong about this, though.
UPDATE 2:
Understanding of processes progressing:
As I understand it, these are the parts of the process:
Register SKView subclassed SKScene as conforming to the physics world contact notification delegate. Say it is so, too.
Set bitmask as categories for each type of bodily interaction type desire to be known about and/or controlled in the simulation
Use these bitmasks to articulate the nature of each object desired to be a part of contacts and/or collisions by applying them appropriately (some magic decision making in here).
Override the contacts callback with code that does stuff, in the same SKView subclass registered as the delegate
Create some magic code that determines who/what has contacted whom.
I understand some of these, but not the differentials of setting contact bitmasks versus the reasoning for the naming of category bitmasks, and not how to determine who has contacted whom.
In almost all physics engines, collision layers are represented by bit masks. All objects in the same collision layers will collide with each other. In the bit mask, each layer is represented by a bit. If the bit at position 0 is on, the object will collide with other objects which have the bit at the same position in their bit mask on.
While checking for collisions, the physics engine's broad-phase routine only reports those pairs as potentially colliding which have at least one common bit set to 1 in their collision bit mask. This check is simply done by ANDing (&) the two bit masks. The potentially colliding pair is reported to the narrow-phase routine only if the above check returns a non-zero result.
For example, if you wanted the blue ball to collide with both the red and white balls but wanted the red and white balls to not collide with each other, you'd set the bit masks to somewhat the following:
Blue -> 0b0011
Red -> 0b0001
White -> 0b0010
In your example, since none of the bodies have a common bit set in their collision bit mask, none of them collide with each other. In the above example, since the red and white balls don't have a common bit in their bit mask, they don't collide.
You might want to set all the collision bit masks to -1 (which turns all bits on) and remove bits/categories from the mask by XORing. The above example would then be implemented by the following pseudocode:
blueBody.collisionMask = -1;
whiteBody.collisionMask = -1;
redBody.collisionMask = -1;
redBodyCategory = 0b0001;
whiteBodyCategory = 0b0010;
whiteBody.collisionMask ^= redBodyCategory;
redBody.collisionMask ^= whiteBodyCategory;
I don't use SpriteKit, but from the little information I obtained from this SO answer, the contactTestBitmask sets the categories with which events will be raised on contact, while the collisionBitmask sets the categories with which the body will actually collide. So if you want contact information only from contacts which involve the Blue ball, you'd set the contactTestBitmask to the values as in my example above.
To check which balls are colliding with each other, you'd have to check the bit mask values and discern which one is which. In the above example, the Blue ball can be recognized by checking if body.contactTestBitmask == 0b0011, and so on for each other body.
As a side note, you might want to look into using a proper game engine like Unity that will make all this much easier.
First, you need to define what your sprites are, this is what the categoryBitMask is for. Think of this as a binary name for your sprite, with each digit being a unique name. Now it is possible to give your sprite more than 1 name, but for simplicity purposes, let's keep it to one.
contactBitMask tells the sprites what names it should be looking for.
So during the physics phase, the engine will take a given sprite, and look for all other physics sprites with the search name provided in the contactBitMask.
After this, evaluations are performed against the original sprite and the sprites in the filtered list.
On a contact, didBegin(contact:) is called, and contact contains all the information you need, including the 2 contact bodies. You then check the categoryBitMask to get the name of the sprites in question, and you do your conditioning based on these names.
Related
I'm pretty new to unity and I set up a simple scene with my character and the ground. I added a Rigidbody2D and a BoxCollider2D to the character and another BoxCollider2D to the ground. The boundaries of the Colliders fit the boundaries of the character / ground exactly, but my character is not falling on to the ground, when I hit play, but rather stops and floats a few pixels above it.
I've seen a lot of similar questions but the solutions didn't work for me (or maybe I didn't understand them right, as I mentioned above I'm new to Unity and thats my first game).
That's what the floating looks like:
I could obviously "fix" this by lowering the upper boundary below the actual boundary of the ground, like this:
But that's obviously not a good solution and I'd still like to know what is causing this bug and how to fix it.
Maybe gap has size of Default Contact Offset:
Set a proximity distance value for colliders to be considered in contact, even they are not actually in contact. Colliders whose distance is less than the sum of their contactOffset values generate contacts. This allows the collision detection system to predictively enforce the contact constraint even when the objects are slightly separated.
Caution: Reducing this value too far could cripple Unity’s ability to calculate continuous polygon collisions. Conversely, increasing the value too much could create artifacts for vertex collision.
You can find it in Edit > Project Settings > Physics2d .
Lets say I create many different 3D objects (Arms, legs, body, head). How would I go about making it so all of them can connect to form a body. And after the body is formed make it so they all animate in the same way. Can anyone help me in the right direction with tutorials or links on these topics?
Here are some good tutorials:
https://www.youtube.com/watch?v=q3oVAXhvQl0
https://www.youtube.com/watch?v=Ch1aJljEYp4
https://medium.com/#larissaredeker/3d-character-customization-fd95a1d57ae
And here a reddit user's answer(Named DankP3):
For body sliders you will probably want to look at blendshapes, this is done in the modelling programme. The more blendshapes you have the more complex it will get. eg. 'normal' 'thin' and 'muscular' bodies at one scale of variation and thicker eye brows at the other.... Of note though, facial morphs don't affect most clothes!
I think one of the most challenging parts will be that using the same animation on a thin character and a muscle bound character will not look good so some significant animation work may be required.
I'd probably apply hairstyle in the same way as clothes (see below), but colour can be manged for hair easily through code, swapping base colours, or textures.
For clothes, you rig to the same skeleton as the character (and you will need the same blendshapes to fit the body or if your body is to change shape). A lot of people put all clothes in the same model and turn them on and off as required (this intuitively seems messy), but looping through the bone array on the meshrenderer is not difficult and contrary to what the other reply says you don't need to match bones by angle and position. if you rig correctly, you can just swap, rather than match, the bones in the clothes mesh renderer for those on the character model when the clothes are put on and you are all done.
Again, all credit goes to DankP3
You can see his answer HERE.
I'm working on a 2D physics game that involves collisions between objects which at the moment use complex polygon colliders.
When the objects collide, I get the normal of the contact point using otherObject.contacts[0].normal, and knock the objects apart in equal-but-opposite directions using Rigidbody2D.AddForceAtPosition, with the force being the normal multiplied by a constant.
Most of the time this works flawlessly, but I've found that when the collision occurs with a concave section (aka: an inward "dip") in the polygon collider, the normal will be flipped, and the objects will instead get pushed towards each other.
Alternatively, are there any other ways I could go about solving this?
The blue circled area is an example of a problematic concave section
Alternatively, are there any other ways I could go about solving this?
Yes, the standard thing in vid games is that most stuff has many small simple colliders, rather than one large complex collider.
This is a basic of game engineering.
(It can be very surprising to hobbyists and folks new to the field.)
So, imagine a car in any ordinary 3D game. You'd have a collider for the rear bumper, one for the front, maybe one for "left doors" and so on. Very typically, each has to react in a different way, and you need to know which area was touched.
In your case if the 2D poly has say 12 edges, just make 12 "small" as it were colliders for each of the edges.
We know nothing about your setup since no screenshot, but that could possibly work.
Note however that Unity's 2D poly collider in fact already does know to slice the object in to smaller triangle-like shapes if it is concave - I'm surprused it dinnae work for you.
Further: now that we can see your image.
In any video game on Earth, the way you'd do that helmet is with a square collider as in orange:
If (for some reason .. why? for what purpose? how? where? what possible reason could there be?) you were making the most precise video game, ever created by humans, on an entirely new plane of engineering, for some imaginary new hardware with quantum warpspace cores, ... in that case ... you'd maybe add the two extra colliders to cover the horns. But nobody would ever notice the difference.
I appreciate you may be doing something exceptionally unusual, like a "close up game" ("you're an atom in medieval Scandinavia, bouncing off helmets" or whatever), in which case there'd be some other solution.
The very short answer is you've stumbled on one of the most surprising things about game technology ... we use crappy, simple, colliders, you've been tricked all your life in every title you play!
I'm new to using xna and I want to make my player collide with with multipe walls from the same class. So I looked around and I understood that the best way for doing that is to create a list of variables containing the walls id's and make a loop that circles them all and then returns the variable of the objects that collide.
My question is if there is a faster more efficient way for doing that? I mean if I have like 10000 objects that loop can cause a lot of memory use.
Thx in advance
Option 1) If these 10000 object are walls of a level, then you should probably use some sort of grid (like this very old example: https://en.wikipedia.org/wiki/The_Legend_of_Zelda#mediaviewer/File:Legend_of_Zelda_NES.PNG)
With a grid you only have to check collision with adjacent objects, or only with objects that are nearby.
Option 2) If these 10000 objects are enemies or bullets that move more freely, then you could also calculate the distance first and only check for collision if the objects are nearby.
But may I ask why you are using XNA? I used to work with XNA 4.x but in my understanding it is pretty much dead (http://www.computerandvideogames.com/389018/microsoft-email-confirms-plan-to-cease-xna-support). If you're new to XNA, I would advice to use other software to make games (like Unity3D). In Unity3D the hard part of collision detection is done for you (is has standard functions for collision detection) and Unity3D also works with C# (like XNA)
You always want to do the least amount of processing to get the job done. For a tiled 2D game you usually have a 2 dimensional grid. When the player want to walk on a certain tile you can check that tile if it is allowed to walk there. In this case you just have to check a single tile. If you have a lot of NPC's you could divide your map into sections and keep track of in what sections the NPC's are. Now you just have to do collision detection on the enemies within your section.
When you need expensive collision, pixel perfect or polygon collision you should first check if an object is even close with a simple radius float or BoundingSphere only then you go on with more expensive collision detection.
Same goes for pretty much anything, if you have a 100x100 tilemap but only need to draw 20x10 for the screen then you should just render that portion by calculations. In unreal, mappers create invisible boxes, when inside these boxes it only draws a certain part of the map and only checks collision within these boxes. GameDev is all about tricks to make things work smoothly.
I am working on game, in which i need different collision behaviours from different side,
Suppose there is a character when it collide to any other obstacle from down side, it will behave differently.
when it collide to any other obstacle or object while it is at top and the obstacle or object is below it, then i need different behavior,
what is the best way to acheive it.-(void)didBeginContact:(SKPhysicsContact*)contact how it be helpfull in my scenario
Thanks
You do not even need the contact point. The method called after you determine what came in contact has access to the nodes in contact. Therefore, you can just compare the x and y locations and handle them accordingly. You can then ignore collisionBitMask and replicate the collisions in the method called by contact.
LearnCocos2D guided me in Right direction
skphysicscontact gives you the contact point developer.apple.com/library/ios/documentation/SpriteKit/… Check where contact point is in relation to nodeA and nodeB position. Ie if posA.x < contactpoint.x and posB.x > contactpoint.x the contact was to the right of nodeA.