# 3D Movement

This topic is 2673 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi All,
I would like for every frame to let a grid of cubes [n][n] to be displaced in the Z Plane a specific distance, and after some delay, they should go back to their initial position, I've tried to the following code but it didn't work
 while (1) { cube[0][5]._pos.y+=1.20f; if(cube[0][1]._pos.y> 20.0f && bstop!=true) { cube[0][1]._pos.y=0; bstop=true; } cube[0][5]._pos.y-=1.20f; if(cube[0][1]._pos.y<-20.0f) cube[0][1]._pos.y=0; } 

##### Share on other sites

Hi All,
I would like for every frame to let a grid of cubes [n][n] to be displaced in the Z Plane a specific distance, and after some delay, they should go back to their initial position, I've tried to the following code but it didn't work
 while (1) { cube[0][5]._pos.y+=1.20f; if(cube[0][1]._pos.y> 20.0f && bstop!=true) { cube[0][1]._pos.y=0; bstop=true; } cube[0][5]._pos.y-=1.20f; if(cube[0][1]._pos.y<-20.0f) cube[0][1]._pos.y=0; } 

I'm going to clear the problem more. I would like to let an object move a pre-specified distance, after that it should goes to its initial position, that should occur per every frame.

##### Share on other sites
So essentially your bit of code is an infinite loop of going 1.20 up and right back down again, effectively changing nothing but doing that for all eternity. On top of that even the magic number indices don't make any apparent sense.

Your problem description is "didn't work" and the code (without more code for context) looks like "doesn't know what he's doing". I'd be surprised if anyone was able to help with that except by ignoring most of it and just posting a premade solution.

##### Share on other sites
@Trienco,

Thanks for your reply, Actually I don't understand how to Implement that type of movement, that's why my code is rubbish.I just would like to have an object that moves in Z Position for a distance, then after achieving that, it should goes back to its initial position. I really couldn't interpret my logic into codes.

##### Share on other sites
Okay, maybe I'm mistaken but what are you exactly hoping to achieve with that?
Why are you both adding and subtracting the same quantity to the same variable each frame? That way that particular cell will always contain the same value at the end of the loop.
If you didn't mean to post both increment and decrement and you just wanted to point out what you wanted to achieve, then it should work just fine on the y axis. Why are you using different indexes?
5 for the increment and 1 for setting the position back to 0?
In other words, that's basically what Trienco also said. It is just not clear why you're doing this.
Why don't you keep either the increment or the decrement and also explain why you're using two different indexes for the array... I don't seem to grasp that

##### Share on other sites
Are you trying to make a door of sorts?

What your doing looks somewhat correct. This might possibly work:

bool movingUp = true; // flag to show if we're moving up or down float moveSpeed = 1.2f; float maxY = 20.0f; // Max value y can be float minY = -20.0f; // min value y can be while (1) { if(movingUp) { cube[0][5]._pos.y += 1.20f; // Chek we've not gone too far and swap direction if(cube[0][5]._pos.y >= maxY) { cube[0][5]._pos.y = maxY movingUp = false; } } else { cube[0][5]._pos.y -= 1.20f; // Chek we've not gone too far and swap direction if(cube[0][5]._pos.y <= minY) { cube[0][5]._pos.y = minY movingUp = true; } } }

I don't really know what cube[0][5] is so you could be more desriptive with your indices (magic numbers that no one knows what they do), define them with a descriptive name. That code will likely go very fast, possibly making the cube look like its flickering. You could use some kind of time variable and move it based on that instead.

##### Share on other sites
Thanks All.
I've managed to solve it.

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 10
• 11
• 13
• 9
• 11
• ### Forum Statistics

• Total Topics
634092
• Total Posts
3015448
×