HomeUser Control Panel (unavailable in archive)ForumsTutorialsArt GalleryResourcesMaps

A Tangent function

09-15-2006, 03:05 PM#1
The)TideHunter(
Ok, i was advised to ask here for a tangent function, i need it, but i dident really know what one was called until somebody told me.

The scenario:

A ball is incoming towards you, the angle is x, then the ball hits you at y, but y is a small ring around you which is 3d, and can hit at any part.
Im not good at explaining, so i did a pic on paint.

Zoom (requires log in)

Now, when the arrow hits the ball, it needs to fire off in the right direction.
Question: how do i find out the angle it fires too? this is where i was told i need a tangent function.

Thanks in advance.

EDIT: Question was wrong, it wasent the angle it fires from but the angle it fires too, the course it will take after its hit the ball.
Attached Images
File type: jpgExample.JPG (7.8 KB)
09-15-2006, 07:08 PM#2
Naakaloh
Arctangent actually.

Atan(yVel/xVel) will give you the direction of the arrow in radians.
09-15-2006, 07:22 PM#3
The)TideHunter(
I dont understand, could you explain further?
If its towards the angle before hitting, then im sorry i said the question wrong.
09-15-2006, 07:47 PM#4
PipeDream
So you have a person/circle who is much more massive than a ball/circle of finite size? All 2d I assume.
If the person is immobile and the collision is lossless, then the velocity of the ball will be reflected across the line going from the center of one circle to the center of the other. The easiest way to do this is rotate your velocity vector into the frame of normal/tangential to the collision surface, negate the normal component, then rotate back. This is just a similarity transform, so you can multiply out the transformation to save some ops and probably find some other nice explanation for what is going on.
Take t to be the angle between the y axis and the line between the two circles, or pi/2+atan2(r2y-r1y,r2x-r1x).

{vx_after,vf_after} = {{cos(t),-sin(t)},{sin(t),cos(t)}} . {{1,0},{0,-1}} . {{cos(t),sin(t)},{-sin(t),cos(t)}} . {vx_before, vy_before}

So multiply out those three matrices and you'll have a nice expression for before to after.

Edit: Oops, I meant negate normal component, not tangential. Matrices are still OK, since I also mixed up which was which there.

To below: No, you're completely right too. When you multiply out the matrices you end up with a rotation by twice the angle from the normal which is another way of saying take the difference and add it to the other side. It's easy to see if you use complex arithmetic instead of matrices.
09-15-2006, 07:48 PM#5
Naakaloh
Arctangent is the inverse function of the tangent function.

The tangent function takes an angle and returns the ratio of the y and x values.

If you take the arctangent of the ratio of the y and x values, in this case the y and x velocities of the arrow, you will get the angle, or direction of the arrow.

Once you determine the angle it's travelling you can assume that an axis along the angle from the center of the object to the point of impact is the normal to the tangent of the point of impact. According to the law of reflection, the reflected angle is the same from the normal as the incident angle to the normal.

Click image for larger version

Name:	reflection example.JPG
Views:	19
Size:	3.7 KB
ID:	12524

So, determine the difference between the normal and the incidence angle and add or subtract that from the normal depending on the direction and you should be fine.

Edit: Heh, maybe you should just listen to PipeDream... he know's more about vectors than I...
Attached Images
File type: jpgreflection example.JPG (3.7 KB)
09-15-2006, 08:00 PM#6
The)TideHunter(
Pipe, thanks alot, but unfortunatly, i dident understand half of that, my young maths skills havent advanced that far.
Naak, could you give me a example?
Maybe a function that returns it, im not sure about velocity and how to get its x/y, and how to implement a function that would do that.

I thought, because its a round circle, its not always reflected on the incoming angle, because depending on where it hits, and the angle between the arrow and the circle effects the outgoing arrow.
I wasent sure =/.

EDIT: To give me infomation, basically theirs going to be ball's moving on the floor, at a set speed, they move in straight lines, until they hit something and bounce somewhere else, but im just not sure about how to get the new angle.
09-15-2006, 09:09 PM#7
Naakaloh
The law of reflection will be true regardless of the axes, you just need to be sure that you're using the correct axis for the normal. In the case of a perfect circle, the axis along the angle from the center of the circle to the point of impact should be the normal for the tangent to the point of impact.

Edit: Here's a function that I think should demonstrate what I mean. I'm just giving all the variables in the parameters, I don't know how you plan to handle it. I think this should work, the I'm not certain how if Atan returns value in degrees or radians.

Collapse JASS:
function GetAngleOfReflection takes real ballXVel, real ballYVel, real impactX, real impactY, real centerX, real centerY returns real

        local real ballDirection = Atan(ballYVel / ballXVel)
        local real normalDirection = Atan( (impactX - centerX) / (impactY - centerY) )
        local real incidenceAngle = normalDirection - ballDirection
        local real reflectedAngle = normalDirection + incidenceAngle

        return reflectedAngle

endfunction
09-17-2006, 04:55 PM#8
The)TideHunter(
Sorry for the late reply, i'v been away since friday night.
Thanks for the function, i'll test it in a moment, but one more question, what are the variables ballXVel, ballYVel, impactX, impactY, centerX and centerY.
I can guess impactX/Y but im not sure about the Vel and centers.
09-17-2006, 04:58 PM#9
Naakaloh
X and Y velocity components of the object that's flying to the circle.

X and Y values of the center of the circle.
09-18-2006, 03:18 PM#10
The)TideHunter(
Velocity as in the objects speed?
09-18-2006, 03:21 PM#11
MaD[Lion]
looks liek someone is trying to make pinball
09-18-2006, 03:22 PM#12
The)TideHunter(
Quote:
Originally Posted by MaD[Lion]
looks liek someone is trying to make pinball

Not quite :), but nearly.
09-18-2006, 04:09 PM#13
Naakaloh
Quote:
Velocity as in the objects speed?

Yes and no, for all considerations in this situation you can treat it as speed since you're handling direction as a separate issue.
09-18-2006, 05:11 PM#14
The)TideHunter(
Thanks so much for this help.
I'v added rep to you and Pipe.