Math: Returning X,Y which is 150pixels @45degrees

BlitzMax Forums/BlitzMax Programming/Math: Returning X,Y which is 150pixels @45degrees

Hi all

I'd like to know how to calculate the X, Y endpoint value which is nn-degrees from your current X,Y:

StartX=140
StartY=340
TargetAngle: 136 degrees.
DistanceToShoot: 235 pixels.
using a circle a

What will the X, Y value be?
TIA!

Is that what you need?

TargetX = Sin(TargetAngle) * TargetDistance
TargetY = -Cos(TargetAngle) * TargetDistance

Fetze:
You have that backwards. Might work, but this is the more traditional formula:

X# = Radius# * Cos(Angle#)
Y# = Radius# * Sin(Angle#)

With that, angle 0 = right and angle 90 = down. So it goes clockwise from 3 o' clock.

Unless you're doing 2D graphics in which case the Y axis is inverted, (positive is down instead of up) and so it goes counterclockwise instead.

Or maybe I have that backwards. I think I might. Maybe it goes clockwise in 2D and counterclockwise in 3D.... Yeah I think that's what it does. So 90 would be straight up.

I think yours is backward sswift, I would call 0 right and 90 up. and workit anti clockwise

Each to their own i suppose

On a 2D screen, 90 degrees will point down because the Y axis is inverted from standard euler coordinates. When dealing with 3D though, 90 degrees will point up.

So if X = 50 and Y = 50 and you wanted to travel 100 pixels at a 45 degree angle, you'd end up at X = 121 and y = 121 (after rounding) which is below and to the right of the previous position.

@sswift
It works. Maybe not like you want your angle, but it works ^^

0° --> up
90° --> right
180° --> down
270 --> left

(On screen)

For the most part, any combination of sin and cosine will work - it's just that the information you get will be relative to the method you use to get it.

On the other hand, I'm sure there is a recommended standard. I'm assuming that cosine is used for the horizontal factor and sin for the vertical?

BTW, i am doing this in 2D from a "top-down" view, no fancy physics or such stuff.

TIA to everyone who contributes. BTW, isnt there in the Maths forum about the problem?

pappavis, hasn't Fetze answered this?
Did the suggestion work? That's what I would use.

@Fetze: Yes, it works that way, but try converting an x,y value to an angle and rotation. The function Atan2(y,x) assumes you're using standard cartesian and polar coordinate systems inverted on the Y axis, which would be 0 right and 90 down. For consistency sake, it is best to use the same throughout.
So from polar to cartesian"
X = Cos(Angle#)*Distance#
Y = Sin(Angle#)*Distance#

and from cartesian to polar
Angle# = Atan2(Y,X) 'Note that Y comes first
Distance# = Sqr(X*X+Y*Y)


Thanks to all who contributed to my question. The following code was the endresult.. Dont mind the c#-style comments.

Look at the function GetXYforTargetAtAngle() down below in the code.


Type clsLijn
    '/// <summary>
    '/// Berken x/y koordinate van 'n doel, starten met jou huidige X/Y.
    '/// </summary>
    '/// <param name="StartX">Die gradehoek waarheen jy wil skiet</param>
    '/// <param name="StartY"></param>
    '/// <param name="intDistance"></param>
    '/// <param name="dblTargetAngle"></param>
    '/// <returns>een objek met x, y koordinaaten</returns>
	Function GetXYforTargetAtAngle:clsLijnPos(StartX:Int, StartY:Int, intDistance:Int, dblTargetAngle:Int)
	
		Local objXY1:clsLijnPos = New clsLijnPos;
		objXY1 = clsLijnPos.Create();
		
		'//normailseer die gradehoek, met cos en tan word die gradehoek oomgebuigd.
		'//dblTargetAngle:-45;
		
		If(dblTargetAngle < 45)
		EndIf
		
		Local dblRadians:Double = Self.GetNormaliseerNaarWareNoord(dblTargetAngle);	'// normailseer naar ware noord in dit geval
		Local dblX:Int = StartX + Int(intDistance * Sin(dblRadians * (Pi / 180)));
		Local dblY:Int = StartY + Int(intDistance * Cos(dblRadians * (Pi / 180)));
		
		objXY1.x = dblX;
		objXY1.y = dblY;
		
		Return objXY1;
	End Function
EndType

Type clsLijnPos
	Field x:Int;
	Field y:Int;
	Field IsNegativeSlope:Byte;
	
	Function Create:clsLijnPos()
		Local objLPos1:clsLijnPos = New clsLijnPos;
		objLPos1.x = 0;
		objLPos1.y = 0;
		objLPos1.IsNegativeSlope = False;
		
		Return objLPos1;
	End Function

	Function GetNormaliseerNaarWareNoord:Double(dblTargetAngle:Int)

		Local dblRadians:Double = 0;

		Try
			dblRadians = (dblTargetAngle - 360) * (Pi / 180);	'// normailseer naar ware noord in dit geval
		Catch Err1:Object
			Throw(Err1.ToString());
		EndTry
		
		Return dblRadians;
		
	EndFunction
EndType