SpriteKit: Which SKFieldNode to use for repulsion, as in an explosion? - sprite-kit

I have some ball-shaped sprites that bounce around the scene (via physicsBody), sometimes hitting a bomb.
I want the balls to be thrown outward from the bomb, as if by an explosion.
What's the best SKFieldNode to use for this explosion effect? Or is SKFieldNode not the best way to accomplish this?
I tried SKFieldNode.magneticField(), but it didn't give me the results I'm looking for.
Here's an example of what I've tried:
The field:
let field = SKFieldNode.magneticField()
field.region = SKRegion(radius: Float(UIScreen.main.bounds.width * 0.40))
field.falloff = 1
field.categoryBitMask = CollisionTypes.explosionField.rawValue
bombNode?.addChild(field)
The balls:
ballNode.physicsBody?.charge = -1.0
But with this setup, the balls are repelled in strange, unexpected ways rather than the nice explosion effect I'm looking for.
Thanks for your input!

For a radial repulsive force, you probably want to use electricField.

Related

Unity - Blocking character from walking off ledges

I don't want my player to be able to walk off ledges. I did this by shooting a single raycast downwards in front of the player, and if ground is NOT hit, then ignore input.
However this is jarring, especially if you diagonally walk along an edge you just completely stop, rather than 'slide' along it.
So I thought I could add two raycasts, one per side to detect which side the ledge is, then allow movement (or steer) the player as applicable.
The problem is I'm not sure how to proceed from here. I'm using a character controller for movement, my current code is like:
velocityXZ = velocity;
velocityXZ.y = 0; // we deal with gravity elsewhere
velocityXZ = inputDir * playerSpeed;
if (facingDropLeft || facingDropRight) {
velocityXZ.x = 0;
velocityXZ.z = 0;
}
velocity = new Vector3(velocityXZ.x, velocity.y, velocityXZ.z);
// handle gravity
charController.Move(velocity * Time.deltaTime);
Could anyone offer some insights into what direction to look into, or methods I will need?
I think that if you want to accomplish an enjoyable result you should use invisible walls. I think that probuilder can help you.
This is the approach I would have with this type of problem.
Use boxes to make wall than turn off the mesh renderers this will make invisible walls

Moving a Sprite Swift

What I'm trying to do is moving a sprite in a random direction or a random point and have it continue moving - with the affects of physics. I tried moving a sprite to a random point and although it worked, it just stopped if the random point was in the middle of the screen. I'm just hoping for the basics so any code/help from someone would be helpful. Also if you think theres a post already on this, can you list the hyperlink? I spent some time researching and I couldn't figure it out. Thanks in advance!
See SKPhysicsBody and methods like applyImpulse.
In short, you create and configure an SKPhysicsBody; assign it to the sprite; and then apply force vectors to the body. Procedural example:
let sprite = SKShapeNode(circleOfRadius: 10)
sprite.physicsBody = SKPhysicsBody(circleOfRadius: 10)
sprite.physicsBody.friction = 0.2
sprite.physicsBody.restitution = 0.75
sprite.physicsBody.mass = 0.5
sprite.physicsBody.applyImpulse(CGVector(1, 1))
Much of this you can configure declaratively, rather than procedurally, with the SpriteKit scene editor in Xcode.

Programming controls for sprite

I am trying to make a snake kind of game on the iPhone. For that I plan to have a simple dot as the sprite which draws a line. The dot is controlled by two buttons on the left and the right of the screen enabling it to turn left or right. Now I really do not have any idea how to make the sprite move automatically forward and how to program the turning. It should not be exactly as in snake where it abruptly turns left or right but it should more be a smooth curve. I hope you're getting my point and I'd appreciate all kinds of thoughts! Thanks a lot!
Somewhat trying to make it like this:
http://www.bnet.im/images/screen_curve.png
There are a lot of ways of doing this, but one simple way would be to store the angle of the snake's trajectory (I've called it theta) and move it a fixed amount in every call to Update.
Assuming that the snake class inherits from CCNode:
-(void)Update:(ccTime)dt {
self.position = ccp(self.position.x + cos(theta)*dt,
self.position.y + sin(theta)*dt);
}
You could then update theta from your event handling logic by increasing it or decreasing it when the user taps to turn left or right.

Collision Detection between two rectangles

Fairly simple question that Im sure you will laugh at me for.
I have two rectangles playerRect and wall.
I have an if statement with the condition being ..
if (CGRectIntersectsRect(playerRect,wall)) {
//handle collision here
}
The problem I'm having is working out which side actually hit the wall rectangle.
I need to know because then I stop te player from moving depending on which side hit.
Thanks for any help
Disco
I would add some direction property to my 'Player' object. This way when you detect a collision, you just check to see which way the player was moving prior to the collision and react accordingly.
Create a CGRect for each side of your object with just a width of 1 (or height of 1 depending on the side) and look for intersections with the sides instead of the entire object. If your object is moving faster than 1 pixel per collision check, then you would check the sides in addition to checking the entire object

Box2d: mousejoint without inertial delay

I'm using mousejoint to drag bodies in box2d, but it causes inertial delay.
Does it exist any way to drag a body instantaneously?
The solution is to tune up properties frequencyHz and dampingRatio in your b2MouseJointDef.
For example:
b2MouseJointDef md;
md.body1 = _groundBody;
md.body2 = body;
md.target = p;
md.maxForce = 10000.0f * body->GetMass();
md.dampingRatio = 0;
md.frequencyHz = 100;
_world->CreateJoint(&md);
I'm trying to implement a pong-style game in Processing/Box2d library and I anticipate having the same problem. One thing that comes to mind is to maintain a hidden object in the Box2d world, one that operates with joints the conventional way, and then draw a virtual object that follows the mouse with no frame delay. This might be adequate to fool the user.
On the other hand, Box2d is not a strict physics simulation and allows for some forgiveness in overlapping objects, so it really seems like there should be a way to do this.