Jump to content

  • Log In with Google      Sign In   
  • Create Account


dribble04

Member Since 06 Oct 2012
Offline Last Active Apr 08 2014 05:47 AM
-----

Topics I've Started

Handling time in a physics engine?

23 March 2014 - 11:56 PM

When building a physics engine, how do we handle simulation time, how can it be controlled from system to system (different hardware), how is it maintained? Can you also explain to me the most efficient way to handle frame data, for backtracking and so on. 

 

Thanks in advance.

 

Cheers!!


Building a Game Engine

02 December 2013 - 04:16 AM

Hey,

 

Am planning on trying to build a game engine. Its gonna be an engine that would be refined or built specifically for fighters, and beat em ups. Anyway, I was kinda wondering what are the tools or libraries I may need in conjunction with OpenGL, GLSL, and C++.

 

 - A simpler math library, support for vectors, quaternions, and so on. I have been using GLM, but I'd like to just have header file.

 - A sound library, I want to be able to possibly use the pitch and tone, etc to manipulate colors, and lighting.

 - A model loader of sorts, support for .obj, .max, etc..

 - A GUI library.

 

Let me know if you know of any that meet the above criteria, and if you think of any other libs or tools that could come in handy please let me know.

 

Cheers.

 


Movement up and down a slope

06 September 2013 - 12:10 AM

Hey, am trying to move an object along a series of slopes, and also make it follow a trajectory when it reaches a certain velocity at the apex of a slope. Am having a few issues with my code. Am coding in C# Unity, so am using Unity's rigid body and collision system, and varying properties as and when required. I would really appreciate it someone could just check and see if there is anything wrong with it.

 

When I accelerate up a slope, it accelerates but on reaching the top or the apex of the slope it starts to move back down. And when I accelerate down a slope it moves very slowly or doesn't move at all (it should be moving at a greater acceleration). 

 

What is the appropriate condition or process to use to synch all the movement together (such that it is smooth and seamless). 

	public void Accelerate(Vector3 slopeNormal)
	{
		if(MoveDirection.magnitude > 0f)
		{
			Vector3 targetVelocity ;
	        Vector3 currentVelocity ;
	        Vector3 velocityChange ;
			Vector3 force ;
			Vector3 a ;
			
			float levelStopTime ;
			float brakingDistance ;
						
			targetVelocity = Speed * MoveDirection ;
			currentVelocity = Character.rigidbody.velocity;	
			
			velocityChange = targetVelocity - currentVelocity;	
			
			a.x = Mathf.Clamp(velocityChange.x, -MaxAcceleration, MaxAcceleration);
			a.y = Mathf.Clamp(velocityChange.y, -MaxAcceleration, MaxAcceleration);
			a.z = Mathf.Clamp(velocityChange.z, -MaxAcceleration, MaxAcceleration);
			Acceleration = a.magnitude * Mathf.Pow (Time.deltaTime, 2f);	// Not sure, CHECK!!
			
			force = Character.rigidbody.mass * a ;

//			levelStopTime = (m_character.rigidbody.velocity / m_maxAcceleration).magnitude ;
//			brakingDistance = (m_character.rigidbody.velocity.magnitude * m_levelStopTime) + (m_maxAcceleration * m_levelStopTime) ;//Mathf.Pow(m_levelStopTime, 2f) / 2f) ;					
			
			if(Input.GetKey(KeyCode.U))
				AccelerateUpSlope(slopeNormal) ;
			else if(Input.GetKey(KeyCode.D))
				AccelerateDownSlope(slopeNormal) ;
			else
				Character.rigidbody.AddForce(force) ;
		}
		else
			Debug.LogError("Movement Direction Invalid!!") ;
	}
	
	public void AccelerateDownSlope(Vector3 slopeNormal)
	{
		float fG, fNet, fParallel ;
		float m ;
		float g ;
		float theta ;
		
		float a ;
		Vector3 A ;

		Vector3 characterNormal = Character.rigidbody.velocity.normalized ;
		
		m = Character.rigidbody.mass ;
		g = Physics.gravity.y ;

		fG = m * g;
		theta = Vector3.Angle(characterNormal, slopeNormal) ;
		
		fParallel = fG * Mathf.Sin(theta) ;
		fNet = fParallel ;
		
		a = fNet / m ;
		A = a * new Vector3(1f, 1f, 0f) ;
		Debug.Log("Down " + A) ;
		
		Acceleration = a  * Mathf.Pow (Time.deltaTime, 2f);	// Not sure, CHECK!!
		Character.rigidbody.AddForce(A, ForceMode.Acceleration) ;
	}
	
	public void AccelerateUpSlope(Vector3 slopeNormal)
	{
		float fG, fNet, fParallel, fPerpendicular ;
		float m ;
		float g ;
		float theta ;
		
		float a ;
		Vector3 A ;

		Vector3 characterNormal = Character.rigidbody.velocity.normalized ;
		
		m = Character.rigidbody.mass ;
		g = Physics.gravity.y ;

		fG = m * g;
		theta = Vector3.Angle(characterNormal, slopeNormal) ;
		
		Debug.Log(theta) ;		
		
		fParallel = fG * Mathf.Sin(theta) ;
		fPerpendicular = CoefficentFriction * fG * Mathf.Cos(theta) ; 
		
		fNet = fParallel - fPerpendicular ;
		
		a = fNet / m ;
		A = a * new Vector3(1f, 1f, 0f) ;
		Debug.Log("UP " + A) ;		
		
		Acceleration = a  * Mathf.Pow (Time.deltaTime, 2f);	// Not sure, CHECK!!
		Character.rigidbody.AddForce(A, ForceMode.Acceleration) ;
	}	
		
	public void ProjectileMotion(Vector3 colliderNormal)
	{
		float maxVelocityX = 45f ;
		float maxVelocityY = 60f ;
		Vector3 initialVelocity = go.rigidbody.velocity ;
		Vector3 initialPosition = go.rigidbody.position ;
		
//		float angle = Vector3.Angle(go.rigidbody.position.normalized, colliderNormal) ;
//		float launchAngleRad = (angle * Mathf.PI) / 180f ;

		float launchAngleRad = (m_launchAngle * Mathf.PI) / 180f ;
		float timeOfFlight = (2f * initialVelocity.magnitude * Mathf.Sin(launchAngleRad)) / Physics.gravity.magnitude ;
		
//		Debug.Log("Launch Angle (rad) - " + launchAngleRad) ;
//		Debug.Log("Time of Flight - " + timeOfFlight) ;
		
		float x, y ;		
		
		x = initialPosition.x + (initialVelocity.x * Mathf.Cos(launchAngleRad) * timeOfFlight) ;
		y = initialPosition.y + (initialVelocity.y * Mathf.Sin(launchAngleRad) * timeOfFlight) - (0.5f * Physics.gravity.y * Mathf.Pow(timeOfFlight, 2)) ;
		
//		Debug.Log("x Position - " + x) ;
//		Debug.Log("y Position - " + y) ;			
		
		Vector3 amtToMove = new Vector3(x, y, go.transform.position.z) ;
		Vector3 finalVelocity = amtToMove / timeOfFlight ;

//		go.rigidbody.MovePosition(amtToMove) ;
		
		if(finalVelocity.x >= 0f)
		{
//			Debug.Log(finalVelocity) ;
			
			finalVelocity.x = Mathf.Clamp(finalVelocity.x, -maxVelocityX, maxVelocityX);
			finalVelocity.y = Mathf.Clamp(finalVelocity.y, -maxVelocityY, maxVelocityY);			
			
			go.rigidbody.AddForce(finalVelocity, ForceMode.VelocityChange) ;	// ForceMode.Acceleration) ;
		}
	}

