Data Realms Fan Forums
http://45.55.195.193/

Getting an angle from 2 points?
http://45.55.195.193/viewtopic.php?f=73&t=17093
Page 1 of 1

Author:  CaveCricket48 [ Mon Nov 09, 2009 10:59 pm ]
Post subject:  Getting an angle from 2 points?

I'm trying to make a sphere accurately bounce with Lua. Currently, it'll work half of the time, while the other half it follows the same path before it struck an object backwards.

The way I have this set up is that the orb casts 2 StrengthRays: one above it and one below it (The rays' position rotate with the orb's direction of velocity), and the rays' direction is the orbs' velocity direction. When both rays strike a terrain pixel, a new vector is created with the closest struck terrain pixel being the vector start. Then the orb's current velocity is added to the last vector to get the bounce direction.

Bounce code snippet:
Code:
   if SceneMan:CastStrengthRay((Vector(self.Pos.X,self.Pos.Y-3):AbsRotateTo(self.Vel)):Perpendicularize(),Vector(self.Vel.X,self.Vel.Y):SetMagnitude(20),0,self.anglechecka,0,0,true) and SceneMan:CastStrengthRay((Vector(self.Pos.X,self.Pos.Y+3):AbsRotateTo(self.Vel)):Perpendicularize(),Vector(self.Vel.X,self.Vel.Y):SetMagnitude(20),0,self.anglecheckb,0,0,true) then

    if math.sqrt((self.anglechecka.X-self.Pos.X) ^ 2 + (self.anglechecka.X-self.Pos.X) ^ 2) > math.sqrt((self.anglecheckb.X-self.Pos.X) ^ 2 + (self.anglecheckb.X-self.Pos.X) ^ 2) then

   self.determinedvel = Vector(self.anglecheckb.X-self.anglechecka.X,self.anglecheckb.Y-self.anglechecka.Y);
   self.determinedvel2 = self.determinedvel:AbsRotateTo(self.determinedvel+self.Vel);
   self.Vel = self.determinedvel2:SetMagnitude(self.ballspeed);

    elseif math.sqrt((self.anglechecka.X-self.Pos.X) ^ 2 + (self.anglechecka.X-self.Pos.X) ^ 2) < math.sqrt((self.anglecheckb.X-self.Pos.X) ^ 2 + (self.anglecheckb.X-self.Pos.X) ^ 2) then

   self.determinedvel = Vector(self.anglechecka.X-self.anglecheckb.X,self.anglechecka.Y-self.anglecheckb.Y);
   self.determinedvel2 = self.determinedvel:AbsRotateTo(self.determinedvel+self.Vel);
   self.Vel = self.determinedvel2:SetMagnitude(self.ballspeed);
   end
end

Author:  Grif [ Tue Nov 10, 2009 3:16 am ]
Post subject:  Re: Getting an angle from 2 points?

By your topic title I'd suggest math.atan2, because I'm too lazy to decipher your incredibly densely packed code. Seriously, expand it out some, dude.

Author:  zalo [ Tue Nov 10, 2009 4:15 am ]
Post subject:  Re: Getting an angle from 2 points?

TLB is telling me there's a function for that in his orbit lands. Feel free to use it.

Author:  findude [ Tue Nov 10, 2009 2:03 pm ]
Post subject:  Re: Getting an angle from 2 points?

Wait how does the trig stuff differ from (pos1-pos2).AbsRadAngle?

Author:  Darlos9D [ Tue Nov 10, 2009 3:04 pm ]
Post subject:  Re: Getting an angle from 2 points?

math.atan2 is definitely your friend.
Of course, first you need to find the line between the two points, which is easy. Then you put it into math.atan2 as math.atan2(-Y, X). This should give you back the proper angle. The reason Y is negated is because in CC +Y is down, whereas lua's math functions assume +Y is up.

Author:  Abdul Alhazred [ Tue Nov 10, 2009 7:02 pm ]
Post subject:  Re: Getting an angle from 2 points?

I would actually advise against using atan2. IMHO using the functions provided by the API result in cleaner code, with many other advantages. Example:
Code:
local Range = SceneMan:ShortestDistance(self.Pos, Target.Pos, false)  -- create a distance vector that takes x-wrapping in to consideration
local angle = Range.AbsRadAngle  -- the angle between self.Pos and Target.Pos
local distance = Range.Magnitude  -- the distance between self.Pos and Target.Pos


