Question

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?

Was it helpful?

Solution

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!

OTHER TIPS

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.

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.

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.

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).

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.

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.

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top