Unity3d Number Overflows

Started by
7 comments, last by warhound 12 years, 5 months ago
I have a program in Unity3d that is calculating some numbers that tend to be very big. The thing is, that Unity3d is throwing exceptions, and I need to still do operations on these numbers. I've been looking for a work around of some kind, but so far have come up with nothing. Does anyone know a way around them?

No one expects the Spanish Inquisition!

Advertisement
How big is 'very big'? What data type(s) are you using in the code? Can you post some example code that demonstrates the problem?
C# is supported by Unity, isn't it? Do you have access to the Decimal type?
[TheUnbeliever]
Alright, so here's the code as requested



function calcHungerPartial(theTarget : GameObject){
var hungerPart : int;
hungerPart=(2*(calcHunger(theTarget)*calcForceApplied(theTarget)))/calcFuel(theTarget);
var all : GameObject[]=GameObject.Find("Main Camera").GetComponent("all objects").all;
var totals : int=0;
var theFor : Vector3=theTarget.transform.position-transform.position;
theFor=theFor.normalized;
var otherFor : Vector3=-theFor;
for (var i : int=0; i<all.length; i++){
var dir : Vector3=all.transform.position-transform.position;
dir=dir.normalized;
var otherDir : Vector3=all.transform.position-theTarget.transform.position;
otherDir=otherDir.normalized;
if (Vector3.Angle(theFor, dir)<90 && Vector3.Angle(otherFor, otherDir)<90){
totals+=((Vector3.Distance(transform.position, all.transform.position)/GetComponent("monitoring systems").accelRate)*((10*Vector3.Distance(transform.position, all.transform.position)+1)/Mathf.Pow(Vector3.Distance(transform.position, all.transform.position), 3)));
}
}
hungerPart=hungerPart*totals;
return hungerPart;
}


Where the 2nd line of the function has a overflow exception and the rest of the functions are:



function calcHunger(theTarget : GameObject){
var projectedHunger : int=0;
projectedHunger+=(Vector3.Distance(transform.position, theTarget.transform.position)/(GetComponent("monitoring systems").accelRate*2));
var theFor : Vector3=theTarget.transform.position-transform.position;
theFor=theFor.normalized;
var otherFor : Vector3=-theFor;
var all : GameObject[]=GameObject.Find("Main Camera").GetComponent("all objects").all;
for (var i : int=0; i<GameObject.Find("Main Camera").GetComponent("all objects").all.length; i++){
var dir : Vector3=all.transform.position-transform.position;
dir=dir.normalized;
var theAng : float=Vector3.Angle(theFor, dir);
var theDist : float=Vector3.Distance(transform.position, all.transform.position);
var thePerpDist : float=theDist*Mathf.Sin(theAng);
var otherDir : Vector3=all.transform.position-theTarget.transform.position;
otherDir=otherDir.normalized;
if (Vector3.Angle(theFor, dir)<90 && Vector3.Angle(otherFor, otherDir)<90){
projectedHunger+=((1/thePerpDist)+(1/(Mathf.Pow(thePerpDist, 2))))*(4*GetComponent("monitoring systems").accelRate);
}
}
projectedHunger-=theTarget.GetComponent("attributes").foodVal;
projectecHunger=GetComponent("monitoring systems").totalHunger+projectedHunger;
return projectedHunger;
}

function calcFuel(theTarget : GameObject){
var fuel : int=(GetComponent("monitoring systems").fuelCost*2);
var theFor : Vector3=theTarget.transform.position-transform.position;
theFor=theFor.normalized;
var otherFor : Vector3=-theFor;
var all : GameObject[]=GameObject.Find("Main Camera").GetComponent("all objects").all;
for (var i : int=0; i<GameObject.Find("Main Camera").GetComponent("all objects").all.length; i++){
var dir : Vector3=all.transform.position-transform.position;
dir=dir.normalized;
var theAng : float=Vector3.Angle(theFor, dir);
var theDist : float=Vector3.Distance(transform.position, all.transform.position);
var thePerpDist : float=theDist*Mathf.Sin(theAng);
var otherDir : Vector3=all.transform.position-theTarget.transform.position;
otherDir=otherDir.normalized;
if (Vector3.Angle(theFor, dir)<90 && Vector3.Angle(otherFor, otherDir)<90){
fuel+=((1/thePerpDist)+(1/(Mathf.Pow(thePerpDist, 2))))*GetComponent("monitoring systems").fuelCost;
}
}
fuel-=theTarget.GetComponent("attributes").fuel;
fuel=GetComponent("monitoring systems").totalFuel+fuel;
return fuel;
}

function calcForceApplied(theTarget : GameObject){
var all : GameObject[]=GameObject.Find("Main Camera").GetComponent("all objects").all;
var forceApplied : int=0;
var theFor : Vector3=theTarget.transform.position-transform.position;
theFor=theFor.normalized;
var otherFor : Vector3=-theFor;
for (var i : int=0; i<GameObject.Find("Main Camera").GetComponent("all objects").all.length; i++){
var dir : Vector3=all.transform.position-transform.position;
dir=dir.normalized;
var theAng : float=Vector3.Angle(theFor, dir);
var theDist : float=Vector3.Distance(transform.position, all.transform.position);
var thePerpDist : float=theDist*Mathf.Sin(theAng);
var otherDir : Vector3=all.transform.position-theTarget.transform.position;
otherDir=otherDir.normalized;
if (Vector3.Angle(theFor, dir)<90 && Vector3.Angle(otherFor, otherDir)<90){
forceApplied+=((1/thePerpDist)+(1/(Mathf.Pow(thePerpDist, 2))))*(all.rigidbody.velocity.magnitude*all.rigidbody.mass);
}
}
return forceApplied;
}


I'm not sure how big the actual numbers are.....so this is all I've really got to work with.

No one expects the Spanish Inquisition!

And as a reply to the C# question, I'm not really familiar with C#, so I wouldn't know....I've been using JavaScript this entire time....

No one expects the Spanish Inquisition!

This says that Unity's JavaScript is a bit different to standard JavaScript, and I'm not familiar with it at all.

However I suspect that what you want to do is use float instead of int for all the values in that calculation.

You should probably also make sure that the result of calcFuel() isn't 0 before dividing by it.
Right, I'll try and change everything to float, I only changed one value to float, that didn't really help too much, but let's give it another shot. As for calcFuel being zero, that's a good point that I forgot about....I'll check back if I have more problems.

No one expects the Spanish Inquisition!

It seems like your trying to calculate the distance traveled to reduce fuel? A better way is to have some fuel amount and then decrement it based on how far you travel each frame or fixed update. You never want to go below zero so check for that as well.


Thanks for your help guys, the error is fixed, I just had to change everything to a float, and ensure that nothing was divided by zero.

No one expects the Spanish Inquisition!

This topic is closed to new replies.

Advertisement