Posted: 29th Aug 2021 2:47
So given the below situation, I want the sprites to meet at the target location, my code works but I don't understand something, at first they were going off in the wrong direction it seemed to be 90 degrees off so I minus the 90 and sure enough they go where I want them to ... but why?



Anyone good at Pythagorean theorem care to explain?

+ Code Snippet
				
sx#=GetSpriteXByOffset(gAtom[atom_index].sprite_id)
sy#=GetSpriteYByOffset(gAtom[atom_index].sprite_id)
				
mx#=gAtom[atom_index].mergeX // target x
my#=gAtom[atom_index].mergeY // target y
				
a#=calculateAngle(sx#, sy#, mx#, my#)-90 // << here, why -90, whats wrong??
SetSpritePhysicsVelocity(gAtom[atom_index].sprite_id, cos(a#)*100, sin(a#)*100)


and the helper functions from UCF project
+ Code Snippet
function calculateDistance(xPosition1#, yPosition1#, xPosition2#, yPosition2#)
	
	distance# = 0
	b# = 0
	c# = 0
	
	if xPosition1# > xPosition2#
		b# = xPosition1# - xPosition2#
	elseif xPosition1# < xPosition2#
		b# = xPosition2# - xPosition1#
	endif

	if yPosition1# > yPosition2#
		c# = yPosition1# - yPosition2#
	elseif yPosition1# < yPosition2#
		c# = yPosition2# - yPosition1#
	endif

   distance# = sqrt((b# * b#) + (c# * c#))

endfunction distance#

function calculateAngle(xPosition1#, yPosition1#, xPosition2#, yPosition2#)

	distance# = calculateDistance(xPosition1#, yPosition1#, xPosition2#, yPosition2#)
	angle# = 0
	c# = 0
	
	if yPosition1# > yPosition2#
		c# = yPosition1# - yPosition2#
	elseif yPosition1# < yPosition2#
		c# = yPosition2# - yPosition1#
	endif
	
	if distance# > 0
		angle# = ASin(c# / distance#)
	endif

   if xPosition1# = xPosition2# and yPosition1# < yPosition2#
		   angle# = 180
   elseif xPosition1# = xPosition2# and yPosition1# > yPosition2#
		   angle# = 0
   elseif xPosition1# < xPosition2# and yPosition1# = yPosition2#
		   angle# = 90
   elseif xPosition1# > xPosition2# and yPosition1# = yPosition2#
		   angle# = 270
   elseif xPosition1# > xPosition2# and yPosition1# < yPosition2#
		   angle# = 270 - angle#
   elseif xPosition1# < xPosition2# and yPosition1# < yPosition2#
		   angle# = angle# + 90
   elseif xPosition1# > xPosition2# and yPosition1# > yPosition2#
		   angle# = angle# + 270
   elseif xPosition1# < xPosition2# and yPosition1# > yPosition2#
		   angle# = 90 - angle#
   endif

endfunction angle#


Is it my code that's wrong or the helper functions, I thought I had a grip on the basics of pythag so I want to understand what's up here, yes it works but superglue and duct tape is not my style, not without some WD40 andway .. lol (engineers joke, if it moves and it shouldn't, duct tape, is it doesn't move and it should, WD40)
Posted: 29th Aug 2021 5:03
does THIS help?

specifically:
+ Code Snippet
If GetPointerState() = 1 
	StartX# = GetPointerX()	:	StartY# = GetPointerY()
	Power# = 0.0
	
	If StrikerValid(StartX#, StartY#) = 1

		Repeat
			MX# = GetPointerX()	:	MY# = GetPointerY()
			
			If Power# < MaxPower# then Power# = Power# + 2.0 
			DrawLine(StartX#,StartY#,MX#,MY#, Red, Red)
			Angle# = ATanFull(StartX#-MX#, StartY#-MY#)
			Print(STR(Angle#) + " @ " + STR(Power#) )
			Sync()
		Until GetPointerState() = 0
		
		SetSpritePhysicsVelocity(Striker, SIN(Angle#)*Power#*20.0, -COS(Angle#)*Power#*20.0 )
	Else
	
	EndIf
	
EndIf
Posted: 29th Aug 2021 5:14
Yes it does, the ATanFull is the missing link, thanks for that ... I'll work that into my code on tomorrow's epic coding shift lol
Posted: 29th Aug 2021 13:54
You can simplify your distance function a lot. You can lose all of those if conditions because the result will be the same.
The distance between two points is the same regardless of their position relative to each other.
Try this link
Posted: 29th Aug 2021 16:10
Yeah I actually got your simpler function in my Utils lib but when my code did not work I started scrambling around looking for solutions and google pointed me to the UCF project and some pythag that went straight over my head, I went with the UCF but I knew it was all wrong hence the post, funny thing my original attempt was almost right lol

Thanks for the help though, I actually use that function quite a lot.
Posted: 30th Aug 2021 16:47
A bit of detail on how atanfull can be calculated:

https://forum.thegamecreators.com/thread/27159


Additionally, I might have some stuff here you may find useful: (don't judge harshly, the site started in the DBC days!)
http://zimnox.com/dbcc/?page=eq
Posted: 30th Aug 2021 18:03
don't judge harshly


Ouch, my eyes!! lol

Thanks for that looks like a wealth of information there, will give it a good look over for sure.