Sign in to follow this  

animate( )...not working?

This topic is 4271 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

hey, i'm making a simple physics engine and stuff, and when i try to make an animate function, for some reason it doesn't animate. here's the code:
CVector position1 = CVector(-5,-5,0);
CVector velocity1 = CVector(10,10,0);
CVector acceleration1 = CVector(0,-9.8,0);
void animate(float deltaTime, CVector position,CVector velocity,CVector acceleration)
{
	position += velocity * deltaTime;
	velocity += acceleration * deltaTime;
}
and in my loop, here's how i call it:
animate(newDeltaTime,position1,velocity1,acceleration1);
and that's it. when i have just deltaTime as an argument it works, but i want this to work for whatever position i want, not just for one vector. any reason to see why it's not working? thanks --nathan

Share this post


Link to post
Share on other sites
It doesn't work because you're passing in the parameters by value, and so the function uses local copies of the parameters and never changes the originals. To fix this, you can either use pointers or references, like so:

With pointers:

void animate(float deltaTime, CVector *position,CVector *velocity,CVector acceleration)
{
(*position) += (*velocity) * deltaTime;
(*velocity) += acceleration * deltaTime;
}

//call it like this
animate(newDeltaTime,&position1,&velocity1,acceleration1);



With references:

void animate(float deltaTime, CVector &position,CVector &velocity,CVector acceleration)
{
position += velocity * deltaTime;
velocity += acceleration * deltaTime;
}

//call it like this
animate(newDeltaTime,position1,velocity1,acceleration1);



Notice the difference in semantics here. The references version only requires you to change the function's parameters, and avoids the nasty situation where a potentially null pointer gets passed in.

Of course, this is all assuming that you're using C++ and not C# or Java (which both would have worked properly with the original code anyway).

-Auron

Share this post


Link to post
Share on other sites

This topic is 4271 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this