How to find direction (in radians or degrees) of a physics body - swift

I have a physics body that gets hit and rotates a lot. I want to limit the amount it can rotate (only 45 degrees in each direction or something) and I wanted to try to do that by applying torque if the direction goes past a certain point. I'm ok with the sprite wobbling back and forth (it's a bird).
I'm open to other suggestions on how to limit the rotation as well.
Thanks in advance!
Edit: I've already tried using zRotation (I printed it out and it was always 0.) I also tried using the vector but that gave me the direction the sprite was going.

Related

SpriteKit move node with physics rather than updating position

My main character moves by touching and holding him and then moving your finger left or right to move the character. To do this I just simply update the node's x position (walks left and right on a flat surface) in touchesMoved() with the x position of the touch location, and apply an animation depending on the direction he's moving.
I want to kind of take this to the next level and accomplish the same effect, but using physics, so that when I'm done moving him and release my finger, he may slide a little bit in the direction he was moving given the speed I was moving him at, if that makes sense. Does anyone know how I can accomplish this effect?
Would I have to do as I'm currently doing and update the position as it moves, but also apply a force/impulse at the same time? Kind of confused on how to approach this
Moving the physics body via force, impulse, or velocity will automatically update the player position.
So you will have to play around with the correct way to accomplish your goal. But based on this, what I would suggest is replace your .position code with .physicsBody!.velocity code in your touchesMoved. Then, on your touchesEnded, you can apply a bit of an impulse to give the player that little bit of an "on ice" effect to keep them going a tad.
You will need to experiment with how much velocity you want to get the character to move at the correct speed, and then you will need to play with the impulse figures as well to get it just right.
This could get a bit tricky though, in touchesMoved... because at some point you will want to reset the velocity to 0 (so they stop moving when your finger stops).
To do that, you will need to to use the .previousLocation from your touch object, and compare the distance of X moved. if the distance X moved is >0 (or some deadzone threshold) then apply the velocity; if the deltaX is 0, then set the velocity to 0.
This may actually be more complicated than just using .position to move the character, then having the character slide a bit with physics on touchesEnded.
You will have to play with it to get it right.

How to smooth movement in Unity 4.3 2D with AddForce?

at the moment i'm working on a 2D plattformer and the work is going very well. But there is one Problem i can't get rid of.
The player can use a dash, which should move the player very fast in the direction he is looking. My problem is, that the player game object instant appears at the target location... like a teleport.
I'm using the AddForce Function to move the game object. On jumping i'm using AddForce as well and there it works really nice, i get a smooth jump movement.
The only different between dash and jump is that on jump i apply force to the y axis and on dash to the x axis. Changing the amount of force doesn't affect the movement only the distance.
Does anyone have an idea what i'm doing wrong?
// Dash
rigidbody2D.AddForce (new Vector2((dashSpeed * direction), 0));
// Jump
rigidbody2D.AddForce (new Vector2(0, jumpForce));
Best,
Verdemis :)
EDIT: I can not access my project at the moment, but i will try to show you what i have done.
Note: dashSpeed is a float value, at the moment something like 3500
and direction contains 1 or -1, depending on the direction the player is looking. The Dash code is part of the Update method.
// Dash
if(Input.GetKeyDown(dashKey))
rigidbody2D.AddForce (new Vector2((dashSpeed * direction), 0));
What is your direction vector, is it normalized? Since multiplying non-normalized vectors can be rather hazardous. Do you have more of the script to show?
EDIT: You should always do physics things in the FixedUpdate loop(link).
Ok i could solved the problem. My problem was that i only did this AddForce only once. The AddForce code was only executed in a single frame. I added a time which i count down to define how long the dash movement gonna be!
The problem may be that you are using a very big force, I was messing around with some physics today and realized that even a force of 100 almost looks instant. Try making a smaller force number to see if that helps you. I just tested making a smaller number and that does not work.

Unity physics about collision and energy

I have been working on a Unity ping pong game using the Leap Motion. I use rigidbody.MovePosition() to move the paddle. However, when I hit the ball (which uses gravity), the paddle launches it too far. Even when I change the masses of both, it doesn't do anything.
What variable should I change to reduce this energy going into the ball?
From the following link.
"MovePosition will put your object at the target location, no matter what. It may push aside other objects in a realistic way, or may blast them out of the way, or may just pass right through them. But it will gladly move you through a solid wall or a mountain.
If you're using MovePosition on a rigidbody to add from where you currently are, it looks like AddForce. With AddForce, the physics step does all the work: applies your velocity, sees the collision and handles the bounce. With MovePosition, the physics step sees you're mysteriously overlapping a solid object. If it isn't too much, it will bounce you apart."
You won't need to use MovePosition. Instead, you can figure out the direction of the shot (based on the position of the ball relative to the paddle). Then you can add an impulse force in that direction to the ball.
Pseudo-code (from the following link):
Vector3 shootDir = ballPosition - paddlePosition; // Calculate direction of the shot
shootDir.Normalize(); // Normalize the shot vector
ball.AddForce(shootDir * speed, ForceMode.Impulse); //Add impulse force in correct direction.
Credit due to Owen Reynolds and Tim Michels.

How to get a ball/sphere to stop

I got a field in unity3d that has some depressions in it (like small holes). The field's slope always leads towards the nearest depression.
A sphere is dropped at random somewhere in the field, rolls around a bit until it stops in one of the depressions.
The problem is, this is taking too long. It could roll around for 5-10 seconds until it stops. I'd like to stop faster.
Any ideas how I can achieve this?
Edit: The main issue is when the ball is next to the depression, but it has speed that is 90 degrees from the hole, then it starts going in circles and takes a while to stop.
Ok, after getting some advice in the comments, and experimenting, this is the way I solved it:
Apply a small measure of strength towards the depression
If the current velocity is more than 30 degrees away from the center of the depression, slow the ball (apply strength in the opposite direction of the velocity)
IF the ball gets very near the center of the depression, stop it and place it in the center
Thanks for all the tips. If anyone comes up with a better way, I'm still open to suggestions.

Body moves too fast after ApplyLinearImpulse()

I have top-down game for iPhone (no gravity) and I'm using Cocos2d and Box2d.
I try to move a bullet by this code:
// 'targetPosition' is a point of touch
b2Vec2 touchInWorld = b2Vec2(targetPosition.x/PTM_RATIO, targetPosition.y/PTM_RATIO);
b2Vec2 direction = b2Vec2(touchInWorld.x - ballBodyDef.position.x, touchInWorld.y - ballBodyDef.position.y);
b2Vec2 force = b2Vec2(direction.x, direction.y);
force.Normalize();
ballBody->ApplyLinearImpulse(force, ballBodyDef.position);
The problem is that the ball moves waaaay to fast if the sprite is small (10x10 px).
If the sprite is 50x50, then the speed is smaller and looks okay.
It's driving me crazy because I can't control the speed at all.
And not only that, if I don't put force.Normalize() the the speed is different depending on the direction of touch ...
Everything was working great when I was using just Cocos2d and animations. I tried using Box2d to implement collisions but it seems such a HUGE amount of effort that I am considering doing the physics myself :(
First thing , i am not sure but why are you dividing by PTM_Ratios here, i don think you need that, may be you are handling it some other way.
Second, 10x10 travels faster than 50x50 because you are setting SetMassFromShapes, so assuming you are creating a 50/PTM_RATIOSx50/PTM_RATIO body for 50x50 sprite, the body has greater mass than 10x10 body. so it travels slow.
Third, you are using Distance for Impulse so definatly it you are not going to Normalize you are going to get a Proportional Impulse to your distance
and for the Hackish Solution , if nothing else works for you just Decrease the PTM_RATIO!