2017년 7월 31일 월요일

Trying to code projectile motion


I am trying to code an app to show projectile motion.  I want a ball to travel in a parabola across the screen. What I want the use to be able to do is enter the velocity and angle into a text box and the app to use the clock time.  
Equations are.


--
You have at least three errors in your blocks (image attached, please do so in the future to save us the trouble) ...

You are using Timer.Interval as the accumulated time theta.  
Read the tool tip for that block, and you will see that it is the (constant) interval between one clock fire and the next, initially 1000 milliseconds.
You are also failing to scale it with a speed factor.

You are using the x and y trigonometric outputs directly in the canvas coordinate system.
They do not at all match.
Canvas coordinates x,y run from 1 to Canvas1.Width and 1 to Canvas1.Height, with the origin at the upper left.
Math coordinates typically run from (0,0) at the center of a diagram, with y increasing upwards, opposite to the Canvas.

Sin and Cos outputs are <= 1 in absolute value, giving you a much too little dot on a canvas.
So you have to apply scale factors and offsets in your formula.

Let us know if you need more help.


--
I just noticed another error.

You asked for a parabola, Angry Birds style I imagine, but you posted formulas involving trig functions sin and cos.

There are two different ways of animating a thrown ball in AI2:
Relying on a clock routine to continually adjust the x,y coordinates of the ball on the canvas, or
Giving the ball an initial Speed and DIrection, and continually adjusting the Speed and Direction, letting AI2 move the ball.

I am starting to suspect you wanted to use the second method?
But your equations don't quite fit your parabola model.

Can you show us where you got them?

--
The formulas come out of my math classroom.  :) I am a teacher and just started using app inventor 2 in my master classes and I thought it would be great to have my Precalculus students use it in the classroom but i have to know how to use it first.  I really don't know how to make it travel in a parabola path.  
I don't know that I need a clock to make the ball move.  Here are the changes I have made and the travel is must more what I would want but now it is diagonal. 


--
I reworked your app to get it closer to what your formulas require.

It is at the point where the ball will move and fall downwards, but I can't translate drags and swipes into a healthy velocity.
You may need to apply fudge factors.

I used functions liberally to show the passage of time better and to separate the GUI from the math.

This sounds like the kind of example Scott Ferguson would have in his App Inventors Developers Library, if you're up to a Google Search.








--
My Angry Flappy Birdy project plots a parabola.

--
Thanks, @Scott.  It's good to have working code for an example.

Before I point out to the original poster how Angry Flappy Birds gets its parabola,
let me mention a side billiards project that might be useful for trigonometry and conservation of
momentum studies:

If you haven't seen the Angry Birds app yet, the idea is to have a slingshot
(in this case, a large capital Y) that can launch a bird into the air to hit a target.

(See the two screen shots).
(The slingshot doesn't line up with the launch points due to an inconsistency between
my emulator's resolution and Scott's device's resolution, and due to AI2's lack of
a facility to specify font height as a proportion of screen height.  No matter.)

An initially inactive clock is used to update the bird sprite's position each clock tick.
(Some extra code to spin the target cat after it is hit can be ignored.)

The launch happens in the When Birdy Touch Up block.
The calculation of the X and Y velocity components is hidden in
the drag event block, where two globals are updated:
global ForwardSpeed and global UpwardSpeed.

The repeating timer SlingBirdyTimer calls UpdateBirdyPosition each tick
to add the two speeds to the birdy's X and Y positions respectively.
Because of gravity, that routine also slows the upward speed by adjusting
it by the gravity constant.  (Because Canvas Y values increase downwards, not upwards.)

A separate routine draws red lines between successive positions
of the center of the bird, to show its trajectory.

You may notice that there is no (t squared ) calculation in this model,
only a ( delta V / delta t ) continual speed adjustment, so you would need slightly different
equations to model this.
I don't know if those would fit into your pre-calculus math.

I'll check my FAQ on this board to see if there is a good graph package I can
integrate into my own attempt.








--
I found a decent blocks-only line graph package in this board,
(Thank you, Ryan Cheung), and am reworking my attempt.

Trying to use a clock for real-time capture of the parabola is self-defeating, because you can't predict what scale factor you will need for the graph until a good set of (x,y) points has been collected.  Most launch attempts either bang against the edge of the canvas or flop to the ground, if the scale and velocity and gravity aren't in sync.

So the trick is to start from t=0, x=0, y=0 and keep incrementing t, collecting x(t) and y(t) until y < 0. then graph it all.

--
I got a full parabola using the technique I described in my prior post.
See attached.
The X axis numbers run together a bit.
I shrank their font size, but it wasn't enough.
I also rounded the X labels, to no avail.

I leave the tweaking to you.








--
Thank you very much for your help.  Here is what I was able to do for the application and I plan to add come creative elements.  You were very helpful.  


--
I looked in your .aia, and notice a typo in the V_sin_theta routine
left over from when I first coded it.

I'll leave it for you or your students to discover.

Learning to read what you code is an important part of coding.

--

댓글 1개: