Discuss Scratch

Guest_102233
Scratcher
100+ posts

Trajectory Calculation for a moving or a still target

So I am currently making a Scrolling platformer boss fight and I set up the projectiles' physics engine like this (edited for relevancy):
define PlaneSlopeStart (InitialVel) (SpawnX) (SpawnY)
go to x:(SpawnX) y:(SpawnY)
point towards [character v]
move (InitialVel) steps
set [xvball v] to ((SpawnX) + ([x position v] of [Level v]))
set [yvball v] to ((SpawnY) + ([y position v] of [Level v]))
go to x:(SpawnX) y:(SpawnY)
and I need help finding the formula/steps to calculate the perfect trajectory for a projectile to hit a moving target or even a still target. I looked through multiple websites. The one that seems to have the answer is here, but I can't understand it fully.

Last edited by Guest_102233 (Nov. 21, 2021 02:33:12)


| My Profile | Highlight my entire signature and press CTRL+SHIFT+Down to scroll down long signatures |
To make your signature (like this), go here: https://scratch.mit.edu/discuss/settings/PutYourUsernameHere/


if <<quick? :: #965B01> and <dog <lazy? ::  #965B01> :: #984723>> then
jump over x:( dog (x):: #984723) y:(dog(y):: #984723):: #112233
end
Guest_102233
Scratcher
100+ posts

Trajectory Calculation for a moving or a still target

bump

| My Profile | Highlight my entire signature and press CTRL+SHIFT+Down to scroll down long signatures |
To make your signature (like this), go here: https://scratch.mit.edu/discuss/settings/PutYourUsernameHere/


if <<quick? :: #965B01> and <dog <lazy? ::  #965B01> :: #984723>> then
jump over x:( dog (x):: #984723) y:(dog(y):: #984723):: #112233
end
Guest_102233
Scratcher
100+ posts

Trajectory Calculation for a moving or a still target

bumpity bump

| My Profile | Highlight my entire signature and press CTRL+SHIFT+Down to scroll down long signatures |
To make your signature (like this), go here: https://scratch.mit.edu/discuss/settings/PutYourUsernameHere/


if <<quick? :: #965B01> and <dog <lazy? ::  #965B01> :: #984723>> then
jump over x:( dog (x):: #984723) y:(dog(y):: #984723):: #112233
end
bSpyder
Scratcher
100+ posts

Trajectory Calculation for a moving or a still target

Hmm. You could make a custom block with run without screen refresh.
You could tweak the values of course (velocity might not be in steps and of course time is not accurate)

define calcTraj (vel) (timeSpan)
set [logX v] to (x position)
set [logY v] to (y position)
repeat (timeSpan)
move (vel) steps
end
set [xTraj v] to (x position)
set [yTraj v] to (y position)
go to x: (logX v) y: (logY v)

Last edited by bSpyder (Nov. 22, 2021 21:50:07)


idk what im doing
Guest_102233
Scratcher
100+ posts

Trajectory Calculation for a moving or a still target

bSpyder wrote:

Hmm. You could make a custom block with run without screen refresh.
You could tweak the values of course (velocity might not be in steps and of course time is not accurate)

define calcTraj (vel) (timeSpan)
set [logX v] to (x position)
set [logY v] to (y position)
repeat (timeSpan)
move (vel) steps
end
set [xTraj v] to (x position)
set [yTraj v] to (y position)
go to x: (logX v) y: (logY v)

Tweaking this code to be compatible with scrolling platformers, the global variables would be a waste of space and local (procedure only) variables can replace it. The repeat timeSpan, move vel steps section doesn't make any sense to me.

| My Profile | Highlight my entire signature and press CTRL+SHIFT+Down to scroll down long signatures |
To make your signature (like this), go here: https://scratch.mit.edu/discuss/settings/PutYourUsernameHere/


if <<quick? :: #965B01> and <dog <lazy? ::  #965B01> :: #984723>> then
jump over x:( dog (x):: #984723) y:(dog(y):: #984723):: #112233
end
Guest_102233
Scratcher
100+ posts

Trajectory Calculation for a moving or a still target

bump

| My Profile | Highlight my entire signature and press CTRL+SHIFT+Down to scroll down long signatures |
To make your signature (like this), go here: https://scratch.mit.edu/discuss/settings/PutYourUsernameHere/


if <<quick? :: #965B01> and <dog <lazy? ::  #965B01> :: #984723>> then
jump over x:( dog (x):: #984723) y:(dog(y):: #984723):: #112233
end
bSpyder
Scratcher
100+ posts

Trajectory Calculation for a moving or a still target

so say every tick the target moves.
And every tick the target moves its velocity.

Thats what timeSpan and vel means

idk what im doing
Guest_102233
Scratcher
100+ posts

Trajectory Calculation for a moving or a still target

bSpyder wrote:

so say every tick the target moves.
And every tick the target moves its velocity.

Thats what timeSpan and vel means
I see..

| My Profile | Highlight my entire signature and press CTRL+SHIFT+Down to scroll down long signatures |
To make your signature (like this), go here: https://scratch.mit.edu/discuss/settings/PutYourUsernameHere/


if <<quick? :: #965B01> and <dog <lazy? ::  #965B01> :: #984723>> then
jump over x:( dog (x):: #984723) y:(dog(y):: #984723):: #112233
end
Guest_102233
Scratcher
100+ posts

Trajectory Calculation for a moving or a still target

bump

| My Profile | Highlight my entire signature and press CTRL+SHIFT+Down to scroll down long signatures |
To make your signature (like this), go here: https://scratch.mit.edu/discuss/settings/PutYourUsernameHere/


if <<quick? :: #965B01> and <dog <lazy? ::  #965B01> :: #984723>> then
jump over x:( dog (x):: #984723) y:(dog(y):: #984723):: #112233
end
PutneyCat
Scratcher
500+ posts

Trajectory Calculation for a moving or a still target

This seems harder than I thought it would be. That website doesn't make clear how you go directly from the things you know (speed of projectile and horizontal distance) to the required angle.

But here's an approach (for calculating theta) that more or less works. Theta is the angle (above horizontal) you're looking for. S is the speed of the projectile. A is acceleration due to gravity. D is the horizontal distance away. This assumes everything is on the same horizontal level.

Theta = 90 - asin((-A/D)/(SxS))/2

Once you have calculated theta, the initial velocities will be vx = S x cos(theta) and vy = S x sin(theta). At each stage of a loop you change x position by VX, y position by VY, and VY by A.

I've tried this using S = 15, A = -1.

Guest_102233
Scratcher
100+ posts

Trajectory Calculation for a moving or a still target

PutneyCat wrote:

This seems harder than I thought it would be. That website doesn't make clear how you go directly from the things you know (speed of projectile and horizontal distance) to the required angle.

But here's an approach (for calculating theta) that more or less works. Theta is the angle (above horizontal) you're looking for. S is the speed of the projectile. A is acceleration due to gravity. D is the horizontal distance away. This assumes everything is on the same horizontal level.

Theta = 90 - asin((-A/D)/(SxS))/2

Once you have calculated theta, the initial velocities will be vx = S x cos(theta) and vy = S x sin(theta). At each stage of a loop you change x position by VX, y position by VY, and VY by A.

I've tried this using S = 15, A = -1.

I'll try testing this out

| My Profile | Highlight my entire signature and press CTRL+SHIFT+Down to scroll down long signatures |
To make your signature (like this), go here: https://scratch.mit.edu/discuss/settings/PutYourUsernameHere/


if <<quick? :: #965B01> and <dog <lazy? ::  #965B01> :: #984723>> then
jump over x:( dog (x):: #984723) y:(dog(y):: #984723):: #112233
end
Guest_102233
Scratcher
100+ posts

Trajectory Calculation for a moving or a still target

PutneyCat wrote:

S is the speed of the projectile.
What do you mean by the speed of the projectile? Is it the X velocity, Y velocity, or the speed defined by the move () steps block?

Last edited by Guest_102233 (Nov. 26, 2021 19:52:18)


| My Profile | Highlight my entire signature and press CTRL+SHIFT+Down to scroll down long signatures |
To make your signature (like this), go here: https://scratch.mit.edu/discuss/settings/PutYourUsernameHere/


if <<quick? :: #965B01> and <dog <lazy? ::  #965B01> :: #984723>> then
jump over x:( dog (x):: #984723) y:(dog(y):: #984723):: #112233
end
PutneyCat
Scratcher
500+ posts

Trajectory Calculation for a moving or a still target

Speed is just a measure of how fast your projectile can go (in any direction). Velocity is speed in a particular direction (vx = horizontal, vy = vertical).

(Think of a right angle triangle - speed is the length of the hypotenuse.)

Speed is what you would insert if you were using the “move x steps” block. But you probably won't want to use that block for something like this, because you'd have to repeatedly recalculate the angle of movement.
Guest_102233
Scratcher
100+ posts

Trajectory Calculation for a moving or a still target

PutneyCat wrote:

Speed is just a measure of how fast your projectile can go (in any direction). Velocity is speed in a particular direction (vx = horizontal, vy = vertical).

(Think of a right angle triangle - speed is the length of the hypotenuse.)

Speed is what you would insert if you were using the “move x steps” block. But you probably won't want to use that block for something like this, because you'd have to repeatedly recalculate the angle of movement.

Okay

| My Profile | Highlight my entire signature and press CTRL+SHIFT+Down to scroll down long signatures |
To make your signature (like this), go here: https://scratch.mit.edu/discuss/settings/PutYourUsernameHere/


if <<quick? :: #965B01> and <dog <lazy? ::  #965B01> :: #984723>> then
jump over x:( dog (x):: #984723) y:(dog(y):: #984723):: #112233
end
PutneyCat
Scratcher
500+ posts

Trajectory Calculation for a moving or a still target

PS:

For clarity, speed is the projectile's speed when first fired (it will change under the influence of gravity).

Also, looking again at the website, I think you can in fact just use the 10th equation (for stationary target). What confused me was that he seemed to start (in his right angle triangle illustration) by using x and y to refer to velocities. But in the set of 10 equations I think they refer to distances (in x and y directions). So you could try just implementing the 10th equation.

ventoaurum
Scratcher
100+ posts

Trajectory Calculation for a moving or a still target

can you share the project? I need to figure out how the scrolling work for me to help u


(signature)
i make bad decisions professionally
Guest_102233
Scratcher
100+ posts

Trajectory Calculation for a moving or a still target

ventoaurum wrote:

can you share the project? I need to figure out how the scrolling work for me to help u

Here it is https://scratch.mit.edu/projects/607938031

| My Profile | Highlight my entire signature and press CTRL+SHIFT+Down to scroll down long signatures |
To make your signature (like this), go here: https://scratch.mit.edu/discuss/settings/PutYourUsernameHere/


if <<quick? :: #965B01> and <dog <lazy? ::  #965B01> :: #984723>> then
jump over x:( dog (x):: #984723) y:(dog(y):: #984723):: #112233
end
Guest_102233
Scratcher
100+ posts

Trajectory Calculation for a moving or a still target

PutneyCat wrote:

PS:

For clarity, speed is the projectile's speed when first fired (it will change under the influence of gravity).

Also, looking again at the website, I think you can in fact just use the 10th equation (for stationary target). What confused me was that he seemed to start (in his right angle triangle illustration) by using x and y to refer to velocities. But in the set of 10 equations I think they refer to distances (in x and y directions). So you could try just implementing the 10th equation.

I'll try…

| My Profile | Highlight my entire signature and press CTRL+SHIFT+Down to scroll down long signatures |
To make your signature (like this), go here: https://scratch.mit.edu/discuss/settings/PutYourUsernameHere/


if <<quick? :: #965B01> and <dog <lazy? ::  #965B01> :: #984723>> then
jump over x:( dog (x):: #984723) y:(dog(y):: #984723):: #112233
end
ventoaurum
Scratcher
100+ posts

Trajectory Calculation for a moving or a still target

I'm sorry that I can't help you(I'm preparing for exam), but this link might help


(signature)
i make bad decisions professionally
Guest_102233
Scratcher
100+ posts

Trajectory Calculation for a moving or a still target

PutneyCat wrote:

Theta = 90 - asin((-A/D)/(SxS))/2
Do you subtract 90, then divide by 2, or divide by 2, then subtract 90?

Visualisation:
(((90) - (...)) / (2))

((90) - ((...) / (2)))

| My Profile | Highlight my entire signature and press CTRL+SHIFT+Down to scroll down long signatures |
To make your signature (like this), go here: https://scratch.mit.edu/discuss/settings/PutYourUsernameHere/


if <<quick? :: #965B01> and <dog <lazy? ::  #965B01> :: #984723>> then
jump over x:( dog (x):: #984723) y:(dog(y):: #984723):: #112233
end

Powered by DjangoBB