We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Dear all,
I know when we have a reflection on a wall it's xAdd = xAdd * -1;
etc.
but my projectile / missile has no xadd / yadd but an angle it is follwing (45°, 273° etc.)
What is the formula to get a new angle from the old angle after an reflection please?
Or how many degrees do I have to turn / rotate my missile?
Thanks!
Best, Chrisir ;-)
Answers
I'm really not sure what you're asking. Can you post an MCVE? (NOT your whole sketch, just a small example that demonstrates what you're talking about.)
no, I'm just asking for the formula. A mcve makes no sense here.
When a missile is flying northeast, angle is 45°. When it bounces on a wall in south-north-direction, the new angle is 315° (flying Nortwest now)
Thank you!
;-)
Well, in that case you just subtract 90 degrees, right? If you're just talking about horizontal and vertical walls, then that's all you ever do. In fact, you don't need to consider angles at all- just reverse the X or Y speed.
If you're talking about more complicated scenes with arbitrary lines at any angle, then all you need to do is reflect the missile. Google "angle of reflection" for a bunch of results.
If you can't get that working, then I suggest posting an MCVE. After all, you should make it as easy as possible for people to help you, right?
First of all your angles are wrong since on computers the positive y direction is down. So east = 0°, south = 90°, west = 180° and north = 270° using these values mean the trig functions e.g. sin(), cos() etc work correctly with the screen coordinate system.
If you are only interested in reflections against vertical and horizontal boundaries then this works.
Personally I would avoid angles altogether and use vectors for the direction of travel and also for the start and end of the barrier. Then calculate the reflected vector.
@KevinWorkman: I think you are right about mcve... thank you!
@quarks: Thank you, I will have a look at it!
I appreciate your comments and your help, from both of you.
Thank you!
Best, Chrisir ;-)
@quark: Is there a solution when I don't know if the line my missile collides with is horizontal or vertical?
Is it just
Thank you...
Chrisir ;-)
@Chrisir: the book link I posted in this thread may be of interest. Scroll down to 'bouncing off an angle'. The code is actionscript, but the maths is the same and it's a good explanation (and a well written book in general) ;)
@_vk - edited again to fix links. Editing from my PC: previous attempts to link from tablet didn't go so well :/
@blindfish the link in your post is linked to this very thread... (I'm intereted in the book:) some nasty bug I think look what I got:
http://forum.processing.org/two/discussion/10649/forum.processing.org/two/discussion/comment/41640/forum.processing.org/two/discussion/comment/41640/forum.processing.org/two/discussion/comment/41640/forum.processing.org/two/discussion/comment/41640/#Comment_41640
The answer is no.
To know the reflection angle requires two things
1) The orientation of the wall
2) the angle of incidence
As I said before I would avoid working with angles, they are not needed. In my AI for Games library there is not an angle to be seen.
A barrier is specified by the end points i.e. start and end
The missile is represented by a position and a vector which defines the direction of movement and the magnitude of the vector defines its speed of movement. This vector is the velocity of the missile.
If the velocity vector intersects the barrier (line intersection calculation)
1) then you calculate the surface normal for the barrier
2) Use the surface normal vector and the missile velocity vector to calculate the reflected velocity vector.
Interesting question. The answers from Quark seem correct as far as they go.
I found this http://www.mightydrake.com/Articles/ricochet.htm which I think adds to the answer.
Going beyond that;
You may want to consider the axial rotation of your ‘missile’, and the rotation/movement of the ‘wall’ if any.
A non-winged missile will have some amount/degree of rotation for stability. A winged missile will have a rotational bias once the wings fall off.
Think billiards, where the ‘English’ imparted to the cue ball affects its travel after it hits the object ball.
And just for fun, you should also consider the rotational kinetic energy of your projectile relative to the initial frame of the ‘wall’. For instance, if the projectile is flying true to the planets axis of rotation or poles, the combined rotational affect is greater flying south assuming a clockwise rotation in the projectile. Of course, all this is influenced by distance from the pole of the launch point and the target area. At the equator the planetary rotational influence is effectively zero though the distance traveled changes from east to west.
Also, for further torment, consider the elasticity of the ’wall’. Will the wall deform in a symmetrical and radial way? Or will it deform along inherent faults in an unknowable or otherwise random way?
If you write code to address all of this, I’d love to see it.
i disagree with quark
imagine you sit in a car and hit a wall and get reflected, say 60 degree, it doesn't matter if it is vert or horiz; your relative rotation is the same
Either way the car would be crushed :/
What you describe is a 3d environment with the car travelling on the z axis and a wall on the xy plane. What quark describes is a 2d calculation. I'm not awake enough to explain the maths involved but the book I linked to above has a good section on trig and maths. You should be able to read it online ;)
If the wall is in the XY plane and the car moves in the Z direction (i.e. like a rocket) then there is no collision unless there is a ceiling ;)
I believe we are discussing a 2D solution.
I have no problems with you disagreeing with me but unfortunately you are wrong. In this picture the black line is the wall the red line is the car moving and the green line is the rebound.
Despite the fact that the car is traveling in the same direction in both cases the ABSOLUTE and the RELATIVE angle is different in both cases.
I see
@quark: maybe I was being dense this morning, but I thought the z axis was movement to/away from you; at least in the processing environment? Anyway your diagram clearly describes the logic involved :)
thats true but changing the z value with a 2D renderer has no visual movement
Anyway I thought I would do an example showing how you could use vectors to represent the direction a ball moves and vectors to represent the walls.
You will notice that the balls can 'escape' at the corners because the reflection is so close to the end of the line the reflected position ends up outside of the shape.
Obviously I have used classes to represent walls and balls to make the program adaptable for other scenarios.
if I find time I make
a mcve
@Chrisir, your example with the car can happen in the real world because real objects have rotation. It is possible to add rotation to these kinds of equations but then they become quite complicated
Edit: Just realized OLAM gave the same example below with billiards... opps
As an example of the difference, a tennis player can serve the ball by just hitting it or but moving their racquet while they are hitting it causing the ball to spin. In the first case when the ball hits the ground and it has little or no spin then it follows the equations Quark is talking about. In the second case when the ball is spinning when it hits the ground modeling its movement is more difficult
asimes is correct, my code does not take into account angular momentum (i.e. ball spin) although you can play with the elasticity and friction of the wall :)
BTW if you only have vertical and horizontal barriers and want to use angles then use my original equations. There is nothing wrong with that you just have to know which type of barrier you are rebounding off.
In fact the vector solution maybe more complicated than you need :)
this is my mcve if you like
or see this as my mcve, it does really bounce
@quark could you please look at my mcve for this...
thank you...
;-)
Just looking at your latest MCVE but I am not sure what I am looking for :-?
If you are implementing turtle graphics (as in LOGO) surely you don't need reflection. :-?
well, this is part of a bigger project.
and yes, I wanted to implement a reflection into turtle graphics (as in LOGO).
Just as you can see from the mcve, how can the turtle be reflected from the wall as if it were a ball?
thes means we have to use its angle and the rotation RE and LI commands.
thank you!
Chrisir ;-)
Ok but is this what you expected?
that's what my latest mcve is doing, right?
No, that's not what I expect.
That's the wrong behaviour I have now and which I want to improve.
That's why I came here and ask this.
The mcve just shows that I use an angle (and want to stich with it) and not PVectors or speedX,speedY and so... and that I want to make a good reflection in the frame of that mcve.
;-)
The clue was in my first post which did the reflection angles. Need to modify it because it used the incident angle rather than the direction of travel. After removing unused code we get.