Jump to content
  • Advertisement
Sign in to follow this  
beginnerfurqan

return object

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

float d = +2.0f, e = -10.0f, f; .... void myIdle() { ..... e += 0.2; } Using the above code I have made an object move upwards. How do I make the object go back down the way it came up(i dont mean wrapping)? I have tried the following but doesnt work. if (e >= 10.0f) { e = 10.0f; e -= 2.0; } Can anyone help?

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
I usually have a bool to where it is going. For example:

//Assuming this is C++

bool up = true;

if (up)
e += 0.2f;
if (!up)
e -= 0.2f;

if (e > MAXHEIGHT)
up = false;
if (e < MAXHEIGHT)
up = true;


I've seen people do it with velocities but for some reason I never got them...

C++

Share this post


Link to post
Share on other sites
EDIT: The poster above just got in while I was writing, the same code though

The if statement block code won't work because e -= 2 will only happen when e>=10, which i guess isn't what you want it to do.

For simplicity, you could do this

Set up a global bool called movingUp and set it to true.

Write a an if else to test to see your object is at the bottom or top of the screen

if(e >= 10) //at the top
movingUp = FALSE
if(e <= 0) //at the bottom
movingUp = TRUE

Then write an if else to move your object such as:

if(movingup)
e += 0.2;
else
e -=2;

Share this post


Link to post
Share on other sites
float e = 0.0f; // The body
float velocity = 4f; // The speed of the body.

//...
void myIdle()
{
// Move the body
e += velocity;

// Add resistance
velocity -= 0.1f;

// Bounce off the ground
if(e < 0f)
e *= -1f;
}


Implementing the bodies velocity will greatly make your life easier. If you run the above example, you'll see that e is thrown up in the air, and then peaks and slowly comes back down, much like gravity would act on a flying ball.

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!