# how to make a 3D Objbect move in circle?

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

## Recommended Posts

How would i make an object rendering move in a cirlcle, right now i have a lazer which is a box moving up and down and when the camera look vector touches it , it speeds up


void Lazer::UpdateLazer(float dt)
{
LazerPos = LazerPos + LazerDir * speed *dt;
D3DXMatrixTranslation(&matTrans, LazerPos.x, LazerPos.y, LazerPos.z);
if (LazerPos.y > 200.0f)
{
LazerDir.y = -1.0f;
}
if (LazerPos.y < 100.0f)
{
LazerDir.y = 1.0f;
}

D3DXMatrixInverse(&ObjMatrix, NULL, &matTrans);
D3DXVec3TransformCoord(&newPos, &gCamera->pos(), &(ObjMatrix));
D3DXVec3TransformNormal(&newDir, &gCamera->look(), &(ObjMatrix));

D3DXIntersect(Box, &newPos, &newDir, &hit, NULL, NULL, NULL, NULL, NULL,NULL);
if (hit)
{
speed += 1.0f;
}
else
{
speed -= 0.001f;
}

}

Edited by Anddos

##### Share on other sites

How would i make an object rendering move in a cirlcle(?)

Use spherical coordinates.

You can convert Spherical coordinates to Cartesian by calculating:

x = radius * sin(phi) * cos(tetha)

y = radius * sin(phi) * sin(tetha)

z = radius * cos(phi)

So you can use a function like this:

Vector3 getPositionInSphere(Vector3 sphere_center, float radius, float theta, float phi)
{
return Vector3(sphere_center.x + radius * sin(tetha) * cos(phi),
sphere_center.y + radius * sin(tetha) * sin(phi),
}

Edited by TiagoCosta

##### Share on other sites

what value do i set for the theta and pi to

Edited by Anddos

##### Share on other sites

Follow this image. p is the radius, ? (theta) azimuthal angle, and ? (phi) polar angle.

So if you want the object to move in a circle on the xy-plane:
Set the ? to ?/2 (pi/2) and update ? each frame:

void Object::UpdateObject(float dt)
{
angle += dt*PI/4; //rotate 45 degrees per seconds

if(angle > 2*PI)
angle -= 2*PI;

ObjectPos = getPositionInSphere(sphere_center, radius, angle ,PI/2);
D3DXMatrixTranslation(&matTrans, ObjectPos.x, ObjectPos.y, ObjectPos.z);

[...]
}



EDIT #1:

BTW, you might have to switch the coordinates in the function getPositionInSphere if you're using a different coordinate system.

For the typical D3D left-hand coordinate system:

Vector3 getPositionInSphereD3D(Vector3 sphere_center, float radius, float theta, float phi)
{
return Vector3(sphere_center.x + radius * sin(phi) * sin(theta),
sphere_center.z + radius * sin(phi) * cos(theta));
}


EDIT #2:

Since you only want to move in a circle (phi is constant PI/2), the function turns into polar coordinates:

Vector3 getPositionInCircleD3D(Vector3 sphere_center, float radius, float theta)
{
return Vector3(sphere_center.x + radius * sin(theta),
sphere_center.y,
}

Edited by TiagoCosta

##### Share on other sites

Thanks for the reply, i am doing this,but the box is just stuck in the same position

D3DXVECTOR3 Lazer::getPositionInSphere(D3DXVECTOR3 sphere_center, float radius, float theta, float phi)
{
return D3DXVECTOR3(sphere_center.x + radius * sin(theta) * sin(phi),
sphere_center.z + radius * sin(theta) * cos(phi));
}

void Lazer::UpdateLazer(float dt)
{
float angle = 0.0f;
angle += dt*D3DX_PI / 4; //rotate 45 degrees per seconds

if (angle > 2 * D3DX_PI)
angle -= 2 * D3DX_PI;

D3DXVECTOR3 sphere_center(0.0f, 100.0f, 0.0f);
LazerPos = getPositionInSphere(sphere_center, 2.0f, angle, D3DX_PI / 2);
D3DXMatrixTranslation(&matTrans, LazerPos.x, LazerPos.y, LazerPos.z);

}


##### Share on other sites

You're resetting the angle to 0 every update, that's why it doesn't change. Move angle to the class definition and only initialize it to 0 in the constructor.

class Lazer
{
[...]
float angle;
};

Laser::Laser() : angle(0.0f)
{
[...]
}

void Lazer::UpdateLazer(float dt)
{
angle += dt*D3DX_PI / 4; //rotate 45 degrees per seconds

[...]
}


##### Share on other sites

its moving in a  wheel type movement, i wanted a helicoper rotor type motion

##### Share on other sites

Use the modified function I wrote on reply #4 getPositionInSphereD3D.

1. 1
2. 2
Rutin
16
3. 3
4. 4
5. 5

• 26
• 11
• 9
• 9
• 11
• ### Forum Statistics

• Total Topics
633704
• Total Posts
3013460
×

## Important Information

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!