View unanswered posts | View active topics It is currently Fri Dec 27, 2024 7:53 am



Reply to topic  [ 15 posts ] 
 Getting an angle from 2 points? 
Author Message
User avatar

Joined: Tue Jun 12, 2007 11:52 pm
Posts: 13144
Location: Here
Reply with quote
Post 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


Mon Nov 09, 2009 10:59 pm
Profile
REAL AMERICAN HERO
User avatar

Joined: Sat Jan 27, 2007 10:25 pm
Posts: 5655
Reply with quote
Post 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.


Tue Nov 10, 2009 3:16 am
Profile

Joined: Sat Feb 03, 2007 7:11 pm
Posts: 1496
Reply with quote
Post 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.


Tue Nov 10, 2009 4:15 am
Profile WWW
User avatar

Joined: Tue Dec 12, 2006 3:10 pm
Posts: 495
Location: Uncertain quantum state
Reply with quote
Post Re: Getting an angle from 2 points?
Wait how does the trig stuff differ from (pos1-pos2).AbsRadAngle?


Tue Nov 10, 2009 2:03 pm
Profile
User avatar

Joined: Mon Jul 16, 2007 9:50 am
Posts: 1512
Location: Tallahassee, FL
Reply with quote
Post 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.


Tue Nov 10, 2009 3:04 pm
Profile YIM
DRL Developer
DRL Developer

Joined: Tue Aug 11, 2009 5:09 am
Posts: 395
Reply with quote
Post 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.


Last edited by Abdul Alhazred on Thu Nov 12, 2009 2:41 pm, edited 1 time in total.



Tue Nov 10, 2009 7:02 pm
Profile
User avatar

Joined: Mon Jul 16, 2007 9:50 am
Posts: 1512
Location: Tallahassee, FL
Reply with quote
Post 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...


Wed Nov 11, 2009 3:32 pm
Profile YIM
User avatar

Joined: Sun Jul 13, 2008 9:57 am
Posts: 4886
Location: some compy
Reply with quote
Post 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.


Wed Nov 11, 2009 9:18 pm
Profile WWW
DRL Developer
DRL Developer

Joined: Tue Aug 11, 2009 5:09 am
Posts: 395
Reply with quote
Post 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.


Thu Nov 12, 2009 2:40 pm
Profile
User avatar

Joined: Mon Jul 16, 2007 9:50 am
Posts: 1512
Location: Tallahassee, FL
Reply with quote
Post 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.


Thu Nov 12, 2009 7:07 pm
Profile YIM
DRL Developer
DRL Developer
User avatar

Joined: Wed Dec 13, 2006 5:27 am
Posts: 3138
Location: A little south and a lot west of Moscow
Reply with quote
Post 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).


Fri Nov 13, 2009 1:35 am
Profile WWW
User avatar

Joined: Tue Jun 12, 2007 11:52 pm
Posts: 13144
Location: Here
Reply with quote
Post 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.


Sat Nov 14, 2009 5:17 am
Profile
User avatar

Joined: Sun Jul 13, 2008 9:57 am
Posts: 4886
Location: some compy
Reply with quote
Post 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.


Sat Nov 14, 2009 6:43 am
Profile WWW
User avatar

Joined: Tue Jun 12, 2007 11:52 pm
Posts: 13144
Location: Here
Reply with quote
Post Re: Getting an angle from 2 points?
Now I get this:

Attachment:
ScreenDump000.bmp
ScreenDump000.bmp [ 47.3 KiB | Viewed 4031 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);


Sat Nov 14, 2009 5:56 pm
Profile
User avatar

Joined: Sun Jul 13, 2008 9:57 am
Posts: 4886
Location: some compy
Reply with quote
Post Re: Getting an angle from 2 points?
check if your rays are working the way you want them to by printing the results.


Sun Nov 15, 2009 12:57 am
Profile WWW
Display posts from previous:  Sort by  
Reply to topic   [ 15 posts ] 

Who is online

Users browsing this forum: No registered users


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by STSoftware for PTF.
[ Time : 0.062s | 15 Queries | GZIP : Off ]