【问题标题】:Simulating movement of a window and have it react to collisions模拟窗口的移动并让它对碰撞做出反应
【发布时间】:2009-06-01 07:13:37
【问题描述】:

我正在阅读此topic 并决定试一试。令我惊讶的是,它似乎真的比我想的要容易。我对“DesiredPos”变量有些困惑。至少在我的实现中。我试图不断移动一个窗口,让它在碰到显示器边缘时像球一样反应。就像乒乓球比赛中的球一样。我已经制作了这样的程序来移动鼠标,但我似乎无法理解这个。

这是我目前所拥有的,对于 Windows API 中的许多功能,我的经验有限。请记住,这是一个核心草稿。

编辑 我还没有实现任何碰撞检测,但我只是想让移动部分工作。

#include <windows.h>
#include <math.h>

int newX(int oldx);
int newY(int oldy);
double SmoothMoveELX(double x);
int main()
{

    int lengthInMs = 10*1000;
    HWND notepad = FindWindow("Notepad",NULL);
    RECT window;
    SetTimer(
            notepad,
            NULL,
            30,
            (TIMERPROC)NULL
            );
    int startTime = GetTickCount();
    int pos = elap / lengthInMs;


    while(1)
    {
            RECT window;
            GetWindowRect(notepad,&window);
            int elap = (GetTickCount() - startTime);
            if(elap  >= lengthInMs)
            {
                  int NEWX = NewX(window.x);
                  int NEWY = NewY(window.y);
                  MoveWindow(
                             notepad,
                             NEWX,
                             NEWY,
                             100,
                             100,
                             TRUE
                             );  
                             }
                             }
} 

int NewX(int oldx)
{
    int newx = oldx*(1-SmoothMoveELX(pos))
             + 10 *SmoothMoveELX(pos));

    return newx;
}

int newY(int oldy)
{
    int newy = oldy*(1-SmoothMoveELX(pos))
             + 10 *SmoothMoveELX(pos));
    return newy;
}

double SmoothMoveELX(double x)
{
       double PI = Atan(1) * 4;
       return ((cos(1 - x) * PI + 1) /2 )
}

【问题讨论】:

  • 那么你的问题是什么?

标签: c++ c winapi


【解决方案1】:

我的建议是看看“verlet 集成”。这是模拟基本力学的一种非常简单的方法。谷歌它,你会发现很多例子,包括碰撞检测和摩擦。从长远来看,这将为您提供更自然的结果,然后使用正弦函数估计速度或新位置。

【讨论】:

    猜你喜欢
    • 2013-08-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多