Jump to content
  • Advertisement
Sign in to follow this  
deltaKshatriya

Unity Unity3d Number Overflows

This topic is 2603 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.


Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!