I am aware of how to use the SpriteKit physics engine to detect when a collision occurs between two nodes, however, I'd like to be able to detect this and also get the direction relative to one of the objects that the collision occurred. Incase anyone is curious, I'm asking this so I can make a platform game and I need to detect when something is resting on the top of the floor. Any help is appreciated, thanks!
Implement the SKPhysicsContactDelegate on one of your objects (perhaps your scene object). Set your scene's physicsWorld.contactDelegate to the object.
In the contact delegate's didBeginContact(_:) method, you are given an SKPhysicsContact object. The contact's contactNormal property should be the direction you're interested in.
Related
I'm attempting to develop a 2.5D fighting game in Unity, and I've gotten to the point of attempting to make basic collision between players function. I'm attempting to do so via collision boxes like those seen in the video here: (4:01 if the timestamp doesn't work)
https://youtu.be/m5yRLhAx4Ro?t=241
What I have attempted to do in order to replicate this system of collision is to make a collision box prefab with a dynamic rigidbody and box collider. A script instantiates a new one as a child of the player and destroys any existing ones whenever the collision box's size and position need to be changed. However, I've found that this does not work. I can get the players to collide with each other, but when I disable that collision by putting the parents on a layer that doesn't collide with any others, the collision boxes themselves will just phase through each other along with their parent objects, the players.
What I want is a system in which the players will be responsible for their movement, but the child objects (the collision boxes) will function as the players' colliders. If anyone knows a way of doing this, or any other way of achieving this sort of system, I would greatly appreciate the help.
Just add a Fixed Joint on the child object then connect it with the parent rigidbody.
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.
I am making a simple game, using Sprite Kit and physics.
The problem is actually very simple, to my knowledge Sprite Kit allows to set gravity on physicsWorld, but I need to set gravity on physicsBodies in a way that a larger/heavier body influences everyone around it. By the life of me, I cannot find a feature to do this and before I start programming custom physics into the game, I want to know if anyone here knows of a standard way to do this.
You are looking for the SKFieldNode. There are different kinds of fields you can create, but it sounds like what you want would be a radialGravityField.
You can find out more at the linked Class Reference.
For each node, iterate over all other nodes in the scene and compute the net force acting on that node from the acceleration due to gravity. You can then applyForce to that node.
There are some new additions to SpriteKit in iOS 8, including numerous enhancements to physics. If you can target your game to iOS 8 and later, I suggest you check out the beta.
i just started working with SpriteKit and encountered a problem:
I want to have an object, i call it playerController, that follows the players touch and a puck, with can be hit by the controller and will in that case bounce of the controller.
Somehow like an AirHockey game.
I constructed 2 SKNodes, with texture and physics, so they bounce of each other properly if gravity is on or if the controller is moving by some velocity.
BUT if the controller is moved by the following action the puck does not bounce of.
SKAction *moveTo = [SKAction moveTo: CGPointMake(touch.position.x ,touch.position.y) duration: 0.1];
[self.controllerPlayerOne runAction: moveTo];
Why? I already searched in the documentation and in several tutorials, but none addressed my problem. Maybe this is wanted by the SpriteKit developer, but how is in this case the realisation of following an touch?
Hopefully someone can help.
Edit:
Currently, i got the controller puck behaviour by setting the controllers position to the touch position and calculating the speed by using the last and the current position of the touch, each time the touch moved.
This is working pretty good.
This is by design. The same behavior can be observed in cocos2d btw.
The thing is: move actions move the sprite, not the physics body. Even if the physics body does follow the sprite, the move action overrules the physics body motion, at best putting it where the sprite is.
Instead when using physics bodies you must move them by changing the body's velocity, you can no longer reliably use the move actions. The physics body then moves the sprite.
I'm a beginner when it comes to iPhone development and i've searched for the answer to this question and haven't found anything yet.
What i'm trying to do is allow the user to move an object without actually touching the object (they can touch anywhere on the screen) and as they move the object will go from where it was originally and move in the direction of the users movement but not jump to thier touch location and be covered up by their finger.
Any help you can provide would be phenomenal
Thank you in Advance,
BWC
You can subclass a UIView to track the touch movements by overriding the touchBegan:withEvent:, touchMoved:withEvent: and touchEnded:withEvent messages. In these messages, track the UITouch objects' locations (converted to points on the view by using the convertPoint:toView: message of the UIView you're subclassing) and calculate the difference of movement; then apply the same difference to your object, wherever it might be on the screen.
I know this is old.... But here is a great tut doing this with Cocos2d and sprite animations.
http://www.raywenderlich.com/1271/how-to-use-animations-and-sprite-sheets-in-cocos2d
Derek