Unity3d Number Overflows
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?
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?
Alright, so here's the code as requested
Where the 2nd line of the function has a overflow exception and the rest of the functions are:
I'm not sure how big the actual numbers are.....so this is all I've really got to work with.
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.
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....
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.
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.
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.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement