如何在libgdx中为精灵应用圆周运动

时间:2016-08-11 05:56:18

标签: android libgdx sprite

我想要四个精灵绕一个原点运行。但如果我这样使用

void ae3d::VertexBuffer::UploadVB( void* faces, void* vertices, unsigned ibSize )
{
    D3D12_HEAP_PROPERTIES uploadProp = {};
    uploadProp.Type = D3D12_HEAP_TYPE_UPLOAD;
    uploadProp.CPUPageProperty = D3D12_CPU_PAGE_PROPERTY_UNKNOWN;
    uploadProp.MemoryPoolPreference = D3D12_MEMORY_POOL_UNKNOWN;
    uploadProp.CreationNodeMask = 1;
    uploadProp.VisibleNodeMask = 1;

    D3D12_RESOURCE_DESC bufferProp = {};
    bufferProp.Alignment = 0;
    bufferProp.DepthOrArraySize = 1;
    bufferProp.Dimension = D3D12_RESOURCE_DIMENSION_BUFFER;
    bufferProp.Flags = D3D12_RESOURCE_FLAG_NONE;
    bufferProp.Format = DXGI_FORMAT_UNKNOWN;
    bufferProp.Height = 1;
    bufferProp.Layout = D3D12_TEXTURE_LAYOUT_ROW_MAJOR;
    bufferProp.MipLevels = 1;
    bufferProp.SampleDesc.Count = 1;
    bufferProp.SampleDesc.Quality = 0;
    bufferProp.Width = ibOffset + ibSize;

    // this branch resizes the buffer and causes validation errors.
    if (vb != nullptr && bufferProp.Width <= sizeBytes)
    {
        ID3D12Resource* stagingBuffer = nullptr;

        HRESULT hr = GfxDeviceGlobal::device->CreateCommittedResource( &uploadProp, D3D12_HEAP_FLAG_NONE, &bufferProp,
            D3D12_RESOURCE_STATE_GENERIC_READ, nullptr, IID_PPV_ARGS( &stagingBuffer ) );
        AE3D_CHECK_D3D( hr, "Failed to create vertex staging resource" );

        char* vbUploadPtr = nullptr;
        hr = stagingBuffer->Map( 0, nullptr, reinterpret_cast<void**>(&vbUploadPtr) );
        if (FAILED( hr ))
        {
            ae3d::System::Assert( false, "Unable to map vertex staging buffer!\n" );
            return;
        }

        memcpy_s( vbUploadPtr, ibOffset, vertices, ibOffset );
        memcpy_s( vbUploadPtr + ibOffset, ibSize, faces, ibSize );

        stagingBuffer->Unmap( 0, nullptr );


        // vb state is invalid at this point, needs D3D12_RESOURCE_STATE_COPY_DEST
        GfxDeviceGlobal::graphicsCommandList->CopyBufferRegion( vb, 0, stagingBuffer, 0, ibOffset + ibSize );

        Global::frameVBUploads.push_back( stagingBuffer );
        sizeBytes = ibOffset + ibSize;

        return;
    }

    sizeBytes = ibOffset + ibSize;

    HRESULT hr = GfxDeviceGlobal::device->CreateCommittedResource(
        &uploadProp,
        D3D12_HEAP_FLAG_NONE,
        &bufferProp,
        D3D12_RESOURCE_STATE_GENERIC_READ,
        nullptr,
        IID_PPV_ARGS( &vb ) );
    if (FAILED( hr ))
    {
        ae3d::System::Assert( false, "Unable to create vertex buffer!\n" );
        return;
    }

    Global::vbs.push_back( vb );

    char* vbUploadPtr = nullptr;
    hr = vb->Map( 0, nullptr, reinterpret_cast<void**>(&vbUploadPtr) );
    if (FAILED( hr ))
    {
        ae3d::System::Assert( false, "Unable to map vertex buffer!\n" );
        return;
    }

    memcpy_s( vbUploadPtr, ibOffset, vertices, ibOffset );
    memcpy_s( vbUploadPtr + ibOffset, ibSize, faces, ibSize );
    vb->Unmap( 0, nullptr );
}

精灵在旋转时被反转,因为这些精灵是用户的个人资料图片,我希望他们在每个轨道上都保持笔直。有没有办法在没有旋转的情况下沿着圆形路径移动精灵。

enter image description here enter image description here

2 个答案:

答案 0 :(得分:3)

从您的代码中可以清楚地看到,您只是在将原始设置为100,100时旋转各个精灵

你必须通过改变它们的位置来移动中心周围的精灵(100,100),就像地球在太阳周围移动一样。

原点是精灵旋转中心。

所以给你的形状半径 r = 50, 圆心(cx,cy)=(100,100)

计算应用几何体的圆的周长上的点

double radianDegrees = Math.toRadians(d);
double updatedX = cx + r * Math.cos(radianDegrees);
double updatedY = cy + r * Math.sin(radianDegrees); 

并将角度增加a,即你想要移动精灵的速度

答案 1 :(得分:3)

您可以创建一个'助手' - 矢量,表示距离原点的距离。

就像那样:

private Vector2 origin;
private Vector2 distance;

void ctor(){
    origin = new Vector2();
    distance = new Vector2(0, 100);
}

public void update(float Delta){
    origin.set(200, 200);
    distance.rotate(40 * Delta);
    Vector2 newPos = origin.add(distance);
    sprite.setPosition(newPos.x - sprite.width/2, newPos.y - sprite.height/2);
}