The parabolic motion of an object is one of the basic sports physical models in the game. In the PC game can be easily simulated by the gravity formula, but in mobile games, because most mobile phones do not support floating-point operations so can not use sin, cos, to decompose the initial speed. So we can only use the approximate simulation Method! What I'm using is: zooming in and out of the simulation, and adding a certain offset for more precision.
First, the 0-90-degree sine is listed with the hash table, and the value is magnified 100,000 times times, for example:
Hashtable Anglevalue;
public void Loadanglevalue ()
{
Anglevalue = new Hashtable ();
Anglevalue.put (string.valueof (0), new Integer (0));
Anglevalue.put (string.valueof), Newinteger (50000));
Anglevalue.put (string.valueof), New Integer (86603));
Anglevalue.put (string.valueof), New Integer (100000));
......
}
So we can get the positive cosine of various angles.
The initial velocity is x=0,y=0 for the V0 object at the current coordinates; T for time g gravity = 10;
The mechanics formula of the root play
vx=v0*cos&;
vy=v0*sin&;
And then according to the gravity formula:
X=vx*t;
Y=vy*t–5*t*t;
Since the cos& sin& are magnified 100,000 times times, the screen coordinates of the cell phone should be reduced by 100,000 times times.
x=vx*t/100000;
y= (vy*t–5*t*t)/100000;
Now the formula is solved in addition to t! Now to solve the time t;
We can keep increasing the value of T in the main loop of the game but because the main loop is very fast! Calculated in milliseconds, so we should add a buffer:
while (true) {
Thisthread.sleep (10);
if (Bfire) {
ttemp++;
if (Ttemp >10) {
T+=1;
ttemp = 0;
}
}
The value of if (Ttemp >10) in the code adjusts the frequency of the time! You can also use the IF (Ttemp >2) to speed up the time or to slow down with other values. The point of attention is that we have to enlarge the time! As to enlarge how many times it depends on the rhythm of the game! I'll zoom in 20,000 times times here, so the formula is:
x=vx*t/100000;
y= (vy*t–5*t*t*20000)/100000;
And we need to put the initial position of the object on the screen. We need to add an initial position constant, and the formula becomes:
x=vx*t/100000;
Y= (100000* (GetHeight () -20))-(vy*t–5*t*t*20000)/100000;
GetHeight () in the mobile phone to get the height of the screen. Okay, let's see what happens when you use this formula (NOKIA 7650 emulator or Unijava emulator)
Figure 1
This is a parabolic trajectory at a 45-degree angle.
Do not feel the height is not enough! The operation is not accurate! So we add an offset on Y to increase the height, and the formula changes to:
x= (Vx+windspeed) *t/100000;;
int pianyi= (t*400000);
if (vy==0) {
pianyi=0;
}
Y= (100000* (GetHeight ()-imgwu.getheight ())-(Vy*t-100000*t*t+pianyi))/100000;
Here the X-axis also adds the wind speed windspeed in reality the object will be affected by wind speed! Of course, the wind speed here is also magnified;
if (vy==0) {
pianyi=0;
}
This code is not required to add a height offset when the flat is thrown. Now look at the parabola at the angle of 45 degrees.
Figure 2
If you are not satisfied you can also change the offset value to make the simulation more accurate. Here's a look at some of the trajectory snapshots under different degrees of intensity and wind speed:
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.