# [Resolved]Reversing Angle Directions

## Recommended Posts

Okay lets say i had a ball moving like this

/

And it hit a vertical wall

How would i go about computing the angle of the ball moving like \ now?

Edit: added a pic of what i want

autoithelp.bmp

Edited by LiveOnTheFly

##### Share on other sites

I think that it has nothing to do with angles.

Usually a ball's movement is treated by two deltas - dx and dy which may go negative or positive and a move delta which is constant so if the ball's right side is hitting the right side of the object then it's dx delta go negative while it's dy delta remains the same.

##### Share on other sites

I ball is rolling hits a object while still continue to roll, lets say it is going 45 degrees from 1,1 the object it hits is a wall on 1,0 (up and down) so no ball can exist on -1,0. with this it hits right at 0,0 and we aren't calculating speed, wind resists or ground resistance then you come to it repelling off at 45 degrees going to 1,-1.

This is really good in pool if you know how to do long shots, pretty much a need for it. A detailed can be seen at www.wimsworld.com/~wbonner/AmeteurPhysicsforPoolPlayers.pdf or http://www.comp.dit.ie/bduggan/Courses/gam...20Reference.doc

0x576520616C6C206469652C206C697665206C69666520617320696620796F75207765726520696E20746865206C617374207365636F6E642E

##### Share on other sites

When I said Ball I meant a sprite of a ball

Right now my code is

```If (\$aSprites[\$i][2] >= \$iGUIHeight - \$iSpriteHeight Or \$aSprites[\$i][2] <= 0) Then
If \$aSprites[\$i][4] > 0 Then
\$aSprites[\$i][4] = \$aSprites[\$i][4] + (360 + \$aSprites[\$i][4])
Else
\$aSprites[\$i][4] = \$aSprites[\$i][4] + (360 - \$aSprites[\$i][4])
End```

It will create an infinite back and forth if it hits two vertical walls in a row.

##### Share on other sites

Bump

Up

My

Post

I tried to fix it myself. I think I just did more damage than good.

Heres my current code:

```If \$aSprites[\$i][5] = 1 Then
If (\$aSprites[\$i][1] >= \$iGUIWidth - \$iSpriteWidth Or \$aSprites[\$i][1] <= 0) Then
\$aSprites[\$i][4] = \$aSprites[\$i][4] + (-(270/2))
EndIf
If (\$aSprites[\$i][2] >= \$iGUIHeight + \$iSpriteHeight or \$aSprites[\$i][2] >= \$iGUIHeight - \$iSpriteHeight Or \$aSprites[\$i][2] <= 0) Then
\$aSprites[\$i][4] = \$aSprites[\$i][4] + 90
if \$aSprites[\$i][2] >= 490 Then
\$aSprites[\$i][2] = \$iGUIHeight - \$iSpriteHeight
Else
\$aSprites[\$i][2] = \$iSpriteHeight
EndIf
;~              MsgBox(0,"","Change")
EndIf
EndIf```

[][1] = x

[][2] = y

[][4] = angle

[][5] = Cannot come out of gui

##### Share on other sites

Just make the appropriate coordinate value negative:

E.g. when an object with a given gradient is moving to the left border then just multiply the x value with -1. Same with the other 3 borders.

Here an example which is not finished yet (30% finished):

I hope this is the thing you are looking for.

UEZ

Edited by UEZ

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

##### Share on other sites

Just make the appropriate coordinate value negative:

E.g. when an object with a given gradient is moving to the left border then just multiply the x value with -1. Same with the other 3 borders.

Here an example which is not finished yet (30% finished):

<snip>

I hope this is the thing you are looking for.

UEZ

lol thats wayy to complicated. Can you just give me the math calculation for calculating the angles?

##### Share on other sites

There is no need of calculation the angle when you just want to check the collision with the border:

