object with constant int/float

I'm using a structure for several objects but I need a float within it to be constant for just one object. (the velocity of the sun in a solar system simulations). Is this possible or do I need a new structure
Can't you just not change the velocity of the sun?
Peter87 is right, don't over complicate things for yourself. It just causes pain.
Well the velocity of the sun is initially set to zero, but this changes when calculating the gravity.I find the the planets drag it slightly. I guess the mass difference in the sun and planets is to low. I am struggling to produce steady orbits is there any way to stabilize it? The earth spinning round the sun OK but it drags the sun slightly and the moon smashes into the earth after 2 orbits.
You can use the specifier constexpr

For example

1
2
3
4
struct A
{
   constexpr static float velocity = 0.0f;
};


EDIT: if your compiler does not support the specifier constexpr you can write the following way

1
2
3
4
5
6
struct A
{
   const static float velocity;
};

const float A::velocity = 0.0f;


Last edited on
Thanks. Looking at that wont it effect every object I create though? I need the planets to have a variable velocity

EDIT:Or do I?
Last edited on
I have not understood what you want. I showed you a static data member that is common for all objects of a given class.
Yeah. I need just one member to have a static data member but cant figure out another way to do it.
Well the velocity of the sun is initially set to zero,...


I find this part to be interesting because it isn't true, our solar system isn't stationary. Maybe your program is fine but your math is flawed. What happens when you take into account the fact that Sol is orbiting the center of the universe galaxy and is being acted upon by a mass far greater than anything else in our system (I would represent the center of the galaxy as a constant rate of acceleration acting on the sun, but that's because I'm lazy).
Last edited on
jamessi88 wrote:
I need a float within it to be constant for just one object.
vlad from moscow wrote:
I have not understood what you want. I showed you a static data member that is common for all objects of a given class.


@OP: Your viewpoint may be centered on the sun, but that doesn't mean the sun doesn't move from its original position. I know, theory of relativity blah blah, but for simulation's sake you can't do what you're trying to do. And how would you expect it to work in writing the code? You can't just say "Oh wait that's the sun? I won't change its velocity then." - that doesn't make sense.
Last edited on
Ok thanks. Is there any way to produce a steady orbit of objects around another object though? I started by two objects of equal mass and they attract according to
Fg = G (m1*m2)/(d^2)



Playing with it I added a object with a very large mass in the center and some of much smaller mass around it with a little starting velocity in the hope of producing some sort of orbit. Is it at all possible to produce a steady orbit in any way?
Last edited on
It is completely possible. An example is our solar system ;)
> I'm using a structure for several objects but I need a float within it to be constant for just one object.
> Is this possible or do I need a new structure

You need a different structure; but it could be a different instantiations of the same template.

1
2
3
4
5
6
7
8
9
10
#include <type_traits>

template < bool CONST = false > struct A
{
    int b = 5 ;
    typename std::conditional< CONST, const int, int >::type c = 9 ;
};

A<> one ; // one.c is not cnst qualified
A<true> two ; // two.c is const qualified 


And then:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
template < bool CONST > void foo( A<CONST>& a )
{
    // ....
}

void bar( A<true>& a )
{
    // ....
}

void bar( A<false>& a )
{
    // ....
}
Computergeek01 wrote:
I find this part to be interesting because it isn't true, our solar system isn't stationary.

It is interesting. If we consider just two bodies in isolation, such as the Earth and the Moon, it is inaccurate to consider that the Moon simply orbits the Earth. What actually happens is the two bodies both orbit around their common centre of gravity, so that neither of them remains stationary.

As more bodies (planets, or the Sun) are added to the system, the same principle applies, but the motion is obviously much more complex.

See also this question in the FAQ here:
"Why does Mercury get thrown out of the Solar System when I turn up the time step?"
http://universesandbox.com/faq/
Last edited on
Topic archived. No new replies allowed.