Also would this be the best way to check if the object is moving on a slope.

	public void CollisionCheck(GameObject go)
	{
		Ray ray ;
		RaycastHit hit ;
		float x, y ;
//		float offset = 10f ;
		float playerGroundSpace = 0.0005f ;	
		
		CapsuleCollider collider = go.GetComponent<CapsuleCollider>() ;
		
		// Checking for collision up and down																
		x = go.transform.position.x - go.transform.localScale.x / 2f ; 
		y = go.transform.position.y + go.transform.localScale.y / 2f ;
		
		ray = new Ray(new Vector2(x, y), new Vector2(0f, 10f)) ;
		
		Debug.DrawRay(ray.origin, ray.direction);
			
		if (Physics.Raycast(ray, out hit))
		{
			float dst = Vector3.Distance(ray.origin, hit.point) ;

			if(dst > playerGroundSpace)
			{
				Debug.Log("Collision, UP/DOWN") ;
			}
		}
		
		// Checking for collision left and right		
		x = go.transform.position.x + go.transform.localScale.x / 2f ;
		y = go.transform.position.y - go.transform.localScale.y / 2f ;
		
		ray = new Ray(new Vector2(x, y), new Vector2(10, 0)) ;
		
		Debug.DrawRay(ray.origin, ray.direction);
		
		if (Physics.Raycast(ray, out hit))
		{
			float dst = Vector3.Distance(ray.origin, hit.point) ;
		
			if(dst > playerGroundSpace)
			{
				Debug.Log("Collision, LEFT/RIGHT") ;
			}		
		}
	}

Projectile Motion

23 August 2013 - 12:10 AM

Hey,

 

Can somebody please tell me how I can simulate a trajectory or projectile motion. I am building a game where a character is moving and as it gains momentum and moves up a slope, I want the character to launch it self.

 

Any help would be greatly appreciated.

 

Thank you.

 

Cheers!!


Dynamically Subdivide a Cubic Bezier Curve

06 August 2013 - 12:43 AM

Hi,

 

Am trying to build a class in unity to render a cubic bezier curve. I want to be able to add more curves and subdivide a given curve on the fly.

 

I have tried using the "Vectrosity" plugin but haven't been able to figure out how to dynamically subdivide a given curve.

 

If any body has any ideas, help please. :)

 

Thank you for any input.

 

Regards,

Andre.


PARTNERS