```...
If \$coordinates[\$k][0] <= 0 Then;border collision x left
\$coordinates[\$k][0] = 1
\$coordinates[\$k][5] *= -1
ElseIf \$coordinates[\$k][0] >= \$width - \$iWidth Then;border collision x right
\$coordinates[\$k][0] = \$width - (\$iWidth + 1)
\$coordinates[\$k][5] *= -1
EndIf
If \$coordinates[\$k][1] <= 0 Then;border collision y top
\$coordinates[\$k][1] = 1
\$coordinates[\$k][6] *= -1
ElseIf \$coordinates[\$k][1] >= \$height - \$iHeight Then;border collision y bottom
\$coordinates[\$k][1] = \$height - (\$iHeight + 1)
\$coordinates[\$k][6] *= -1
EndIf
...```

\$coordinates[\$k][0] = x coordinate

\$coordinates[\$k][1] = y coordinate

\$coordinates[\$k][5] = slope x

\$coordinates[\$k][6] = slope y

You just negate the appropriate slope depending on the collision border.

slope: m = delta y / delta x

UEZ

Edited by UEZ

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

##### Share on other sites

So you want 180 - \$originalangle

Giggity

##### Share on other sites

So you want 180 - \$originalangle

Nope thsats jus the bouncing back and forth again

Let me try ang clarify more

Imagine a box

in this box we have a circle

the circle bounces around the box

the circle moves by the angle it is pointing at

If the circle hits a wall bounce off of it. EG something that looks like <

How do I calulate the angle needed?

##### Share on other sites

I tried to minimize the code as much as possible:

```#include <GUIConstantsEx.au3>
#include <GDIplus.au3>
#include <WindowsConstants.au3>

Global Const \$width = @DesktopWidth / 2
Global Const \$height = @DesktopHeight / 2

Opt("GUIOnEventMode", 1)
Global \$hwnd = GUICreate("Border Collision!", \$width, \$height, -1, -1, Default, BitOR(\$WS_EX_TOPMOST, \$WS_EX_TOOLWINDOW))
GUISetOnEvent(\$GUI_EVENT_CLOSE, "close")
GUISetState()

_GDIPlus_Startup()
Global \$graphics = _GDIPlus_GraphicsCreateFromHWND(\$hWnd)
Global \$bitmap = _GDIPlus_BitmapCreateFromGraphics(\$width, \$height, \$graphics)
Global \$backbuffer = _GDIPlus_ImageGetGraphicsContext(\$bitmap)
_GDIPlus_GraphicsClear(\$backbuffer)
_GDIPlus_GraphicsSetSmoothingMode(\$backbuffer, 4)
Global \$brush_color
Global \$brush = _GDIPlus_BrushCreateSolid()
_GDIPlus_BrushSetSolidColor(\$brush, \$brush_color)
Global \$max_dots = 15
Global \$max_speed = 10
Global \$iWidth = 60
Global \$iHeight = 60
Dim \$coordinates[\$max_dots][7 + 3 * \$max_dots]
Dim \$brush[\$max_dots]
Initialize()

Do
_GDIPlus_GraphicsClear(\$backbuffer, 0xFF000000)
Draw_Dots()
Calculate_New_Position()
_GDIPlus_GraphicsDrawImageRect(\$graphics, \$bitmap, 0, 0, \$width, \$height)
Sleep(5)
Until False

Func Initialize()
For \$k = 0 To \$max_dots - 1
\$r = Random(64, 255, 1)
\$g = Random(64, 255, 1)
\$b = Random(64, 255, 1)
\$brush_color = "0xAF" & Hex(\$r, 2) & Hex(\$g, 2) & Hex(\$b, 2)
\$brush[\$k] = _GDIPlus_BrushCreateSolid(\$brush_color)
New_Coordinates(\$k)
Next
EndFunc   ;==>Initialize

Func Draw_Dots()
Local \$i, \$temp_x, \$temp_y
For \$i = 0 To \$max_dots - 1
_GDIPlus_GraphicsFillEllipse(\$backbuffer, \$coordinates[\$i][0], \$coordinates[\$i][1], \$iWidth, \$iHeight, \$brush[\$i])
Next
EndFunc   ;==>Draw_Dots

Func New_Coordinates(\$k)
\$coordinates[\$k][0] = Random(\$width / 20, \$width - \$width / 20, 1);start x position
\$coordinates[\$k][1] = Random(\$height / 20, \$height - \$height / 20, 1) ;start y position
\$coordinates[\$k][2] = Random(2, \$max_speed, 1) ;speed of pixel
\$coordinates[\$k][3] = Random(0, \$width, 1) ;new destination x
\$coordinates[\$k][4] = Random(0, \$height, 1);new destination y
\$coordinates[\$k][5] = Random(-7, 7) ;random slope
\$coordinates[\$k][6] = Random(-7, 7)
EndFunc   ;==>New_Coordinates

Func Calculate_New_Position()
Local \$k
For \$k = 0 To \$max_dots - 1
\$coordinates[\$k][0] += \$coordinates[\$k][5] ;increase x coordinate with appropriate slope
\$coordinates[\$k][1] += \$coordinates[\$k][6] ;increase y coordinate with appropriate slope
If \$coordinates[\$k][0] <= 0 Then ;border collision x left
\$coordinates[\$k][0] = 1
\$coordinates[\$k][5] *= -1
ElseIf \$coordinates[\$k][0] >= \$width - \$iWidth Then ;border collision x right
\$coordinates[\$k][0] = \$width - (\$iWidth + 1)
\$coordinates[\$k][5] *= -1
EndIf
If \$coordinates[\$k][1] <= 0 Then ;border collision y top
\$coordinates[\$k][1] = 1
\$coordinates[\$k][6] *= -1
ElseIf \$coordinates[\$k][1] >= \$height - \$iHeight Then ;border collision y bottom
\$coordinates[\$k][1] = \$height - (\$iHeight + 1)
\$coordinates[\$k][6] *= -1
EndIf
Next
EndFunc   ;==>Calculate_New_Position

Func close()
_GDIPlus_BrushDispose(\$brush)
_GDIPlus_BitmapDispose(\$bitmap)
_GDIPlus_GraphicsDispose(\$graphics)
_GDIPlus_GraphicsDispose(\$backbuffer)
_GDIPlus_Shutdown()
Exit
EndFunc   ;==>close

Func _GDIPlus_BrushSetSolidColor(\$hBrush, \$iARGB = 0xFF000000)
Local \$aResult
\$aResult = DllCall(\$ghGDIPDll, "int", "GdipSetSolidFillColor", "hwnd", \$hBrush, "int", \$iARGB)
If @error Then Return SetError(@error, @extended, 0)
Return SetError(\$aResult[0], 0, \$aResult[0] = 0)
EndFunc   ;==>_GDIPlus_BrushSetSolidColor```

I hope this is more clearly!

UEZ

Edited by UEZ

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

##### Share on other sites

I tried to minimize the code as much as possible:

<snip>

I hope this is more clearly!

UEZ

Thanks for trying to help me UEZ but your code doesn't calculate angles which is what i need. I'm creating a sprite engine that moves the sprites based on angles.

##### Share on other sites

If m=Δy / Δx then you can use m = tan(θ), θ = ATan(m). θ is the angle.

UEZ

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

##### Share on other sites

If m=Δy / Δx then you can use m = tan(θ), θ = ATan(m). θ is the angle.

UEZ

Lol i'm only in pre-algebra can you give me some sample code using that? thanks

##### Share on other sites

This function I wrote might help you. This takes your origin coordinates and finds the coordinates of the endpoint at the specified angle and length.

```;x1 = Origin X
;x2 = Origin Y
;Angle = Angle in degrees
;Length = Distance from origin
Func AbsoluteCoordinates(\$x1, \$y1, \$Angle, \$Length)
Local \$Return[2]
\$Return[0] = \$x1 + (\$Length * Cos(\$Angle / 180 * \$PI))
\$Return[1] = \$y1 - (\$Length * Sin(\$Angle / 180 * \$PI))
Return \$Return
EndFunc   ;==>Angle```

##### Share on other sites

This function I wrote might help you. This takes your origin coordinates and finds the coordinates of the endpoint at the specified angle and length.

