How to measure the force by which Two rigid Bodies collide? - unity3d

How to measure the force by which Two rigid Bodies collide and in the reaction add force to other object in opposite direction. For example in the cricket game how the force is measured b/w bat and ball . If player blocks it add a min force to the ball and if he plays the shot he ball goes too far for a four or a six. I will say that just simple addforce() component will not work. It will just apply the force every time when it hits the bat doesn't matter it is blocked or a shot played.

Related

Unity2D disable player collision with ennemy when taking damage

My player has multiple hearts, and i want to damage him one heart for every collision with an ennemy, but after each collision i want him to be invulnerable for a certain amout of time, and making him able to pass through the ennemy, thought about disabling his BoxCollider2D for a moment but that makes him fall through the ground as i'm using a Rigibody2D, how can i achieve that ?
There is two ways to do this:
1-https://docs.unity3d.com/ScriptReference/Physics.IgnoreCollision.html
2-https://docs.unity3d.com/Manual/LayerBasedCollision.html (You will change layers for a short period of time)

How to track multiple collisions between 2 sprites?

Here's an example, just for ease of understanding:
Sprite A is a ball. Sprite B is a cube. When the ball touches the cube once, I want the label to say first contact. When the same ball touches the same cube again, I want the label to say second contact. When the same ball , touches the same cube again, I want the label to say third contact.
How do I accomplish this?
It seems Sprite kit only allows 1 hit collisions between sprites to be programmed, which is enormously limited, in my opinion. Especially, since I want to create an enemy that actually changes behaviour dependant on the amount of hits it receives from the player, not just the same action for every collision.
Figured out a way. If you assign sprite A with points and use a cumulative score to keep track of the points you can then then use "score" as a collision counter. So each time sprite A touched Sprite B the score would increment by 10 points, simply then saying if score == 100 for example ... program something or if score == 150 ... program something (did begin method) and it worked, allowing you to program different actions or whatever at each point of collision with the same sprites! Awesome. Didn't think it would work but it did :)

SKPhysics in Swift - Lowering Speed Property Jittery

Usually in 2D physics engines like JBox2D, if the user wanted to make a simulation run in "slow motion", you would just decrease the value by which the simulated world iterates by, for example normal movement at 60fps, the step would be 1/60 if the physics world is stepped forward every frame.
But for the SKPhysics class in the sprite kit in xcode 6 using swift, I only see the property called ".speed" which when I decreased, only led to a jittery mess, that updated the physics bodies once every 10 frames.
Is there a specific property, or trick maybe to get the SKPhysics world to iterate forward in time by smaller increments without gross glitchy movements?
The only way to slow down the simulation in sprite kit is to use the physicsWorld.speed element.
But if you are trying to change only one or two sprite speeds, try the velocity element of the sprite:
node.physicsBody.velocity = x
you can directly change the velocity(speed) of your node or sprite through its physicsBody.
Hope it helps :)

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.

Slope limit on player movement

I use Unity3D and I have a player with a rigidbody. I add force tto the body for moving the player. My player walks over a terrain but is able to walk up mountains that are to steep to climb. I want to limit the player so it cannot walk up a slope that is to steep.
I know there is a CharacterController component that has this functionality, but I have to use the rigidbody, so I want the same but on my rigidbody.
I can get the normal of the triangle I am standing on, and calculate its angle, but I cannot seem to make the player stop moving up the slope. Only make the player stop moving (which makes the player unmovable once it hits a angled slope)
Any ideas how to solve this problem?
It's difficult to answer without more details on how you're using the physics engine. How/Are you using friction? What angle are you applying the force? Is it always horizontal or at the angle of the floor? Does the player have a mass?
Anyway I can think of a few ways to solve this
Go the pure physics route. Using player mass, friction, force angle, gravity, etc. Get the physics to handle these situation for you. This may take a fair amount of time and programming.
Keep the rigid body but fake the forces. Scale the force you are applying to the body of the player with the angle of the triangle the player is on. You can either use trigonometry to work out what you should apply or your own mapping. By your own mapping I mean set an angle where 0 force is applied (say 45 degree) and do a linear(or non linear) scale on the force applied so on flat ground force is 1 and at 45 force is 0.
Don't use rigid bodies. There is a reason most games don't use rigid bodies to control characters. It's hard and complicated and most of the time not worth the time it would take. Of course I don't know the details of your project so if this isn't an option, fine.
Hope that gives you some things to think about.