how to make a 3D Objbect move in circle?

Started by
6 comments, last by Aqua Costa 9 years, 6 months ago

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;
    }
    
}
:)
Advertisement

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), 
                   sphere_center.z + radius * cos(tetha)
}

what value do i set for the theta and pi to

:)

image001.gif

Follow this image. p is the radius, ? (theta) azimuthal angle, and ? (phi) polar angle.
The angles are in radians.

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.y + radius * cos(phi),
                   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,
                   sphere_center.z + radius * cos(theta));
}

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.y + radius * cos(theta),
        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);

    
}
:)

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

    [...]
}

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

:)

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

This topic is closed to new replies.

Advertisement