SKPhysicsBody, rotate a sprite after applying impulse to change direction - sprite-kit

I am looking for some info on how I can get my sprite to change its rotation by changing the angle of the sprite.
Basically here is how it is set up. Imagine a rocket that is sitting in a stationary position. The physics body is the shape of the rocket, with the tip pointing to the top. Now I apply an immediate impulse to the rocket to send it into the air. While in the air, I would like to use the accelerometer to rotate the sprite. With the rotation, I am expecting the direction of the sprite to change. Unfortunately, The sprite goes straight up and down instead of the direction it is rotated, even though the texture and physics body is rotated. I am having a hard time trying to find info on what techniques are used to achieve the results I want. Most examples have a continuous force, but I only have the initial force with no change after that. I have played around with linearDamping and angularDamping, but that was no help. I have also tried attaching another physics body to the tip to make the tip more heavy to see if the direction will change, but that didn't help either. My only work around that worked is to change the gravity direction, But I would like to avoid this. Any help or direction would be appreciated.

I think the solution you may be looking for is in Sprite Kits SKFieldNode class.
https://developer.apple.com/library/ios/documentation/SpriteKit/Reference/SKFieldNode_Ref/#//apple_ref/occ/clm/SKFieldNode/linearGravityFieldWithVector:
The linearGravityFieldWithVector: is probably your best bet. It applies a linear gravity field in a direction you set.
You can also check out electricField which is similar however you can change the charge to positive or negative.

Related

HingeJoint doesnt open the opposite direction

I was wondering if you could tell me if this is possible. I've spent a couple hours to no avail trying to find a way to make a object with box collider swing inwards and outwards like a door. I used hinge joint, but it will only swing the gameobject inwards or outwards around the joint. I am trying to achieve a 'saloon door' effect that allows free rotation around the y axis. I have tried fiddling with the anchor position and axis values but no luck. I was surprised not to find any similar problems online, perhaps hinge joint is not meant for this purpose? Any clarification is much appreciated.
I'm not really sure how I fixed it, but I got the door to swing freely. One possible reason is surrounding colliders or terrain prevented it from swinging freely. sigh

How to implement trolley-like physics behaviour in Unity

I am stuck with two things while trying to simulate the physics setup for a trolley-like object (push powered vehicle with free rotating wheels on the front and fixed wheels on the back).
I have the RigidBody with its mesh and four WheelColliders, the object moves fine if I just apply torque to the wheels. However if I use the AddForce method on the RigidBody it won't move; I see the object being pushed (slightly balancing) but the wheels won't rotate so it stays in place. How can I get the wheels to move if the object is being pushed?
My other problem is to simulate the standard 360 degrees rotating wheels on the front of the trolley. What would be the best way to simulate this? I was thinking about a horizontal WheelCollider and a vertical one as a child but that seems really weird and I doubt it will actually work. Any ideas?
https://docs.unity3d.com/Manual/WheelColliderTutorial.html
This tutorial shows an example of how to use the wheel colliders and apply steering and torque to them.
It might be what you are looking for in regards to the adding force part.
I suspect it might allow the 360 degree rotation as well, but I am not familiar enough with these colliders to guarantee that.
EDIT:
In the guide there is the line:
public float maxSteeringAngle; // maximum steer angle the wheel can have
Which looks relevant for the 360 degree turning.

Spritekit how to not veer off

I'm building a pacman style maze game.
I can apply impulse and limit it to whatever vertical or horizontal direction as desired on the hero.
However, when I add physics body sprites for walls, it gets weird.
I have set the hero physics body and the walls to have no bounce.
Running head on into a wall it stops dead like expected.
But if I then change direction and move along the wall I veer off at a slight angle from the wall.
Any ideas what causes this and how to avoid it?
I want to only move in vertical and horizontal straight lines.
Ok so the solution here is slightly counter intuitive.
Rather than using applyForce: or applyImpulse: family of methods on the physicsBody, use runAction:forKey: on the sprite itself.
For any change in direction, removeAction:forKey: just before calling runAction:forKey: to keep things orthogonal.
(Otherwise there will be a brief angular movement )
The other trick here is to find an appropriate speed for distance. Too fast and it will fly through walls. (Bug?)
Then apply the speed and distance with a multiplier based on the size of your scene.
The total distance should be something greater than possible in any direction.
That keeps the thing moving until it hits a wall or you tell it to move in a different direction.
This works really well.
Using the physics body movement, it seems best suited to a platformer situation where you have a gravity.
In a top down view, there isn't normally gravity.
( though it is imaginable that you could want something that exerts gravity on a sprite to suck it in)

