views:

441

answers:

10

Hi, I'm currently designing a program that will involve some physics (nothing too fancy, a few balls crashing to each other)

What's the most exact datatype I can use to represent position (without a feeling of discrete jumps) in c#?

Also, what's the smallest ammount of time I can get between t and t+1? One tick?

EDIT: Clarifying: What is the smallest unit of time in C#? [TimeSpan].Tick?

A: 

I'm not sure I understand your last question, could you please clarify?

Edit:

I might still not understand, but you can use any type you want (for example, doubles) to represent time (if what you actually want is to represent the discretization of time for your physics problem, in which case the tick is irrelevant). For most physics problems, doubles would be sufficient.

The tick is the best precision you can achieve when measuring time with your machine.

OysterD
+5  A: 

In .Net a decimal will be the most precise datatype that you could use for position. I would just write a class for the position:

public class Position
{
    decimal x;
    decimal y;
    decimal z;
}

As for time, your processor can't give you anything smaller than one tick.

Sounds like an fun project! Good luck!

tghw
-1: Decimals are rarely used in any 3D game calculations, they are just too slow. Use a Vector class built with 3 floats and you're all set.
Jon Tackabury
+1: For real types decimal is the most precise and that is what was asked.
Rusty
A: 

I think you should be able to get away with the Decimal data type with no problem. It has the most precision available. However, the double data type should be just fine.

Yes, a tick is the smallest I'm aware of (using the System.Diagnostics.Stopwatch class).

JTA
A: 

For a simulation you're probably better off using a decimal/double (same type as position) for a dimensionless time, then converting it from/to something meaningful on input/output. Otherwise you'll be performing a ton of cast operations when you move things around. You'll get arbitrary precision this way, too, because you can choose the timescale to be as large/small as you want.

dmo
+7  A: 

The Decimal data type although precise might not be the optimum choice depending on what you want to do. Generally Direct3D and GPUs use 32-bit floats, and vectors of 3 (total 96 bits) to represent a position in x,y,z.

This will usually give more than enough precision unless you need to mix both huge scale (planets) and microscopic level (basketballs) in the same "world".

Reasons for not using Decimals could be size (4 x larger), speed (orders of magnitude slower) and no trigonometric functions available (AFAIK).

On Windows, the QueryPerformanceCounter API function will give you the highest resolution clock, and QueryPerformanceFrequency the frequency of the counter. I believe the Stopwatch described in other comments wraps this in a .net class.

Bjorn Reppen
Yeah cuz everyone knows basketballs are microscopic!!!
sep332
+1: A vector built with 3 floats is pretty standard in D3D. I wouldn't use decimals unless you needed higher accuracy, but if it's just a game I wouldn't worry about it.
Jon Tackabury
+3  A: 

I would use a Vector datatype. Just like in Physics, when you want to model an objects movement, you use vectors. Use a Vector2 or Vector3 class out of the XNA framework or roll your own Vector3 struct to represent the position. Vector2 is for 2D and Vector3 is 3D.

TimeSpan struct or the Stopwatch class will be your best options for calculating change in time. If I had to recommend, I would use Stopwatch.

Dale Ragan
A: 

@Tyler, if you want to make your own position class that's fine ... but you probably want to make it a struct just like the Vector3/2 types that XNA comes with. Since you will probably have a large number of positions, you will want to avoid the GC cost of having to deal with all those objects.

Joel Martinez
A: 

@Joel, yes, I'm planning on using a struct instead of a class

Juan Manuel
A: 

Hey Juan, I'd recommend that you use the Vector3 class as suggested by several others since it's easy to use and above all - supports all operations you need (like addition, multiplication, matrix multiply etc...) without the need to implement it yourself. If you have any doubt about how to proceed - inherit it and at later stage you can always change the inner implementation, or disconnect from vector3.

Also, don't use anything less accurate than float - all processors these days runs fast enough to be more accurate than integers (unless it's meant for mobile, but even there...) Using less than float you would lose precision very fast and end up with jumpy rotations and translations, especially if you plan to use more than a single matrix/quaternion multiplication.

Adi
Thanks for the suggestion
Juan Manuel
+4  A: 

Unless you're doing rocket-science, a decimal is WAAAY overkill. And although it might give you more precise positions, it will not necessarily give you more precise (eg) velocities, since it is a fixed-point datatype and therefore is limited to a much smaller range than a float or double.

Use floats, but leave the door open to move up to doubles in case precision turns out to be a problem.