Page 1 of 1
Find angle to object relative to direction bot is facing?
Posted: Wed Feb 06, 2013 10:22 am
by noobbotter
I'm trying to figure out how to determine the angle to an object relative to the direction my character is facing. For example, if my character is looking Due East, and the object I want an angle of is due South, it should return 90 degrees. If I'm looking Southeast and the object is Northwest, it should return 180 degrees. I'm not sure how I would go about doing that. I'm doing this in a userfunction, just in case that makes a difference.
I'm sure it's something similar to this part of a post I found but have no idea how to implement it, or how to make it work according to the relative direction my character is facing.
angle = atan2(y2-y1, x2-x1)+(M_PI);
Thanks for any help you can provide with this.
Re: Find angle to object relative to direction bot is facing
Posted: Wed Feb 06, 2013 11:08 am
by rock5
Little known fact, even by me, is that there is a 'angleDifference' function in the bot. Of course you need to know the angle to your target. Looking at how it's used in the bot, I think this should work.
Code: Select all
angleToTarget = math.atan2(target.Z - player.Z, target.X - player.X)
angleDiff = angleDifference(angleToTarget, player.Direction)
Re: Find angle to object relative to direction bot is facing
Posted: Wed Feb 06, 2013 12:33 pm
by noobbotter
Thanks Rock. I think it was the player.Direction that was throwing me off. I didn't realize that existed. I'll give it a try when I get home tonight. If this is in a userfunction, would I have to do any kind of include for the math library or anything like that?
Re: Find angle to object relative to direction bot is facing
Posted: Wed Feb 06, 2013 2:03 pm
by rock5
It's part of the bot. Nothing to include.
Re: Find angle to object relative to direction bot is facing
Posted: Fri Feb 08, 2013 2:15 pm
by noobbotter
Thanks again, Rock. I implemented it and it worked great. I just had to then convert the resulting radians to degrees.
Actually I can probably skip that part if I can figure out the straight conversion of radians to the angles of a clock-type direction. I'm using the degree of the angle to tell me a relative direction such as 3 O'clock, or 6 O'clock. For example, 12:00 would include angles greater than 344 and less than 15.
6:00 (180 degrees) would include any angles between 164 and 195.
I am using the following function after I find the angle (degree variable) to give an audible cue as to a direction:
Code: Select all
function angle(degree)
if degree > 14 and 45 > degree then
RoMScript("PlaySoundByPath('Interface/AddOns/ingamefunctions/One.wav')");
elseif degree > 44 and 75 > degree then
RoMScript("PlaySoundByPath('Interface/AddOns/ingamefunctions/Two.wav')");
elseif degree > 74 and 105 > degree then
RoMScript("PlaySoundByPath('Interface/AddOns/ingamefunctions/Three.wav')");
elseif degree > 104 and 135 > degree then
RoMScript("PlaySoundByPath('Interface/AddOns/ingamefunctions/Four.wav')");
elseif degree > 134 and 165 > degree then
RoMScript("PlaySoundByPath('Interface/AddOns/ingamefunctions/Five.wav')");
elseif degree > 164 and 195 > degree then
RoMScript("PlaySoundByPath('Interface/AddOns/ingamefunctions/Six.wav')");
elseif degree > 194 and 225 > degree then
RoMScript("PlaySoundByPath('Interface/AddOns/ingamefunctions/Seven.wav')");
elseif degree > 224 and 255 > degree then
RoMScript("PlaySoundByPath('Interface/AddOns/ingamefunctions/Eight.wav')");
elseif degree > 254 and 285 > degree then
RoMScript("PlaySoundByPath('Interface/AddOns/ingamefunctions/Nine.wav')");
elseif degree > 284 and 315 > degree then
RoMScript("PlaySoundByPath('Interface/AddOns/ingamefunctions/Ten.wav')");
elseif degree > 314 and 345 > degree then
RoMScript("PlaySoundByPath('Interface/AddOns/ingamefunctions/Eleven.wav')");
elseif degree > 344 or 15 > degree then
RoMScript("PlaySoundByPath('Interface/AddOns/ingamefunctions/Twelve.wav')");
end
end
With that, is there a way to include a variable inside those RoMScript("PlaySoundByPath functions? I have tried it like this and didn't seem to get it to take the variable (in this case, the variable is "myvariable" which would be text of "One" thru "Twelve" which I want to use to play the file "One.wav" or "Twelve.wav" depending on what the variable is):
Code: Select all
RoMScript("PlaySoundByPath('Interface/AddOns/ingamefunctions/"..myvariable..".wav')");
Is there a way to get that to work or does the internal ' marks mess that up? I guess either way, I would still need to go through all those "if" statements to determine what to set the variable to so it's wouldn't necessarily speed it up any unless maybe I used a table to store all that info in.
Thanks.
Re: Find angle to object relative to direction bot is facing
Posted: Fri Feb 08, 2013 2:40 pm
by rock5
Code: Select all
RoMScript("PlaySoundByPath('Interface/AddOns/ingamefunctions/"..myvariable..".wav')");
That looks ok. Make sure you spelt myvariable correctly. Try printing out myvariable just before that, to make sure it has the value you expect. Check in game for any error messages.