Skip to content

Instantly share code, notes, and snippets.

@gregkrsak
Last active August 29, 2015 13:56
Show Gist options
  • Save gregkrsak/8810154 to your computer and use it in GitHub Desktop.
Save gregkrsak/8810154 to your computer and use it in GitHub Desktop.
The physics object is the base class of all objects in spherical gravity. The singularity is an "attractor" of objects in spherical gravity, implemented as a trigger.
# GKSphericalGravitySingularity.boo
#
# Copyright (c) 2013 Greg M. Krsak ([email protected])
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.
#
import UnityEngine
#
# Class: GKSphericalGravitySingularity
#
# Provides properties associated with spherical gravity.
#
class GKSphericalGravitySingularity(MonoBehaviour):
# Field: Magnitude
#
public Magnitude = 25000
# Field: Centroid
#
public centroid = Vector3(0, 0, 0)
# Method: Start
#
# Called once.
#
def Start():
self.centroid = self.transform.position
# Method: Update
#
# Called every frame.
#
def Update():
pass
# Method: OnTriggerStay
# Parameters: otherObject as Collision
#
# Called when an object is within the gravity field.
#
def OnTriggerStay(otherObject as Collider):
# Is the object within the trigger an instance of GKSphericalPhysicsObj?
shouldCalculateGravity = otherObject.GetComponent[of GKSphericalPhysicsObj]()
if shouldCalculateGravity:
vectorToCentroid = (self.centroid - otherObject.transform.position)
# Update the object's gravity direction
direction = vectorToCentroid.normalized
otherObject.GetComponent[of GKSphericalPhysicsObj]().gravityDirection = direction
# Update the object's gravity accelleration using the Inverse Square Law
magnitude = self.Magnitude * (1 / (vectorToCentroid.magnitude * vectorToCentroid.magnitude))
accelleration = otherObject.GetComponent[of GKSphericalPhysicsObj]().gravityDirection * magnitude
otherObject.GetComponent[of GKSphericalPhysicsObj]().gravityAccelleration = accelleration
# Update the object's rigidbody velocity
otherObject.rigidbody.velocity += otherObject.GetComponent[of GKSphericalPhysicsObj]().gravityAccelleration * Time.deltaTime
# GKSphericalPhysicsObj.boo
#
# Copyright (c) 2013 Greg M. Krsak ([email protected])
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.
#
import UnityEngine
#
# Class: GKSphericalPhysicsObj
#
# Applices spherical physics properties to an object.
#
class GKSphericalPhysicsObj(MonoBehaviour):
# Field: bounciness
#
# How bouncy is this object? This is a unitless, scalar value.
#
public bounciness = 0.8
# Field: bounceThreshold
#
# How likely to bounce is this object? This is a velocity magnitude
# threshold.
#
public bounceThreshold = 0.2
# Field: gravityDirection
#
# A normalized vector, in the direction of gravity.
#
public gravityDirection = Vector3(0, 0, 0)
# Field: gravityAccelleration
#
# The accelleration of this object due to gravity.
#
public gravityAccelleration = Vector3(0, 0, 0)
# Property: weight (readonly)
#
# Gravity's force on this object.
#
public weight as Vector3:
get:
return self.gravityAccelleration * self.rigidbody.mass
# Property: momentum (readonly)
#
# The object's momentum.
#
public momentum as Vector3:
get:
return self.rigidbody.velocity * self.rigidbody.mass
# Method: Start
#
# Called once.
#
def Start ():
pass
# Method: Update
#
# Called every frame.
#
def Update():
pass
# Method: OnCollisionEnter
# Parameters: otherObject as Collision
# The object that this object is colliding with.
#
# Called on collision.
#
def OnCollisionEnter(otherObject as Collision):
self.bounceAgainst(otherObject)
# Method: bounceAgainst
# Parameters: otherObject as Collision
# The object that this object is bouncing against.
#
# Bounces the object off of another.
#
def bounceAgainst(otherObject as Collision):
# Bounce only if the impact velocity is great enough
if self.rigidbody.velocity.magnitude >= self.bounceThreshold:
contact = otherObject.contacts[0] # TODO: Some sort of smooth/average?
bounceDirection = Vector3.Reflect(self.rigidbody.velocity.normalized, contact.normal.normalized)
bounceMagnitude = self.rigidbody.velocity.magnitude * self.bounciness
bounceVelocity = bounceDirection * bounceMagnitude
self.rigidbody.velocity = bounceVelocity
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment