سلام.
برای نمایش حرکت لازمه که در فریم های متوالی مکان و موقعیت توپ رو محاسبه کنید و تصویر مناسب رو بسازید و نمایش بدید. پس لازمه که زمان حال و زمان فریم قبل رو بدونیم و با استفاده از این اخلاف زمانی و دادن دلتا تایم به فرمول های فیزیکی مکان قبلی توپ رو به همراه سرعت اون گرفته و مکان جدید رو بدست بیاریم.
یک مثال در c++builder:
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
double GetNow()
{
LARGE_INTEGER lic, lif;
QueryPerformanceCounter(&lic);
QueryPerformanceFrequency(&lif);
return ((double)lic.QuadPart) / ((double)lif.QuadPart);
};
double current_time;
typedef struct TVec2D
{
float x, y;
} * PVec2D;
typedef struct TBall
{
TVec2D pos, speed;
} * PBall;
TBall * p_Ball = NULL;
//---------------------------------------------------------------------------
tagPOINT * convert_to_pixel_space(
tagPOINT * pOut,
const TVec2D * pV,
const float scale,
const float x_offset,
const float y_offset,
const int display_width,
const int display_height)
{
float x = (pV->x - x_offset) * scale;
float y = (pV->y - y_offset) * scale;
x = (display_width / 2) + x;
y = (display_height / 2) - y;
pOut->x = (int)x;
pOut->y = (int)y;
return pOut;
};
void render(TForm1 * f, TBall * p_ball)
{
f->Canvas->Rectangle(0, 0, f->ClientWidth, f->ClientHeight);
int radius = 10;
tagPOINT xy;
convert_to_pixel_space(
&xy,
&p_ball->pos,
100.0f,
0.0f,
0.0f,
f->ClientWidth,
f->ClientHeight);
f->Canvas->Ellipse(
xy.x - radius,
xy.y - radius,
xy.x + radius,
xy.y + radius);
};
void __fastcall TForm1::FormCreate(TObject *Sender)
{
p_Ball = new TBall();
p_Ball->pos.x = 0.0f;
p_Ball->pos.y = 0.0f;
p_Ball->speed.x = 0.0f;
p_Ball->speed.y = 0.0f;
current_time = GetNow();
};
//---------------------------------------------------------------------------
void __fastcall TForm1::FormMouseDown(TObject *Sender, TMouseButton Button, TShiftState Shift,
int X, int Y)
{
float x = this->ClientWidth / 2 - X;
float y = -(this->ClientHeight / 2 - Y);
p_Ball->speed.x = x * 0.1f;
p_Ball->speed.y = y * 0.1f;
p_Ball->pos.x = 0.0f;
p_Ball->pos.y = 0.0f;
current_time = GetNow();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Timer1Timer(TObject *Sender)
{
const float G = -9.8f;
const float Elasticy = 0.8f; // Object Material
//---------------------------------------
double now_t = GetNow();
double dt = now_t - current_time;
float dx = p_Ball->speed.x * dt;
float dy = p_Ball->speed.y * dt + 0.5f * G * dt * dt;
p_Ball->pos.x += dx;
p_Ball->pos.y += dy;
p_Ball->speed.x = dx / dt;
p_Ball->speed.y = dy / dt;
if (p_Ball->pos.y < 0.0f)
{
p_Ball->pos.y *= -1.0f;
p_Ball->speed.y *= -1.0f * Elasticy;
};
render(this, p_Ball);
current_time = now_t;
}
//---------------------------------------------------------------------------