开发者

projectile motion in android

I am trying to implement projectile motion in android. I want my object to follow the trajectory depending on inputs.

But I am stuck with few implementation issues:

  1. What should be my assumption of initial velocity and its x and y components.
  2. How do I make sure that my trajectory falls within visible screen.
  3. The instantaneous x and y component of the displacemnet are given by x=uxt + (1/2) axt2 and y= uyt + (1/2) ayt2. So how do I store the entire list of x and y co-ordinates to plot the trajectory path.
  4. Finally what is the best approach to make my object follow the path drawn by plotted co-ordinates.

===========================================================

Edit 1:

This is my progress so far:-

Based on my Imageview's on touch event I am calculating the angular rotation i.e. angle in radians and I am also making sure that maximum allowable value of angle is 90 degree.If the user rotates more than 90 degree my code sets the angle variable to 90 degree i.e. it's max value.

I am assuming positive Y up thus my gravity is negative.

I am assuming initial velocity to be 100mm/s i.e units are in milli meters and accordingly modified the value of accleration due to gravity as by default it is in m/s2.

Here is my code for plotting the trajectory points, I am getting positivie value for initial velocity for y axis but a large negative value for displacement along y axis.My x axis values are both positive but still I am unable to plot the points as the values do not lie in canvas.Please help.:-

