Help with calculating trajectory

General discussion about LÖVE, Lua, game development, puns, and unicorns.
betobala
Prole
Posts: 11
Joined: Sun May 19, 2024 11:37 pm

Help with calculating trajectory

Post by betobala »

My goal is to do something like this: https://imgur.com/a/o4HaceP

This is what a got so far(just the line stopping to draw when detect a ball): https://imgur.com/a/rm8rStf

I'm using physics module in balls and table.

I don't have idea how to do it, could someone help me?

update1: https://imgur.com/a/FEmRLOd I managed to do the collision detection by using rayCast in a double sized ball shape, but could not get the angles right.
Attachments
RogueCue_update1.love
(440.47 KiB) Downloaded 375 times
RogueCue.love
(439.98 KiB) Downloaded 395 times
RNavega
Party member
Posts: 385
Joined: Sun Aug 16, 2020 1:28 pm

Re: Help with calculating trajectory

Post by RNavega »

See if you can work with ChatGPT for it to explain vector reflection for you.
You have the incoming vector and it's being reflected on the normal of a surface (like the surface of a ball, or the surface of the boundaries of the table).

To find the reflected vector, you flip the direction of the incoming vector, project it onto the normal, then get the rejection (the difference between the flipped incoming vector and its projection onto the normal) and add that rejection twice to the flipped incoming vector.
This will place you on the reflected position.

Image

Image taken from this article here:
https://www.fabrizioduroni.it/2017/08/2 ... on-vector/

