Jump to content

QUESTION: Reference Frame dev-question (KSP krakensbane approach for high velocities)


Recommended Posts

Hi,

I have a question about the implementation of the reference frame in KSP (the krakensbane approach for high velocities).

There is a blog entry by HarevesteR where my question would have fi to (altough its an older entry), anyway somehow I am not able to add comments there.

http://forum.kerbalspaceprogram.com/entries/12-Krakensbane

So I hope to find an anser here.

I am implementing a space game in Unity3D (just for the fun/hobby, spacegame using "real" distances, procedural planets (still a long road to go) and oculus rift support). I of course meet the same issues like in KSP,

therefor I also implemented a floating origin approach for the camera position to get rid of the floating point precision issue which works fine. After adding a number of asteroids in a believable distance and starting to fly by, I noticed a need another solution for velocities, as the common speeds for the unity physics engine are not enough for universe distances. So I looked at KSP presentation at Unite 2013 trying to understand how they did it.

Basically I think I understood the approach. If the ridigbody where the camera is applied to (so basically the spaceship) reaches a certain speed, the velocity vector3 is copied to a, say, "referenceframe" script (which is similar to the floating origin script) with a separate vector3 velocity value. At the same time the velocity is copied, the velocity of the ridigbody (spaceship) is set to zero again. This separate vector3 value is used to, when other than (0,0,0), reposition all objects around the spaceship (which has now zero velocity and is "not moving") each frame. So similar to floating origin, just for velocities.

This works, however you notice some stuttering when the script moves the objects. So I wonder if this is exactly KSPs approach or if they did something different?? Hopefully anyone knows.

Below is my script. One optimization might be to group the asteroids into one gameobject so that not 400 asteroid gameobjects have to be moved separately by the script, but only one gameobject. Hopefully this increases performance, however any other hint, or how KSP did this, would be very much appreciated!

// ReferenceFrameBylayer.cs
// Written by Joerg Zdarsky
// 23 August 2014
using UnityEngine;
using System.Collections;

/* Reference Frame Script
* Contents
* 1 Description
* 2 Usage
* 3 Notes
* 4 C# - ReferenceFrameByLayer.cs
* Description
* This script translates all objects in the world to keep objects moving around an object that has
* velocity zero. Similar approach to floating origin, just for velocities
* Usage
* Attach this script to the gameobject that moves around 'threshold' determines the distance at which mass object translation occurs,
* this is not a light-weight operation so less frequent movement is better.
* Notes
* Make sure your other camera controls are in Update() and not LateUpdate() otherwise they could clash.
*/
[RequireComponent(typeof(GameObject))]
public class ReferenceFrameByLayer : MonoBehaviour
{
public float threshold = 27777.78f; // =100.000km/h, treshhold is meters per second (as provided by rigidbody.velocity.magnitude)
public Vector3 referenceFrameVelocity = new Vector3(0,0,0);
public string layer;
public GameObject parentGameObject;

void LateUpdate()
{
Vector3 gameObjectVelocity = gameObject.rigidbody.velocity;
float gameObjectMagnitude = gameObjectVelocity.magnitude;
Vector3 gameObjectAbsoluteVelocity = gameObjectVelocity + referenceFrameVelocity;
float gameObjectAbsoluteMagnitude = gameObjectAbsoluteVelocity.magnitude;

// Check if velocity needs to be added to reference framce
if (gameObjectMagnitude > threshold) {
this.referenceFrameVelocity += gameObjectVelocity;
gameObject.rigidbody.velocity = new Vector3(0,0,0);
}

// Check if velocity needs to be returned to the gameobject
else if (gameObjectAbsoluteMagnitude < threshold && referenceFrameVelocity.magnitude >= threshold) {
gameObject.rigidbody.velocity += this.referenceFrameVelocity;
this.referenceFrameVelocity = new Vector3(0,0,0);
}

// Moves objects (not-light-weight)
if (referenceFrameVelocity != new Vector3 (0, 0, 0)) {
Object[] objects = FindObjectsOfType(typeof(Transform));
foreach(Object o in objects)
{
Transform t = (Transform)o;

if (t.gameObject.layer == LayerMask.NameToLayer(layer))
{
t.position -= this.referenceFrameVelocity*Time.deltaTime;
}
}

objects = FindObjectsOfType(typeof(ParticleEmitter));
foreach (Object o in objects)
{
ParticleEmitter pe = (ParticleEmitter)o;
Particle[] emitterParticles = pe.particles;
for(int i = 0; i < emitterParticles.Length; ++i)
{
emitterParticles[i].position -= this.referenceFrameVelocity;
}
pe.particles = emitterParticles;
}
}
}
}

Link to comment
Share on other sites

This thread is quite old. Please consider starting a new thread rather than reviving this one.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...