شبیه سازی برخورد توپ با زمین با ویژگی جاذبه؟ - هفت خط کد انجمن پرسش و پاسخ برنامه نویسی

شبیه سازی برخورد توپ با زمین با ویژگی جاذبه؟

+1 امتیاز

سلام

دوستان من یه سوال برام پیش اومده من میخوام بخورد حرکت توپ با زمین رو شبیه سازی کنم (البته با ویزگی جاذبه زمین) نمونه هایی هم در این مورد
هست در سطح اینترنت اما توضیحشون و کداشون گیج کنندست میخوام خودم این کار رو انجام بدم آیا فرمول خاصی داره فرمولش چیه ؟
و اگه کسی این کار رو کرده یه توضیحی بده که یه اطلاعاتی در مورد شبیه سازیش داشته باشم.


توجه : دوستان لطفآ کد قرار ندهید قرار خودم این کار رو کنم فقط در مورد شبیه سازیش یه توضیحی بدید ممنون میشم


با تشکر

سوال شده شهریور 7, 1393  بوسیله ی pedram bahrami (امتیاز 19)   1 2 5

2 پاسخ

+2 امتیاز

box2d رو ببین. کتابخانه‌ای که Angry Birds هم بر پایه اون بنا شده

 

پاسخ داده شده شهریور 8, 1393 بوسیله ی حامد مصافى (امتیاز 1,104)   2 3 12
+3 امتیاز

سلام.

برای نمایش حرکت لازمه که در فریم های متوالی مکان و موقعیت توپ رو محاسبه کنید و تصویر مناسب رو بسازید و نمایش بدید. پس لازمه که زمان حال و زمان فریم قبل رو بدونیم و با استفاده از این اخلاف زمانی و دادن دلتا تایم به فرمول های فیزیکی مکان قبلی توپ رو به همراه سرعت اون گرفته و مکان جدید رو بدست بیاریم.

یک مثال در 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;
}
//---------------------------------------------------------------------------

 

پاسخ داده شده شهریور 9, 1393 بوسیله ی محمد قدیانی (امتیاز 317)   1 9
ویرایش شده شهریور 9, 1393 بوسیله ی محمد قدیانی
...