On that image L is the (flipped) incoming vector, N is the surface normal, N' (the red arrow) is the projection of L onto N, and U' or U'' is the result of (L - N') AKA the rejection of L onto N.
So if you take L and add twice U (or subtract U, it depends on the sign of U given by how you calculated U), this will give you R, the reflected L onto N.

Other resources (in this order, since the first ones explain projection):
Xugro
Party member
Posts: 114
Joined: Wed Sep 29, 2010 8:14 pm

Re: Help with calculating trajectory

Post by Xugro »

If you don't want to do any of the math, then you can just simulate the shot. Take the table, the cue stick and the balls into an separate physics environment, do the shot there and if the white ball hits any other ball, then you have all the information you need to draw what you want: At the moment of impact you can draw where the white ball is. And if you simulate for a small amount of time afterwards you can draw the lines, where the balls will go.

You just need to be careful about secondary impacts. But those can be removed if you just simulate the cue stick, the white ball and the ball that was hit - e.g. in a second run (or another separate physic environment).
betobala
Prole
Posts: 11
Joined: Sun May 19, 2024 11:37 pm

Re: Help with calculating trajectory

Post by betobala »

RNavega wrote: Thu May 30, 2024 8:44 pm See if you can work with ChatGPT for it to explain vector reflection for you.
You have the incoming vector and it's being reflected on the normal of a surface (like the surface of a ball, or the surface of the boundaries of the table).

To find the reflected vector, you flip the direction of the incoming vector, project it onto the normal, then get the rejection (the difference between the flipped incoming vector and its projection onto the normal) and add that rejection twice to the flipped incoming vector.
This will place you on the reflected position.

Image

Image taken from this article here:
https://www.fabrizioduroni.it/2017/08/2 ... on-vector/

On that image L is the (flipped) incoming vector, N is the surface normal, N' (the red arrow) is the projection of L onto N, and U' or U'' is the result of (L - N') AKA the rejection of L onto N.
So if you take L and add twice U (or subtract U, it depends on the sign of U given by how you calculated U), this will give you R, the reflected L onto N.

Other resources (in this order, since the first ones explain projection):
Do you think the way I did is going to work? Like im not getting the normal from the ball itself, im getting from a invisible ball that has the double radius of the red ball.

Image (the point of contact is the green ball)
betobala
Prole
Posts: 11
Joined: Sun May 19, 2024 11:37 pm

Re: Help with calculating trajectory

Post by betobala »

Xugro wrote: Thu May 30, 2024 9:43 pm If you don't want to do any of the math, then you can just simulate the shot. Take the table, the cue stick and the balls into an separate physics environment, do the shot there and if the white ball hits any other ball, then you have all the information you need to draw what you want: At the moment of impact you can draw where the white ball is. And if you simulate for a small amount of time afterwards you can draw the lines, where the balls will go.

You just need to be careful about secondary impacts. But those can be removed if you just simulate the cue stick, the white ball and the ball that was hit - e.g. in a second run (or another separate physic environment).
I think this is more complicated than math and a little bit janky hahaha
User avatar
knorke
Party member
Posts: 274
Joined: Wed Jul 14, 2010 7:06 pm
Contact:

Re: Help with calculating trajectory

Post by knorke »

betobala wrote: Thu May 30, 2024 10:40 pm
RNavega wrote: Thu May 30, 2024 8:44 pm ...
To find the reflected vector, you flip the direction of the incoming vector, project it onto the normal, then get the rejection (the difference between the flipped incoming vector and its projection onto the normal) and add that rejection twice to the flipped incoming vector.
This will place you on the reflected position.
...
Do you think the way I did is going to work? Like im not getting the normal from the ball itself, im getting from a invisible ball that has the double radius of the red ball.

Image (the point of contact is the green ball)
I think using the invisible ball with doubled radius is not going to work.
For an obvious example think of a shot where the balls closely miss each other. But with doubled radius they could touch.

Another problem is that the vector is more simplified than the collision handling of Box2D/love.physics.
Box2D simulates things like spin of the balls and friction which can produce different results.
betobala
Prole
Posts: 11
Joined: Sun May 19, 2024 11:37 pm

Re: Help with calculating trajectory

Post by betobala »

knorke wrote: Thu May 30, 2024 11:12 pm
betobala wrote: Thu May 30, 2024 10:40 pm
RNavega wrote: Thu May 30, 2024 8:44 pm ...
To find the reflected vector, you flip the direction of the incoming vector, project it onto the normal, then get the rejection (the difference between the flipped incoming vector and its projection onto the normal) and add that rejection twice to the flipped incoming vector.
This will place you on the reflected position.
...
Do you think the way I did is going to work? Like im not getting the normal from the ball itself, im getting from a invisible ball that has the double radius of the red ball.

Image (the point of contact is the green ball)
I think using the invisible ball with doubled radius is not going to work.
For an obvious example think of a shot where the balls closely miss each other. But with doubled radius they could touch.

Another problem is that the vector is more simplified than the collision handling of Box2D/love.physics.
Box2D simulates things like spin of the balls and friction which can produce different results.
Do you know any possible solutions for that? Im just a beginner with game dev and can not think in any solution
RNavega
Party member
Posts: 385
Joined: Sun Aug 16, 2020 1:28 pm

Re: Help with calculating trajectory

Post by RNavega »

betobala wrote: Thu May 30, 2024 10:40 pm Do you think the way I did is going to work? Like im not getting the normal from the ball itself, im getting from a invisible ball that has the double radius of the red ball.
Knorke's point that Box2D might cause things to behave in a way that differs from your vector prediction is important. Some testing is needed to confirm this.
I think it should work because we're talking about the initial impulses or collisions (the direction in which the balls will start moving), not their final positions.

Your way of finding where the white ball will hit the colorful ball is pretty clever with raycasting that imaginary green circle.

The surface normal in that case can be found by going from that raycast point down to the colorful ball center. It's the same as the surface normal of the colorful ball:
temp2.png
temp2.png (11.74 KiB) Viewed 5296 times
The white ball is coming in by the orange arrow and gets reflected by the normal to become the blue arrow (edit: and the colorful ball moves along the opposite of that surface normal after the white ball hits it, so it's opposite to the teal line).
betobala
Prole
Posts: 11
Joined: Sun May 19, 2024 11:37 pm

Re: Help with calculating trajectory

Post by betobala »

Thanks everyone, I will try to apply this, hope I get it, I have zero experience calculating vetors
betobala
Prole
Posts: 11
Joined: Sun May 19, 2024 11:37 pm

Re: Help with calculating trajectory

Post by betobala »

RNavega wrote: Fri May 31, 2024 12:53 am
betobala wrote: Thu May 30, 2024 10:40 pm Do you think the way I did is going to work? Like im not getting the normal from the ball itself, im getting from a invisible ball that has the double radius of the red ball.
Knorke's point that Box2D might cause things to behave in a way that differs from your vector prediction is important. Some testing is needed to confirm this.
I think it should work because we're talking about the initial impulses or collisions (the direction in which the balls will start moving), not their final positions.

Your way of finding where the white ball will hit the colorful ball is pretty clever with raycasting that imaginary green circle.

The surface normal in that case can be found by going from that raycast point down to the colorful ball center. It's the same as the surface normal of the colorful ball:

temp2.png

The white ball is coming in by the orange arrow and gets reflected by the normal to become the blue arrow (edit: and the colorful ball moves along the opposite of that surface normal after the white ball hits it, so it's opposite to the teal line).
I'm trying to invert the normal line, but i can not do it, do u have any tips to help me calculate that?
Post Reply

Who is online

Users browsing this forum: No registered users and 5 guests