Here is my code:-

 public void drawProjectile(double angle)
     {
         Log.w(this.getClass().getName(),"drawProjectile called");
         mUx = mUi*Math.cos(angle);
         mUy = mUi*Math.sin(angle);
         now =1;// unit is seconds

         for(int i = 1;i<=10;i++)
         {
             Log.w(this.getClass().getName(),"In plotting points loop");

         Log.d(this.getClass().getName(), "Value of Current Time is: " +Long.toString(now));
         mX1=(float) (mUx*now);
         mY1 = (float)(mUy*now+(mGravity*100/2)*now*now);

         Log.d(this.getClass().getName(), "Value of mUx: " + Double.toString(mUx));
         Log.d(this.getClass().getName(), "Value of mUy: " + Double.toString(mUy));
         Log.d(this.getClass().getName(), "Value of mX1: " + Float.toString(mX1));
         Log.d(this.getClass().getName(), "Value of mY1: " + Float.toString(mY1));
         mCanvas.drawPoint(mX1, mY1, mPaint);

         now+=5;
         }

Sample test values are:-

10-17 17:21:36.842: WARN/com.example.sitolia.Balls(617): drawProjectile called
10-17 17:21:36.842: WARN/com.example.sitolia.Balls(617): In plotting points loop
10-17 17:21:36.842: DEBUG/com.example.sitolia.Balls(617): Value of Current Time开发者_C百科 is: 1
10-17 17:21:36.842: DEBUG/com.example.sitolia.Balls(617): Value of mUx: 8.715580058482463
10-17 17:21:36.852: DEBUG/com.example.sitolia.Balls(617): Value of mUy: 99.61946930316475
10-17 17:21:36.852: DEBUG/com.example.sitolia.Balls(617): Value of mX1: 8.71558
10-17 17:21:36.852: DEBUG/com.example.sitolia.Balls(617): Value of mY1: 94.71947
10-17 17:21:36.852: WARN/com.example.sitolia.Balls(617): In plotting points loop
10-17 17:21:36.852: DEBUG/com.example.sitolia.Balls(617): Value of Current Time is: 6
10-17 17:21:36.885: DEBUG/com.example.sitolia.Balls(617): Value of mUx: 8.715580058482463
10-17 17:21:36.885: DEBUG/com.example.sitolia.Balls(617): Value of mUy: 99.61946930316475
10-17 17:21:36.885: DEBUG/com.example.sitolia.Balls(617): Value of mX1: 52.29348
10-17 17:21:36.885: DEBUG/com.example.sitolia.Balls(617): Value of mY1: 421.3168
10-17 17:21:36.885: WARN/com.example.sitolia.Balls(617): In plotting points loop
10-17 17:21:36.885: DEBUG/com.example.sitolia.Balls(617): Value of Current Time is: 11
10-17 17:21:36.885: DEBUG/com.example.sitolia.Balls(617): Value of mUx: 8.715580058482463
10-17 17:21:36.893: DEBUG/com.example.sitolia.Balls(617): Value of mUy: 99.61946930316475
10-17 17:21:36.893: DEBUG/com.example.sitolia.Balls(617): Value of mX1: 95.87138
10-17 17:21:36.893: DEBUG/com.example.sitolia.Balls(617): Value of mY1: 502.91415
10-17 17:21:36.893: WARN/com.example.sitolia.Balls(617): In plotting points loop
10-17 17:21:36.893: DEBUG/com.example.sitolia.Balls(617): Value of Current Time is: 16
10-17 17:21:36.893: DEBUG/com.example.sitolia.Balls(617): Value of mUx: 8.715580058482463
10-17 17:21:36.902: DEBUG/com.example.sitolia.Balls(617): Value of mUy: 99.61946930316475
10-17 17:21:36.902: DEBUG/com.example.sitolia.Balls(617): Value of mX1: 139.44928
10-17 17:21:36.902: DEBUG/com.example.sitolia.Balls(617): Value of mY1: 339.5115
10-17 17:21:36.902: WARN/com.example.sitolia.Balls(617): In plotting points loop
10-17 17:21:36.902: DEBUG/com.example.sitolia.Balls(617): Value of Current Time is: 21
10-17 17:21:36.902: DEBUG/com.example.sitolia.Balls(617): Value of mUx: 8.715580058482463
10-17 17:21:36.902: DEBUG/com.example.sitolia.Balls(617): Value of mUy: 99.61946930316475
10-17 17:21:36.902: DEBUG/com.example.sitolia.Balls(617): Value of mX1: 183.02718
10-17 17:21:36.902: DEBUG/com.example.sitolia.Balls(617): Value of mY1: -68.89114
10-17 17:21:36.902: WARN/com.example.sitolia.Balls(617): In plotting points loop
10-17 17:21:36.902: DEBUG/com.example.sitolia.Balls(617): Value of Current Time is: 26
10-17 17:21:36.902: DEBUG/com.example.sitolia.Balls(617): Value of mUx: 8.715580058482463
10-17 17:21:36.912: DEBUG/com.example.sitolia.Balls(617): Value of mUy: 99.61946930316475
10-17 17:21:36.912: DEBUG/com.example.sitolia.Balls(617): Value of mX1: 226.60509
10-17 17:21:36.912: DEBUG/com.example.sitolia.Balls(617): Value of mY1: -722.2938
10-17 17:21:36.912: WARN/com.example.sitolia.Balls(617): In plotting points loop
10-17 17:21:36.912: DEBUG/com.example.sitolia.Balls(617): Value of Current Time is: 31
10-17 17:21:36.912: DEBUG/com.example.sitolia.Balls(617): Value of mUx: 8.715580058482463
10-17 17:21:36.912: DEBUG/com.example.sitolia.Balls(617): Value of mUy: 99.61946930316475
10-17 17:21:36.922: DEBUG/com.example.sitolia.Balls(617): Value of mX1: 270.18298
10-17 17:21:36.922: DEBUG/com.example.sitolia.Balls(617): Value of mY1: -1620.6964
10-17 17:21:36.922: WARN/com.example.sitolia.Balls(617): In plotting points loop
10-17 17:21:36.922: DEBUG/com.example.sitolia.Balls(617): Value of Current Time is: 36
10-17 17:21:36.922: DEBUG/com.example.sitolia.Balls(617): Value of mUx: 8.715580058482463
10-17 17:21:36.922: DEBUG/com.example.sitolia.Balls(617): Value of mUy: 99.61946930316475
10-17 17:21:36.992: DEBUG/com.example.sitolia.Balls(617): Value of mX1: 313.7609
10-17 17:21:36.992: DEBUG/com.example.sitolia.Balls(617): Value of mY1: -2764.099
10-17 17:21:36.992: WARN/com.example.sitolia.Balls(617): In plotting points loop
10-17 17:21:36.992: DEBUG/com.example.sitolia.Balls(617): Value of Current Time is: 41
10-17 17:21:36.992: DEBUG/com.example.sitolia.Balls(617): Value of mUx: 8.715580058482463
10-17 17:21:36.992: DEBUG/com.example.sitolia.Balls(617): Value of mUy: 99.61946930316475
10-17 17:21:36.992: DEBUG/com.example.sitolia.Balls(617): Value of mX1: 357.33878
10-17 17:21:37.002: DEBUG/com.example.sitolia.Balls(617): Value of mY1: -4152.502
10-17 17:21:37.002: WARN/com.example.sitolia.Balls(617): In plotting points loop
10-17 17:21:37.002: DEBUG/com.example.sitolia.Balls(617): Value of Current Time is: 46
10-17 17:21:37.002: DEBUG/com.example.sitolia.Balls(617): Value of mUx: 8.715580058482463
10-17 17:21:37.037: DEBUG/com.example.sitolia.Balls(617): Value of mUy: 99.61946930316475
10-17 17:21:37.037: DEBUG/com.example.sitolia.Balls(617): Value of mX1: 400.9167
10-17 17:21:37.037: DEBUG/com.example.sitolia.Balls(617): Value of mY1: -5785.9043
10-17 17:21:37.037: DEBUG/com.example.sitolia.SitoliaActivity(617): Value of angle in ontouch: 1.4835298


Some initial advice: Decide on which Y direction is up (positive or negative) and ensure theco-ordinate system of your math and your graphics API agree about this. For the purposes of this answer I'll call negative Y up, thus gravity is a positive value.

Your Issues:

  1. You need a largish negative value for the Y velocity and some small value for the x velocity (otherwise it just goes straight and down). Typically these are calculated using school level trigonometry from the launch Speed and angle to the ground.

  2. Ideally you would want to scale the graphics API (that is what the coordinate system that maps to the screen) to show whatever space it needs to dynamically. To start with it might be easier to stick with initial values of vx and vy that you have calculated to fit on your screen.

  3. If you want store a list of the coordinates you would use an array, however this wouldn't usually be needed because of my solution to 4 below

  4. Normally given how easy it is to calculate the points you would calculate them as you needed to draw them, specifically in the code that draws the trajectory you could loop through all relevant values of t, calculate the x and y displacements and draw a dot at that point - thus as you progress along increasing higher values of t in the loop the line of the trajectory is drawn.

Regarding timers: I think you need to decide if you want to animate the projectile across the screen or plot the path of the projectile as a line all at once. I don't know much about the accuracy or mechanisms to use within Android (I'm an android newbie) but in general think the strategy to use for animations of this sort would be to rely on a regular callback from the OS but then use some kind of ~1/100s accurate time-mark to measure the time between when you drew the last frame and now - use this as the amount to put the time variable forward by and draw the new frame. This strategy ensures that the animation will go at the same speed on differing hardware and software environments.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