This is a read-only archive of the old Scratch 1.x Forums.
Try searching the current Scratch discussion forums.

#1 2012-07-17 01:18:05

TorbyFork234
Scratcher
Registered: 2012-03-01
Posts: 1000+

Projectile physics

I'm making a game and I need to know the physics of an arrow's arc the air like in this project. I tried downloading that one but it was hard to understand with all the lists and scripts for the target in there too.

Can anyone help?

Offline

 

#2 2012-07-17 01:44:47

amcerbu
Scratcher
Registered: 2009-07-21
Posts: 500+

Re: Projectile physics

You use x- and y-velocity.  Taking the arctangent of (Velocity.Y) / (Velocity.X) will give you the angle of motion (although, you'll have to take 90 - (angle) to convert it to Scratch degrees).  However, the arctangent function doesn't work when x-velocity = 0 (dividing by 0).  You have to manually catch that one and tell the program what the value of the direction should be so that it doesn't freeze up.  Basically, you have a script like this:

when gf clicked
set [Acceleration.Gravity v] to [-0.2] // Choose a value
forever
change [Velocity.Y v] by (Acceleration.Gravity)
change [Position.X v] by (Velocity.X)
change [Position.Y v] by (Velocity.Y)
point in direction ((90) - ( [atan v] of ((Velocity.Y) / (Velocity.X)) ))
physics scripts...
go to x: (Position.X) y: (Position.Y)

Last edited by amcerbu (2012-07-17 01:45:03)

Offline

 

#3 2012-07-17 01:52:38

TorbyFork234
Scratcher
Registered: 2012-03-01
Posts: 1000+

Re: Projectile physics

amcerbu wrote:

You use x- and y-velocity.  Taking the arctangent of (Velocity.Y) / (Velocity.X) will give you the angle of motion (although, you'll have to take 90 - (angle) to convert it to Scratch degrees).  However, the arctangent function doesn't work when x-velocity = 0 (dividing by 0).  You have to manually catch that one and tell the program what the value of the direction should be so that it doesn't freeze up.  Basically, you have a script like this:

when gf clicked
set [Acceleration.Gravity v] to [-0.2] // Choose a value
forever
change [Velocity.Y v] by (Acceleration.Gravity)
change [Position.X v] by (Velocity.X)
change [Position.Y v] by (Velocity.Y)
point in direction ((90) - ( [atan v] of ((Velocity.Y) / (Velocity.X)) ))
physics scripts...
go to x: (Position.X) y: (Position.Y)

So basically, (not in scratch,) to add the effect of of an arrow's arc (without gravity playing a part yet), I do

< [atan v] of ((Velocity.Y) / (Velocity.X))>
I don't know what atan is/what it does, but I'll see if this works.

Last edited by TorbyFork234 (2012-07-17 01:52:59)

Offline

 

#4 2012-07-17 02:12:18

TorbyFork234
Scratcher
Registered: 2012-03-01
Posts: 1000+

Re: Projectile physics

It works amazingly! Thanks!

Offline

 

#5 2012-07-17 02:28:54

amcerbu
Scratcher
Registered: 2009-07-21
Posts: 500+

Re: Projectile physics

"atan" is short for arctangent.  It's an inverse trigonometric function.  The tangent function normally takes an angle measure and returns a slope (ΔY / ΔX).  The arctangent function takes a slope value and returns an angle.  You use it to "convert" between x and y velocity movement and directional movement. 

The part that adds gravity is the "change Velocity.Y by Acceleration.Gravity."

Offline

 

#6 2012-07-17 02:59:44

TorbyFork234
Scratcher
Registered: 2012-03-01
Posts: 1000+

Re: Projectile physics

amcerbu wrote:

The part that adds gravity is the "change Velocity.Y by Acceleration.Gravity."

I could tell that, the only thing I didn't understand with your scripts was the atan part.

Also, it get funky with Velocity.X=10, Velocity.Y=10, and Velocity.X=Velocity.X*0.9. It curves upwards then drops down.

Offline

 

#7 2012-07-17 20:40:30

amcerbu
Scratcher
Registered: 2009-07-21
Posts: 500+

Re: Projectile physics

Can you upload the project so I can see what's wrong?

Offline

 

#8 2012-07-17 21:57:43

AtomicBawm3
Scratcher
Registered: 2009-06-27
Posts: 1000+

Re: Projectile physics

TorbyFork234 wrote:

amcerbu wrote:

The part that adds gravity is the "change Velocity.Y by Acceleration.Gravity."

I could tell that, the only thing I didn't understand with your scripts was the atan part.

Also, it get funky with Velocity.X=10, Velocity.Y=10, and Velocity.X=Velocity.X*0.9. It curves upwards then drops down.

That's a pretty strong coefficient...try something more like .95 and the difference will be pretty visible..just think about it:
1: .9 and   .95
2: .81 and  .9025
3: .729 and  .857375
4: .6561 and .81450625

and so on... using just a little higher coefficient makes a big difference.


http://i50.tinypic.com/j0yw0p.jpg

Offline

 

#9 2012-07-17 22:50:56

amcerbu
Scratcher
Registered: 2009-07-21
Posts: 500+

Re: Projectile physics

^^ That's true.  In fact, you might even want something like 0.99 or 0.999 if you don't want the arrow to slow down too terribly fast.

Offline

 

#10 2012-07-18 00:22:14

TorbyFork234
Scratcher
Registered: 2012-03-01
Posts: 1000+

Re: Projectile physics

amcerbu wrote:

^^ That's true.  In fact, you might even want something like 0.99 or 0.999 if you don't want the arrow to slow down too terribly fast.

I know that, I'm just saying that when the coeffiecent is 0.9 it's completely weird. Also, I can't upload it right now amercbu.

Last edited by TorbyFork234 (2012-07-18 00:22:26)

Offline

 

#11 2012-07-18 00:37:28

AtomicBawm3
Scratcher
Registered: 2009-06-27
Posts: 1000+

Re: Projectile physics

TorbyFork234 wrote:

amcerbu wrote:

^^ That's true.  In fact, you might even want something like 0.99 or 0.999 if you don't want the arrow to slow down too terribly fast.

I know that, I'm just saying that when the coeffiecent is 0.9 it's completely weird. Also, I can't upload it right now amercbu.

Are you sure you didn't accidentally set it to -.9? Because that would make it pretty much stay in one place while rising and falling.


http://i50.tinypic.com/j0yw0p.jpg

Offline

 

#12 2012-07-18 01:12:16

amcerbu
Scratcher
Registered: 2009-07-21
Posts: 500+

Re: Projectile physics

Just try eliminating the block for friction altogether.  As it is, the friction on an arrow is pretty slim.

Offline

 

#13 2012-07-18 01:41:10

TorbyFork234
Scratcher
Registered: 2012-03-01
Posts: 1000+

Re: Projectile physics

@AtomicBawm3, I'm completely sure.

@amcerbu, I'm trying to make it scroll, with a lot of power, so adding in friction will make a difference on that large scale.

Offline

 

Board footer