2D maths help

BlitzMax Forums/BlitzMax Beginners Area/2D maths help

Jams(Posted 2005) [#1]
Hi, i'm wondering if anyone can help me with a maths problem that's been bugging me, i'm trying to code a kind of black hole, that will suck particles towards the center. But i can't seem to work out the angle between the particles current x,y, and the center of the black hole. Here's some psuedo code to show you what i'm doing wrong...

Method Particle.Update()
  For Dist:Distorter = Eachin Distorters
     Range = Sqr(DX * DX + DY * DY )
     If Range < Dist.Radius
         Dist.ProcessParticle( Particle, Range, DX, DY )
     Endif
  Next
End Method

Method Distorter.ProcessParticle(Particle:Particle, Range, DX, DY)
  Local Strength:Float = 1 - (Range / Dist.Radius )

  'Here's where im stuck... need to calculate the new angle so the particle will face the center of the black hole

  Particle.X = Particle.X + ( NewX * Strength )
  Particle.Y = Particle.Y + ( NewY * Strength )
End Method


Can anyone explain how to fill in the gap?

Any help much appreciated :)


FlameDuck(Posted 2005) [#2]
The angle would be ATan2( dx , dy ) where dx is the difference between the particles x position and the point of origins x position, and dy is the same, for the y axis.

It's a bit easier to manage if you use vectors.
Then the following line:
Local myVector:TVector = TVector.createVector(-x+pocx,-y+pocy)
will create a vector that points at (pocx , pocy) and if you want it to be a "unit" vector, all you have to do is:
myVector.setAngle(myVector.getAngle())