HGE engine study Note 1: how to simulate physical collisions

Source: Internet
Author: User

Several problems to be solved during physical collision simulation:
 
1. How to simulate speed changes?
Set a friction coefficient friction (0 <friction <1.0) and an X coordinate single-time slice incremental dx after the response user presses the button, and an Y coordinate single-time slice incremental dy, every other time slice dx * = friction; dy * = friction; as long as the parameter is set properly, it seems that the speed naturally slows down. because of the Precision Limit of the floating point data type we use, the object stops after a certain number of time slices.
 
2. How to simulate a collision?
During Processing of each time slice, it is determined whether the edge coordinates of the object have exceeded the screen edge after dx and dy are added. If so, a certain policy is adopted to reset the coordinates of the object to make it within the normal range, if the X axis is exceeded, dx is reversed. If the Y axis is exceeded, dy is reversed. after the coordinates are calculated, draw again.
 
3. Sound processing during collision
Here the volume, left-right audio, and playback speed are involved. you can set the volume and playback speed according to the scenario. You can take the X and Y coordinates as one of the parameters. A reasonable solution for left and right channels is to determine the mixing ratio of left and right channels based on the window width and the X coordinate of the object.
 
 
The windows sdk window simulates this process (only imitating the speed and collision, etc. The processing of the sound seems complicated, and it hasn't figured out how to write it yet. because it is a simple DEMO without multithreading and other technologies, the synchronization of coordinates and other data in the program is not accurate. It is best to right-click the top, bottom, and left to release it in a short time, if you press the button, the speed changes suddenly ):
[Cpp]
/**
* FILE: collision. cpp
* Function: simulates collision and other activities of a ball in a closed area.
* Author: mzlogin (http://blog.csdn.net/mzlogin)
* Statement: copyright is not pirated.
*/
 
# Include <windows. h>
 
Float x = 100366f; // X coordinate of the center of the ball
Float y = 100366f; // Y coordinate of the center of the ball
Float speed = 10.0f; // The initial speed after the ball response button
Float friction = 0.99f; // friction coefficient between the ball and the ground
Float dx = 0.0f; // X axis Increment
Float dy = 0.0f; // y axis Increment
 
Lresult callback MainWndProc (HWND, UINT, WPARAM, LPARAM );
 
Int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR sz1_line, int nCmdShow)
{
WNDCLASS wc;
MSG msg;
HWND hWnd;
 
If (! HPrevInstance)
{
Wc. lpszClassName = "GenericAppClass ";
Wc. lpfnWndProc = MainWndProc;
Wc. style = CS_OWNDC | CS_VREDRAW | CS_HREDRAW;
Wc. hInstance = hInstance;
Wc. hIcon = LoadIcon (NULL, IDI_APPLICATION );
Wc. hCursor = LoadCursor (NULL, IDC_ARROW );
Wc. hbrBackground = (HBRUSH) (COLOR_WINDOW + 1 );
Wc. lpszMenuName = "GenericAppMenu ";
Wc. cbClsExtra = 0;
Wc. cbWndExtra = 0;
 
RegisterClass (& wc );
}
 
HWnd = CreateWindow ("GenericAppClass ",
"Happy Ball ",
WS_OVERLAPPEDWINDOW &(~ WS_MAXIMIZEBOX )&(~ WS_THICKFRAME ),
100,
100,
800,
600,
NULL,
NULL,
HInstance,
NULL
);
 
ShowWindow (hWnd, nCmdShow );
 
While (GetMessage (& msg, NULL, 0, 0 )){
TranslateMessage (& msg );
DispatchMessage (& msg );
}
 
Return msg. wParam;
}
 
Lresult callback MainWndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
Static PAINTSTRUCT ps;
Static HDC hDC;
Static HBRUSH hBrush;
Static RECT rect;
GetClientRect (hwnd, & rect );
Const int nRadius = 16;
 
Switch (message)
{
Case WM_CREATE:
SetTimer (hwnd, 1, 5, NULL );
HBrush = CreateSolidBrush (RGB (0, 0, 0 ));
Return 0;
 
Case WM_PAINT:
HDC = BeginPaint (hwnd, & ps );
SelectObject (hDC, hBrush );
Ellipse (hDC, x-nRadius, y-nRadius, x + nRadius, y + nRadius );
EndPaint (hwnd, & ps );
Break;
 
Case WM_KEYDOWN:
Switch (wParam)
{
Case VK_UP:
Dy-= speed;
Break;
Case VK_DOWN:
Dy + = speed;
Break;
Case VK_LEFT:
Dx-= speed;
Break;
Case VK_RIGHT:
Dx + = speed;
Break;
}
Break;
 
Case WM_TIMER:
Dx * = friction;
Dy * = friction;
X + = dx;
Y + = dy;
If (x> rect. right-nRadius) {x = (rect. right-nRadius)-(x-(rect. right-nRadius); dx =-dx ;}
If (x <nRadius) {x = nRadius + nRadius-x; dx =-dx ;}
If (y> rect. bottom-nRadius) {y = rect. bottom-nRadius-(y-(rect. bottom-nRadius); dy =-dy ;}
If (y <nRadius) {y = nRadius + nRadius-y; dy =-dy ;}
InvalidateRect (hwnd, & rect, TRUE );
 
Break;
 
Case WM_DESTROY:
KillTimer (hwnd, 1 );
DeleteObject (hBrush );
PostQuitMessage (0 );
Return 0;
}
Return DefWindowProc (hwnd, message, wParam, lParam );
}

HGE sample source code and Win32SDK source code and executable program: http://www.bkjia.com/uploadfile/2012/0213/20120213111636377.rar


From deserve your kick

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.