Edit: Used 'SceneMan.SceneWrapsX' in an incorrect way.

Author:  Darlos9D [ Wed Nov 11, 2009 3:32 pm ]
Post subject:  Re: Getting an angle from 2 points?

I swear to god I tried using ShortestDistance() and it failed to scene wrap.

AbsRadAngle/AbsDegAngle and Magnitude variables on Vectors are, sadly, news to me. Sigh...

Author:  Geti [ Wed Nov 11, 2009 9:18 pm ]
Post subject:  Re: Getting an angle from 2 points?

you can give an argument in ShortestDistance() that allows scene wrapping. i think its the last one, and its boolean.

Author:  Abdul Alhazred [ Thu Nov 12, 2009 2:40 pm ]
Post subject:  Re: Getting an angle from 2 points?

Darlos9D wrote:
I swear to god I tried using ShortestDistance() and it failed to scene wrap.
I actually tested it to make absolutely sure that it works as expected, and yes it does. This was not a complete waste of time since I also noticed that the argument description of the third argument says "Whether to check if the passed in points are outside the scene, and to wrap them if they are.", meaning that the use of 'SceneMan.SceneWrapsX' that my post above previously had was wrong.

Author:  Darlos9D [ Thu Nov 12, 2009 7:07 pm ]
Post subject:  Re: Getting an angle from 2 points?

Well, it's right in a way I think, but only if you're only interested in X wrapping. I mean, if the scene doesn't actually wrap and a point is outside of the scene is checked, wouldn't checking as if it were wrapped be bad?

Of course, maybe the function handles this on its own.

Author:  TheLastBanana [ Fri Nov 13, 2009 1:35 am ]
Post subject:  Re: Getting an angle from 2 points?

To deal with that, you would just set the argument for whether to check or not to (SceneMan.SceneWrapsX or SceneMan.SceneWrapsY).

Author:  CaveCricket48 [ Sat Nov 14, 2009 5:17 am ]
Post subject:  Re: Getting an angle from 2 points?

I'm using math.atan2 for simplicity, and I'm doing something wrong.

Code:
   self.bouncecheck = Vector(0,0);
   if SceneMan:CastStrengthRay(Vector(self.Pos.X,self.Pos.Y),Vector(self.Vel.X,self.Vel.Y):SetMagnitude(10),0,self.bouncecheck,0,0,true) then
   self.Vel = Vector(self.Vel.X,self.Vel.Y):RadRotate(math.atan2(self.bouncecheck.Y*-1,self.bouncecheck.X));


This makes the object bounce in strange directions.

Author:  Geti [ Sat Nov 14, 2009 6:43 am ]
Post subject:  Re: Getting an angle from 2 points?

so, uh, whats that complex about
Code:
angle = SceneManager:ShortestDistance(vector1, vector2, true).AbsRadAngle
? that way you can be sure you arent ♥♥♥♥ something up with the angle.

Author:  CaveCricket48 [ Sat Nov 14, 2009 5:56 pm ]
Post subject:  Re: Getting an angle from 2 points?

Now I get this:

Attachment:
ScreenDump000.bmp
ScreenDump000.bmp [ 47.3 KiB | Viewed 4570 times ]


And I have this:
Code:
   self.bouncecheckA = Vector(0,0);
   self.bouncecheckB = Vector(0,0);
   if SceneMan:CastStrengthRay(Vector(self.Pos.X,self.Pos.Y-3):AbsRotateTo(self.Vel),Vector(self.Vel.X,self.Vel.Y):SetMagnitude(10),0,self.bouncecheckA,0,0,true) and SceneMan:CastStrengthRay(Vector(self.Pos.X,self.Pos.Y+3):AbsRotateTo(self.Vel),Vector(self.Vel.X,self.Vel.Y):SetMagnitude(10),0,self.bouncecheckB,0,0,true) then
   angle = SceneManager:ShortestDistance(self.bouncecheckA,self.bouncecheckB,true).AbsRadAngle;
   self.Vel = Vector(self.Vel.X,self.Vel.Y):RadRotate(angle);

Author:  Geti [ Sun Nov 15, 2009 12:57 am ]
Post subject:  Re: Getting an angle from 2 points?

check if your rays are working the way you want them to by printing the results.

Page 1 of 1 All times are UTC [ DST ]
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/