```;x1 = Origin X
;x2 = Origin Y
;Angle = Angle in degrees
;Length = Distance from origin
Func AbsoluteCoordinates(\$x1, \$y1, \$Angle, \$Length)
Local \$Return[2]
\$Return[0] = \$x1 + (\$Length * Cos(\$Angle / 180 * \$PI))
\$Return[1] = \$y1 - (\$Length * Sin(\$Angle / 180 * \$PI))
Return \$Return
EndFunc   ;==>Angle```
Thanks but that not my problem. I need to calculate the angle of an object after bouncing off of a wall Edited by LiveOnTheFly

##### Share on other sites

Calculating the new angle is easy...

new angle = 360 - old angle

see pic:

untitled.bmp

old angle is 45 degrees

after hitting wall angle is 360 - 45

the new angle is now 315 degrees.

But like said before, it's easier to ignore angles and just deal with the x and y coordinates individually.

e.g. for a ball moving to the right you add 1 to the x coord....once it gets to the wall you start subtracting 1 which now sends it to the left.

What you're trying to do sounds very similar to a project I was working on a while back:

Ice Hockey Game:

http://www.autoitscript.com/forum/index.php?showtopic=89473

click and drag to hit the ball!

code:

```#include <GUIConstantsEx.au3>
#include <Misc.au3>

Opt("GUIOnEventMode", 1)
Opt("MouseCoordMode",2)

Global \$iGraphCount = 0
Global \$iMoveUnit = 4
Global \$m1[2]
Global \$m2[2]
Global \$aPos[3] = ["",200,200]

Global \$dll = DllOpen("user32.dll")

GUICreate("",600,600,Default,Default,Default,0x2000000)
GUISetOnEvent(\$GUI_EVENT_CLOSE,"_Close")

\$hGraphic = GUICtrlCreateGraphic(0,0,600,600)
GUICtrlSetBkColor(-1,0xFFFFFF)

GUISetState(@SW_SHOW)
While 1

\$m2 = \$m1

Sleep(10)

\$m1 = MouseGetPos()

If _IsPressed("01",\$dll) Then

_Clear()

If Sqrt((((\$aPos[1] + \$iRad) - \$m1[0])^2) + (((\$aPos[2] + \$iRad) - \$m1[1])^2)) < (\$iRad * 2) - 1 Then

\$iMoveX = \$m1[0] - \$m2[0]
\$iMoveY = \$m1[1] - \$m2[1]

Do

\$iMoveX *= 0.99
\$iMoveY *= 0.99

\$aPos[1] += \$iMoveX
\$aPos[2] += \$iMoveY

\$m1 = MouseGetPos()

GUICtrlSetGraphic(\$hGraphic,\$GUI_GR_REFRESH)

_Clear()

Sleep(10)

Until Abs(\$iMoveX) < 0.3 And Abs(\$iMoveY) < 0.3

EndIf

GUICtrlSetGraphic(\$hGraphic,\$GUI_GR_REFRESH)

EndIf

WEnd

Func _Clear()

\$iGraphCount += 1

GUICtrlSetGraphic(\$hGraphic,\$GUI_GR_COLOR,0xFFFFFF,0xFFFFFF)
GUICtrlSetGraphic(\$hGraphic,\$GUI_GR_RECT,0,0,600,600)
GUICtrlSetGraphic(\$hGraphic,\$GUI_GR_COLOR,0x000000,\$GUI_GR_NOBKCOLOR)

If \$iGraphCount > 10 Then
GUISetState(@SW_LOCK)
GUICtrlDelete(\$hGraphic)
\$hGraphic = GUICtrlCreateGraphic(0,0,600,600)
GUICtrlSetBkColor(-1,0xFFFFFF)
GUISetState(@SW_UNLOCK)
\$iGraphCount = 0
EndIf

EndFunc

Func _Close()
Exit
EndFunc```

The "reverse angles" bit is dealt with here:

i.e. if the ball is at a wall, make the movement negative

...if the movement is already negative it will make it positive again

