Creating a Platform in sprite-kit - sprite-kit

How would I make a sprite so that an object would be able to move through the bottom of it, but bounce off the top? as opposed to just being bounced off from any contact.

You could just add a 'jump' state check for your sprite,and while he is moving up turn off collision checking for the platform on your sprite, then when falling down, reenable it. You may have to add a check for whether or not the sprite is inside a platform though, because the physics will get you if you enable it while inside.

You could create two nodes of the same width that are stacked on each other (therefore looking like one). Give their physicsBody a different categoryBitMask and contactTestBitMask to differentiate them.
Now check for collisions and buffer them in a simple list. If you now handle the collisions just check which collision occured first: The collision with the upper or lower node? Then you know whether you need to add force (i.e., bounce) that object or not. :)

Related

How do you wrap a level around based on the players position

I want to create a circular room in a 2D level. How can I handle this problem?
My thought process was to break the level into chunks and move their position depending on where the player currently is. This would allow the level to wrap around depending on where the player travels. I can do this manually with each part but i'm looking for a better solution that can handle this programatically. I'm open to better ways to solve this problem as well.
Is the space 2D? if so, you could place two invisible colliders at the extremities of the room (one at the beginning and one at the end), and change the player's position when he collides with them. To ensure that the transition is smooth, place them a little outside of the camera space: the player won't be rendered during transition, and you would obtain a teleport effect from side to side.
As another suggestion, you can lock the player to being in the center chunk with a camera just showing that chunk. Everytime he gets through a collider on the end or the start of the middle platform you delete the opposite side platform and place it in the far end of the platform the player is now seeing, effectively making the new platform as the middle one.

remove bouncing from PhysicsBody?

How can I completely remove the bounciness from my PhysicsBodies, I already set the restitution of all PhysicsBodies to 0 but when multiple PhysicsBodies collide they still bounce off of each other, how can turn this off?
Here's a video where i swipe up and down which triggers this code on the blue SpriteNodes:
node.physicsBody?.applyImpulse(CGVector(dx: x, dy: y))
https://imgur.com/a/MD2Sc
I want the blue SpriteNodes to just stay up or down and not bounce away
Are you experimenting in a space without any world damping/friction? If so, you will need to zero the velocity of objects the moment they collide in order to get zero bounce. But you'll also need to move them outside the boundary of the objects they collided with.
By way of explanation:
This is, I presume, happening because your objects are achieving a (slight) state of overlap right before the collision is reported to the physics engine. When this happens, the first response of some physics engines is to remove this overlap by moving the object with a corrective amount of force or movement. What's happening looks like a continuation of that movement.
In some physics engines you get the opportunity to determine how strongly the corrective behaviour is, and how frequently it occurs, and even set the physics simulation to much higher rates of solving than the frame rate. In these engines you could find the right balance of settings to prevent this. But not in SpriteKit.

Unity ignore collisions on the same layer

I want to use Physics.IgnoreCollision to avoid bullets hitting themselves.
The bullets are spawned on layer 8. Why is this not working? How can you ignore collisions with everything on the same layer?
// bulletscript.cs
gameObject.layer = 8;
// maingamescript.cs
Physics.IgnoreCollision(8,8);
There may also be other objects on layer 8 that should also be ignored.
(for example, the player ship).
Why not use the handy dandy Physics Manager. Go to Edit->Project Settings-> Physics and set up the proper layer collisions.
Go to Physics Manager and un-check the layer with itself to avoid collision.
Troubleshot:
Unity bug, try to update to latest Unity 5.3.x (5.4 currently has nasty bugs)
Make sure the bullets and players are effectively in the wanted layer
The change in layer takes some time to take effect (few frames), since bullet are fast, you are probably encurring in that lag (so in the time the change take effect the bullet already hitted the target)
The best way is to have a Bullet Prefab that is spawned already in the correct layer, you can later customize that by changin graphics at runtime if you like, but to avoid the lag it should be instantiated already in the correct layer.
There is no need to go to physics manager, the OP already do the correct code equivalent to the physics manager. The real problem is that the GameObject should be already instantiated with the correct layer, because layer update may take some time to get effective.
Another workaround is to disable/reenable the collider.

Can I move dynamic physics bodies using SKAction when only contact detection is needed?

I am looking at tutorial where things are defined like this:
planes are sprites with dynamic physics bodies
plane moving is done with actions by following the path
there is contact detection between bullet and plane
bullet is sprite and it has physics body set to be static (which is little unusual in my opinion)
Here is the link to tutorial for more information.
Questions:
When we use actions to move physicsBodies is there a difference how we set body's dynamic property? Because bullet is static but still there is no problem for movement.
When we have situation like this, where we don't need collision detection, but just contact detection, and we can't move sprites (enemies) by applying forces or impulses, what is a good approach? Is this approach correct?
I think this is nice way, but I would like to be fully aware what is really happening when we use this method and are there any drawbacks or possible problems.
There are posts on SO that suggest we shouldn't use actions for moving dynamic physics bodies. I am aware that we can't use this approach in every case. For example this would not work for moving platform with other object on it, because there would be no correct physics simulation between body on the platform and platform moved by action. But in cases like from this tutorial, where we only need contact detection for object that can be moved only by actions (moved along path) I suppose it's not a problem ?
static means that the body isnt affected by physics. That doesnt mean it cant be manually repositioned or moved. So if something is set to static, it participates in the physics simulation, but it isnt affected by it. Think of a plane hitting a mountain. The plane is dynamic, the mountain is just going to sit there even though its participating in the physics. But you could still move that mountain around manually using a position or an action.
Not totally understanding your question, but I'll give it a shot.
You can move physicsBody's manually (using position property or actions), but you need to ask yourself why you're doing that. You typically don't want to do it because they're bypassing the physics simulation. If you're forcing it to move around, what's the point of using a physics body in the first place.. right?
But if your physicsbody is something like a powerup that is totally static, and you just want it to move around in a circle using an action then thats fine. You probably just want to use contact detection for the bullet, powerup, etc without actually moving it around using physics. Nothing wrong with that.

Removing a fixture on body (cocos2d/box2d)

I have several balls bouncing around the screen, each with its own body and can bounce off one another. The user can pick up a ball and drag it around the screen.
I'd like to "kill" the selected ball's so that its temporarily removed from the space as long as the user is touching down on it. So other balls will bounce through it as if it weren't there. Upon release, the ball will regain its physical properties and can resume being bounced around.
How should I execute this? Should I remove the body entirely and re-create it upon touchEnded? Any ideas or help appreciated. Thanks
I believe you can't add/remove fixtures to an existing body. The way to achieve what you want is to disable the body (set it's active state to false). You could also change it's collision filter/mask so that it won't collide with certain game objects, but still collides with world boundaries for example.
You can wrap your Box2D body in a custom class that will keep the definition (bodyDef and fixtureDef are reusable) then you can :
destroy / recreate the fixture at position 0, 0 on the existing body moved by the touch
OR destroy / recreate the entire body at the touch position
I believe managing fixtures is better for performance.