- Home /
Reducing the distance between 2 objects in a hinge
I currently have this code:
#pragma strict var lockPos = 0;
function Start () { }
function Update() {
transform.rotation = Quaternion.Euler(lockPos, transform.rotation.eulerAngles.y, lockPos); } function OnCollisionEnter(collision: Collision) {
if (collision.rigidbody) {
var fixedJoint: FixedJoint = gameObject.AddComponent(FixedJoint);
fixedJoint.connectedBody = collision.rigidbody;
fixedJoint.breakForce = 1000; fixedJoint. fixedJoint.breakTorque = 1000; } } It works fine, no problems. But the distance between the 2 objects is a noticeable difference. I'm curious how/if i can with a fixedHinge can i reduce that distance.
Basically my object is coming from a launcher and i want it to stick to the object in place, the light grey are the ones that are being launched, the dark grey is the one that i want to be the base.
My question is: A. Can the distance shown between be made to be connecting? B. Is there an easier/more efficient way to do this that allows the same physics?
The above image is from my unity screen, the 2 lighter grey objects ARE connected to the dark grey object with the joint.
Thank you, used this forum a lot for other problems I've had and hope that you guys could give me a hand here!
Answer by Matt-Downey · Aug 07, 2012 at 05:26 AM
I think you can add the following to OnCollisionEnter:
var temp0 : Vector3;
var temp1 : Vector3;
var temp2 : Vector3;
temp0 = collision.transform.position;
temp1 = transform.position;
//Step1
temp2 = temp1 - temp0;
//Step2
temp2.Normalize();
//Step3
temp2 *= radius + radius; //or radius1 + radius2 //addition is faster than multiplication
//not that it matters much for something so simple;
//Step4
temp2 += temp0;
transform.position = temp2;
Basically,
1) take the position of the white ball and find the direction compared to the gray ball.
2) make the distance of this direction 1 meter long (if it's 10 meters away the distance will be one meter compared to the origin (0,0,0), perhaps something like (.707,0,.707) or (1,0,0).
3) multiply by the combined radii (if one ball were bigger with a radius or 2.5 and the others had a radius of .5 for instance, you would multiply by 3...
4) add this relative position to the actual position of the gray orb
For step 4: the position of an object with respect to world space is the position relative to another object plus the distance of that other object with respect to world space.
Edit: Attempting to incorporate this into my code, i did the radius's of both objects inside my code. Each object has the same radius of .5001, and its returning an error about the expression of "Normalize", "Expression in statements must only be executed for their side effects"
Appreciate the quick response, just doing my research it's saying that the temp2.normalize is returning the error due to it not doing anything.
Last Edit: I figured out that if you slow down the speed (was using a fast speed for physics purposes, ill just reduce the drag) it fits in nice and snug. I'm assu$$anonymous$$g the way that they do the collision detection in Unity is based on a timed calculation in set intervals and the quickness of the ball I was using registered being in close enough to "collide" but wasnt touching.
Thank you.
sorry about that it's a simple mistake: replace "Normalize" with "Normalize()"
anything with a capital letter is usually a function, and all functions require parameters--the set of parenthesis ()
an example:
Vector3.normalized: this will take a Vector3(1,1,0) and return Vector3(0.707,0.707,0) without changing the original variable.
Vector3.Normalize() on the other hand won't return any value for you to use, but it will change the original variable to (0.707,0.707,0)...
Hence when it says "Expression in statements must only be executed for their side effects" it means: without telling the computer the inputs to the function, the computer can't make calculations.
Think of the function $$anonymous$$athf.$$anonymous$$ax(one : float, two : float), the function won't know what to do if you just say "$$anonymous$$athf.$$anonymous$$ax", you need to say "$$anonymous$$athf.$$anonymous$$ax(1,2)" and it will return "2".
It seems like common sense that if there are no inputs, the computer will know what to do, but that's not the case, since computers take everything literally.
A side note:
For any rigidbody that is moving, there are three degrees of accuracy for collisions: discrete (the lowest level: quickly moving objects can pass through each other),dynamic (mid lvl: good for moving objects that collide with non-moving objects), and continuous dynamic (high level: intended for objects colliding with other moving objects).
Discrete is very cheap to run, and works in a lot of cases, generally if you attach a rigidbody to an wall or a table, you would assign this.
Continuous is usually used for players that are moving against a static scenery like a player walking through a town.
The last case--continuous dynamic--is used for things like billiard balls hitting each other on a pool table and is the most expensive for the physics engine to calculate.
Note: you can change these within the Rigidbody component assigned to any object
Your answer
Follow this Question
Related Questions
Falling object, change its angle 0 Answers
Why are my hinge joints inaccurate? 1 Answer
spring joint minimum distance 1 Answer