Edited by andybiochem

- Table UDF - create simple data tables - Line Graph UDF GDI+ - quickly create simple line graphs with x and y axes (uses GDI+ with double buffer) - Line Graph UDF - quickly create simple line graphs with x and y axes (uses AI native graphic control) - Barcode Generator Code 128 B C - Create the 1/0 code for barcodes. - WebCam as BarCode Reader - use your webcam to read barcodes - Stereograms!!! - make your own stereograms in AutoIT - Ziggurat Gaussian Distribution RNG - generate random numbers based on normal/gaussian distribution - Box-Muller Gaussian Distribution RNG - generate random numbers based on normal/gaussian distribution - Elastic Radio Buttons - faux-gravity effects in AutoIT (from javascript)- Morse Code Generator - Generate morse code by tapping your spacebar!

##### Share on other sites

Okay that works for the left wall. but what about the right? Heres what 45 and 315 look like to autoit(see attachments)

##### Share on other sites

It's midnight here in the UK, so I'm off for a bit of a kip now.

Without looking in detail, I think your problem is with your Angle function.

You're always adding the new co-ordinate to the x pos, and always subtracting from the y position.

This will mean that you will never be able to plot to the left of the central position.

...or (i'm sleepy now!)...

it might be due to your use of Cos and Sin...once you get past 180 degrees this may not work as expected.

Sorry, will look again tomorrow for you.

- Table UDF - create simple data tables - Line Graph UDF GDI+ - quickly create simple line graphs with x and y axes (uses GDI+ with double buffer) - Line Graph UDF - quickly create simple line graphs with x and y axes (uses AI native graphic control) - Barcode Generator Code 128 B C - Create the 1/0 code for barcodes. - WebCam as BarCode Reader - use your webcam to read barcodes - Stereograms!!! - make your own stereograms in AutoIT - Ziggurat Gaussian Distribution RNG - generate random numbers based on normal/gaussian distribution - Box-Muller Gaussian Distribution RNG - generate random numbers based on normal/gaussian distribution - Elastic Radio Buttons - faux-gravity effects in AutoIT (from javascript)- Morse Code Generator - Generate morse code by tapping your spacebar!

##### Share on other sites

Try this:

```;coded by UEZ 2009
#AutoIt3Wrapper_UseUpx=n
#AutoIt3Wrapper_Run_After=upx.exe --best "%out%"
#AutoIt3Wrapper_Run_Obfuscator=y
#Obfuscator_Parameters=/SO
#include <GUIConstantsEx.au3>
#include <GDIplus.au3>
#include <WindowsConstants.au3>

Global Const \$width = @DesktopWidth / 2
Global Const \$height = @DesktopHeight / 2

Opt("GUIOnEventMode", 1)
Global \$hwnd = GUICreate("Border Collision!", \$width, \$height, -1, -1, Default, BitOR(\$WS_EX_TOPMOST, \$WS_EX_TOOLWINDOW))
GUISetOnEvent(\$GUI_EVENT_CLOSE, "close")
GUISetState()

_GDIPlus_Startup()
Global \$graphics = _GDIPlus_GraphicsCreateFromHWND(\$hWnd)
Global \$bitmap = _GDIPlus_BitmapCreateFromGraphics(\$width, \$height, \$graphics)
Global \$backbuffer = _GDIPlus_ImageGetGraphicsContext(\$bitmap)
_GDIPlus_GraphicsClear(\$backbuffer)
_GDIPlus_GraphicsSetSmoothingMode(\$backbuffer, 4)
Global \$brush_color
Global \$brush = _GDIPlus_BrushCreateSolid()
_GDIPlus_BrushSetSolidColor(\$brush, \$brush_color)
Global \$max_dots = 1
Global \$max_speed = 5
Global \$iWidth = 60
Global \$iHeight = 60
Dim \$coordinates[\$max_dots][5], \$angle
Dim \$brush[\$max_dots]
Initialize()

Do
_GDIPlus_GraphicsClear(\$backbuffer, 0xFF000000)
Draw_Dots()
Calculate_New_Position()
_GDIPlus_GraphicsDrawImageRect(\$graphics, \$bitmap, 0, 0, \$width, \$height)
Sleep(5)
Until False

Func Initialize()
For \$k = 0 To \$max_dots - 1
\$r = Random(64, 255, 1)
\$g = Random(64, 255, 1)
\$b = Random(64, 255, 1)
\$brush_color = "0xAF" & Hex(\$r, 2) & Hex(\$g, 2) & Hex(\$b, 2)
\$brush[\$k] = _GDIPlus_BrushCreateSolid(\$brush_color)
New_Coordinates(\$k)
Next
EndFunc   ;==>Initialize

Func Draw_Dots()
Local \$i, \$temp_x, \$temp_y
For \$i = 0 To \$max_dots - 1
_GDIPlus_GraphicsFillEllipse(\$backbuffer, \$coordinates[\$i][0], \$coordinates[\$i][1], \$iWidth, \$iHeight, \$brush[\$i])
Next
EndFunc   ;==>Draw_Dots

Func New_Coordinates(\$k)
\$coordinates[\$k][0] = \$width / 2 ;Random(\$width / 20, \$width - \$width / 20, 1);start x position
\$coordinates[\$k][1] = \$height / 2 ;Random(\$height / 20, \$height - \$height / 20, 1) ;start y position
\$coordinates[\$k][2] = Random(1, \$max_speed, 1) ;speed of pixel
\$angle = Random(0, 359, 1)
ConsoleWrite("Angle: " & \$angle & "°" & @CRLF)
\$coordinates[\$k][3] = \$coordinates[\$k][2] * Cos(\$angle * 3.141592653 / 180)
\$coordinates[\$k][4] = \$coordinates[\$k][2] * Sin(\$angle * 3.141592653 / 180)
EndFunc   ;==>New_Coordinates

Func Calculate_New_Position()
Local \$k
For \$k = 0 To \$max_dots - 1
\$coordinates[\$k][0] += \$coordinates[\$k][3] ;increase x coordinate with appropriate slope
\$coordinates[\$k][1] += \$coordinates[\$k][4] ;increase y coordinate with appropriate slope
If \$coordinates[\$k][0] <= 0 Then ;border collision x left
\$coordinates[\$k][0] = 1
\$coordinates[\$k][3] *= -1
ElseIf \$coordinates[\$k][0] >= \$width - \$iWidth Then ;border collision x right
\$coordinates[\$k][0] = \$width - (\$iWidth + 1)
\$coordinates[\$k][3] *= -1
EndIf
If \$coordinates[\$k][1] <= 0 Then ;border collision y top
\$coordinates[\$k][1] = 1
\$coordinates[\$k][4] *= -1
ElseIf \$coordinates[\$k][1] >= \$height - \$iHeight Then ;border collision y bottom
\$coordinates[\$k][1] = \$height - (\$iHeight + 1)
\$coordinates[\$k][4] *= -1
EndIf
Next
EndFunc   ;==>Calculate_New_Position

Func close()
_GDIPlus_BrushDispose(\$brush)
_GDIPlus_BitmapDispose(\$bitmap)
_GDIPlus_GraphicsDispose(\$graphics)
_GDIPlus_GraphicsDispose(\$backbuffer)
_GDIPlus_Shutdown()
Exit
EndFunc   ;==>close

Func _GDIPlus_BrushSetSolidColor(\$hBrush, \$iARGB = 0xFF000000)
Local \$aResult
\$aResult = DllCall(\$ghGDIPDll, "int", "GdipSetSolidFillColor", "hwnd", \$hBrush, "int", \$iARGB)
If @error Then Return SetError(@error, @extended, 0)
Return SetError(\$aResult[0], 0, \$aResult[0] = 0)
EndFunc   ;==>_GDIPlus_BrushSetSolidColor```

Currently angle 0/360 is not at 12 o'clock but 3 o'clock!

UEZ

Edited by UEZ

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

## Create an account

Register a new account

×

• Wiki

• Back

• Git