Orbit Collision Angle - c#

I am working on a game, and I currently have objects continuously orbiting around a sphere at a fixed distance. I need to allow the object to bounce off of each other. Does anyone know how I can go about doing this?
I have the collision detection working, and each object has a bounding sphere. I am able to get the point of collision, I just need to take the current rotation vector from each object and get the resulting "bounce" angle (vector to rotate around) and have each object continue orbiting around it's new vector.
Let me know if that doesn't make sense or if you need anything else! I should mention that this is done using Unity3D (I am not using rigidbodies, or the built in physics engine for performance reasons)
Edit:
Here is what I've tried:
public void OnTriggerEnter(Collider collider)
{
// Determine resultant rotation axis
Vector3 collisionNormal = collider.ClosestPointOnBounds(thisTransform.position);
rotationAxis = Vector3.Reflect(rotationAxis, collisionNormal);
}
Here is a link to the Vector3.Reflect() method in the Unity3D docs: Vector3.Reflect()
At this point the objects don't start moving in a new direction they collide and then don't bounce off. They just appear to stop when the collision occurs.

Related

How do I lock the rotation of a grabbed object with Unity Oculus VR

For an Oculus Quest game i'm working on, I need to be able to grab an object and not rotate it in any way. I should be able to move it in the x, y and z axes though. I'm doing this in a climbing game and the object is quite big. My player is locked on (0,0,0) and you climb by grabbing the terrain and moving it, giving the illusion that you are climbing.
I am using Unity's Oculus integration asset and I have the OVR Grabbable script on the object I want to be able to grab.
How do I make sure that the object I'm grabbing, doesn't rotate at all?
I've tried using a rigidbody and locking the rotation of the wall I want to climb like that, but that doesn't work. Once I grab it, I can still rotate the object.
I have also tried locking the rotation of the hand rigidbody, but that setting seemed to be ignored, because I could still rotate the hands.
I've also tried adding a bit of code in the script, which would reset the objects rotation in the fixed update. I put this code in the OVR Grabbable script.
void FixedUpdate()
{
transform.rotation = Quaternion.identity;
}
Using this code didn't keep the wall from rotating, but it did snap back to rotation (0,0,0) every frame. THis caused the wall to function as if it would still rotate, but it looked like it was switching between (0,0,0) and the rotation it would be at in every frame. This is of course also not the desired result.
I am not using VRTK, because that does not work with the type of climbing I'm trying to achieve.
I would like to be able to grab an object, move it in the x, y and z axes, while it doesn't rotate at all. Currently, I can still rotate the object. How would I fix this issue and completely lock the rotation whenever I grab the object?
If the object has become a child of the hand and you still want it to move but not rotate. You could add a simple script which scores its default rotation and applies it in LateUpdate.
This is designed for non-physics objects so be sure to remove your test where you added the rigidbody to the wall.
Something simple like this would do the job.
Quaternion defaultRotation;
void Awake()
{
defaultRotation = transform.rotation;
}
void LateUpdate()
{
transform.rotation = defaultRotation;
}

Position and rotation of point in relation to two other points

To give you the setting: I'm making a Vive game in zero-g, where the player moves by grabbing handles and propelling themselves.
What I'd like is for the player to be able to rotate themselves, by grabbing a handle with both hands. Imagine how you'd move in zero-g if you held on to a bar with both hands.
To illustrate:
On the left hand side the player has grabbed a handlebar with both hands. Left arm extended, right arm bent.
In the right hand side picture the player has now extended their right arm, which has rotated the player around the bar.
I guess it's easier to see it as if the player would be moving the entire world, when they do this.
My question is: How can I do this in Unity in 3 dimensions, either through math of Unity-trickery? It needs to roll, yaw and position the player relative to the hands.
Thank you!
Record the average of the three vectors. Then in the next frame, get the difference from the previous average. The difference can be used as euler angles to apply constant force to a rigidbody (or rotate an object by that amount, or other possibilities, depending on your goals).
https://docs.unity3d.com/ScriptReference/Transform.Rotate.html
https://docs.unity3d.com/ScriptReference/Rigidbody.AddForce.html
Vector3 previousCenterPoint;
void Update() {
Vector3 newCenterPoint = (leftHand.transform.position + rightHand.transform.position + player.transform.position) / 3f;
if (previousCenterPoint != null)
{
Vector3 someEulerAngles = newCenterPoint - previousCenterPoint;
someRigidBody.AddForce(someEulerAngles, ForceMode.VelocityChange);
}
previousCenterPoint = newCenterPoint;
}