Cocos2d - Creating collidable Sprites?

Hello everyone I an very new to cocos2d, so I apologize if this is a simple question. I would like to create to sprites that collide when they hit each other.
For instance, one sprite, spriteA, is in a fixed position on the screen. And another sprite, spriteB, is moving around the screen. SpriteB will collide with spriteA. If that doesn't make sense or you don't understand it, tell me and I will elaborate further. Any help is appreciated. Thank YOU!
Try using Chipmunk or Box2d physics systems. These are included with Cocos2d and work by having a physics simulation that updates with every time the graphics change on screen.
The physics simulation will tell you if two objects are overlapping, and will provide physical attributes like weight, slipperiness (friction), speed and direction, which creates reactions like bouncing, sliding, realistic loss of speed and changes of direction on impact.
If you are new to physics simulation, here's a 30 second run down:
Create "bodies" in the physics simulation that represent each graphical sprite
Bodies are usually defined more simply than their graphical counterparts, like a circle, square or simple polygon shape
In order to update the graphics on the screen accurately, first you establish a pixels to meters ratio. Meters are notional (i.e. made up) measurement that is used in the physics sim
Each time the physics simulation "ticks", you update the graphics on screen accordingly
So if a body in the physics sim moves 1 meter, you might transform the pixel sprite 32 pixels
Here's a good tute on collision detection using Box2d.
http://www.raywenderlich.com/606/how-to-use-box2d-for-just-collision-detection-with-cocos2d-iphone
enjoy
Its actually very simple:
Just schedule a timer: [self schedule:#selector(checkForCollision:)];
Specify the method: - (void)checkForCollision:(ccTime)dt {}
In the curly braces, make CGRects for each sprite using CGRectMake.
Then in the same method, just call: if (CGRectIntersectsRect) {}
That easy!
Well technically speaking when the 2 sprites interact, or share at least one common point, then they are colliding. I'm a little confused by your question. Are you asking for direction on how to make the sprite move on screen, or are you asking how to handle the actual collision(such as calling a method if they collide)?

Problem using chipmunk

I made a game that using a character that can moving left or right by touching the screen, above the character, it is a apple, what i need is the apple will fall down, the character may push the apple up again just like playing volleyball.
That's the problem,what i have learnt from "bounding ball" is give a sprite a shape and body, then they would collide automatically,when i give shape and body to the character, the animation is not work, the character moved and after a second he "jumps" to the starting point,whatever i using setPosition or Action(MoveTo) to sprite are also useless
moving the body is not work too.I use touchmoved to set the body->p,it keeps shaking and when i release the button, it "fly" alway out of bound..........
Can anyone tell me what should i do or give a similar example(e.g. volleyball and something like that),Plx help!!!
I appreciate that if anyone can give me a hand.
Adding a chipmunk body and shape to a sprite is not the best way to go about this. Try not to think from the screen's point of view! Instead, create a controller that contains a reference to the physics body and the sprite.
Using a physics engine, such as chipmunk, can be a bit daunting. It helps to enable debug drawing. Have a look into how you can best do that! There is some code you can take from the cocos2d chipmunk example, that comes with the normal cocos2d download)
I take it you've added a "ground" shape as a static shape and have gravity set?
When you move a body with body->p = cpv(x,y); the problem is that you don't change the objects state other than it's position. To the physics engine, it's like you teleport the object. If the object was falling, it will still be falling.
So now, your falling object is not touching the ground. Its speed increases. You "teleport" it again.. but until it bumps into anything, gravity will keep increasing its falling speed.
So, if you move an object to another point, be sure to reset all forces and the velocity on that object, if this need to be done!
A better way than using body->p is to use these functions:
void cpBodySlew(cpBody *body, cpVect pos, cpFloat dt)
void cpBodyUpdateVelocity(cpBody *body, cpVect gravity, cpFloat damping, cpFloat dt)
void cpBodyUpdatePosition(cpBody *body, cpFloat dt)
The documentation of these functions isn't too ridged, but there's other documentation out there. Give it a google :-p I haven't fully mastered using these functions yet either (I'm still trying to get around Chipmunk too) but they're the way to go about it.
Alternatively you can (instead of setting body->p) set the velocity of the object directly (though cpBodySlew does this for you)
The latest chipmunk code is doing this with a pivot joint in between the object to be grabbed and the mouse (touch): ChipmunkDemo.c.
See also: Mouse interaction