Player camera targeting/locking another object

I am developing a game that uses a third person camera view, character centralized, no issues with that. But I can't find a way to "lock" on another object while keeping my character in view (Rocket League ball cam: https://youtu.be/FDcO04gXihM ).
I am aware of lookAt() method but it's not enough on its own. I still need to calculate the position of the camera to avoid staying between the player and the target. The problem: http://imgur.com/a/MdO9m
This is what I'm currently doing to move the camera (the "free cam"):
if (freeCam) {
transform.position = Vector3.Lerp(transform.position, camTarget.transform.position, speed);
transform.rotation = Quaternion.Lerp(transform.rotation, camTarget.transform.rotation, speed);
} else {
// Lock cam
}
The camTarget is a game object parented to the player.
I now understand what your problem is and try to explain the solution.
Let's say you have the player object as PO and the target object TO and the camera object CO.
So when you want to achieve, to follow with the camera view always the target object and keep the player object in between, all you have to do ->
The CO needs to travel around the PO in an orbit (orbital camera) with a fixed distance.
Next create a vector from the point TO to PO (the center of the orbit of CO)
Then calculate the 2 hit points of the vector (TO|PO) with the orbit. You now have an enter hit point ENP and an exit hit point EXP.
As of vectors have always a direction the EXP will always be the further point if you keep the direction from TO to PO.
Set the CO position to EXP and lookat at TO
Apply an fixed offset to EXP corresponding to the world.upvector to always sit above the PO.
[optional] If surface exists, do a collision detection with EXP against the surface. If hit, set EXP to collision detection point, and set PO render to false. As soon EXP is no longer colliding with surface set PO render to true.
[optional] If want to achieve to always have a good view rather then always a good look at PO, take the angle of the forward vector of CO. If it is quarter I of IV (position of a angle in math) you can assume, that ur currently just seeing the PO and not TO because the camera sits right beyond PO and looks to TO right through PO. If this happens set PO render to false. As soon as CO forward vector angle is quarter II-IV set PO render to true.
That's it. I will try to give u more in depth code tonight, because right now I'm at work.
Maybe you could check if the camera's position is between the player and the target and just rotate it 180ยบ

How to determine if an object is between two other objects in XNA

I have a screen filled with circles. One circle is the player, one is the enemy, and the others are obstacles. I want the enemy to be able to calculate if there is an obstacle in the path of a straight line from the player to the enemy, so it can adjust accordingly (it is taking place in space, so it is a straight line for jumping from asteroid to asteroid).
Right now, I just turn a random direction when the AI is stuck on an asteroid.
A simple solution could be find the Vector2 between player and enemy (using Vector2.Subtract), and then divide it into smaller vectors or reduce it to a delta that you choose. This can be done normalizing the distance vector and multipling it by a fixed number.
Then simply add that delta to the enemy position and check if the new position collides with an objects (maybe using Rectangle.Contains method), and do this until you reach the player.

Unity C# raycasting from center of an object to find mesh intersection

I'm trying to make a respawn system for a game in unity that starts the character back on the last platform they were on.
As it is currently, it keeps track of which platform they were last grounded on with onCollisionEnter and detects if onCollisionExit touches an out of bounds area.
I need to find the position of a face on the mesh with the y axis (assuming the best way is to do a raycast on the global y axis from the center of the platform) and add the height of the character/2 to determine where to respawn the character.
I'm very new to unity and c#, so I've never done a raycast before and I'm not sure if it's possible to raycast from inside an object to find it's mesh in a given direction, or if there is a better/more efficient way.
Thanks in advance :)
"if it's possible to raycast from inside an object to find it's mesh in a given direction"
You can place a empty gameobject at the center of your mesh (make it child of your mesh ) then pass the position of this empty gameobject to raycast origin.
I usually make re-spawn system with triggers. If you explain little bit more what actually you want to do. I'll try to guide you in that particular direction.

Categories

Resources