   Max turn radius using vector based movement.
  I've lurked/read a lot here but I've finally gotten around to joining and posting. So first let me say hello, and thank you to all of you who lend a hand to others. It's great to see.My issue...I've implemented flocking behavior in two different ways currently, and neither have given me exactly what I want. I believe, using straight trig I could get the behavior that I would like through trial and error, but I'd really prefer to learn how to implement this with vectors.My vector implementation is working, but I would like to constrain the turn radius of the boids as well as the speed of moment.After a bit of research, I'm understanding vectors more, as well as vector math, but their relation to angles (and vice verse) is eluding me. Is there a simple way to find the rotation/angle in degrees/radians/etc from current vector to projected vector?Additionally, if given an angle, is there a simple way to find a vector that would begin movement in that direction?Logically, I can project progress down a vector as well as the considered vector and calculate the angle to each of those points and find the angle between them, but that seems overly intensive.Aside from using the tactic above and iterating until the given max angle is reached, which seems VERY intensive, I am amiss at finding a vector given an angle.Any help is much appreciated.   
  Hello, and welcome to the site. [smile]Quote:Original post by BlindsightgamesMy vector implementation is working, but I would like to constrain the turn radius of the boids as well as the speed of moment.With "turn radius" do you actually mean angular velocity?Quote:After a bit of research, I'm understanding vectors more, as well as vector math, but their relation to angles (and vice verse) is eluding me. Is there a simple way to find the rotation/angle in degrees/radians/etc from current vector to projected vector?The cosine of the angle between two vectors times both their lengths equals the scalar product between them, i.e,|v1||v2|cos(angle) = dot(v1, v2)Is this what you asked for?Quote:Additionally, if given an angle, is there a simple way to find a vector that would begin movement in that direction?This is done by straight trigonometry, or if you will with a matrix multiplication, whichever you prefer. The resulting computation is the same. Check out this page.Quote:Logically, I can project progress down a vector as well as the considered vector and calculate the angle to each of those points and find the angle between them, but that seems overly intensive.I'm not sure what you want to do here. Could you explain it with an example?I know Reynolds uses a lot of ad-hoc vector operations in his steering behavior article (if that is where you started) that can be confusing. There is a lot of truncation going on that could cause jerky movement. I think it's a good idea to try more "physical" operations within the individual behaviors to make motion smoother and more plausible. (Note that I stay clear of the word realistic)   ;Quote:Original post by Staffan EHello, and welcome to the site. [smile]Thanks!Quote:Quote:Original post by BlindsightgamesMy vector implementation is working, but I would like to constrain the turn radius of the boids as well as the speed of moment.With "turn radius" do you actually mean angular velocity?...perhaps...?Quote:The cosine of the angle between two vectors times both their lengths equals the scalar product between them, i.e,|v1||v2|cos(angle) = dot(v1, v2)Is this what you asked for?I think so. So hrm...angle =  Math.acos(dot(v,v2) / (v2.length * v.length)) / (Math.PI/180);...but then I'd have to find the direction to turn... dir = (v.x*v2.y - v.y*v2.x)>0 ? 1 : -1;angle *= dir;That check out?Quote:Quote:Additionally, if given an angle, is there a simple way to find a vector that would begin movement in that direction?This is done by straight trigonometry, or if you will with a matrix multiplication, whichever you prefer. The resulting computation is the same. Check out this page.Yeah, I decided it would just be easy to use a rotate around a point functions. This is what I started using:degrees *= Math.PI/180;;sn = Math.sin(degrees);cs = Math.cos(degrees);newx = x*cs - y*sn;newy = x*sn + y*cs;x = newx;y = newy;Seems I'm at a disadvantage with all of this since I never even got to take geometry and went straight into higher algebras. Trig and physics are still a good reach for me. Seems like this should work. Next step is constraining turn angle based on speed, then it's on to obstacle avoidance lol -- why can't I ever start with EASY projects? :P   ;  Okay, I've gotten some aspects of the flocking and collision avoidance SOMEWHAT worked out, but I'm really having a hard time with some of the details. Does anyone happen to have some code that does flocking and collision avoidance while allowing for both turn radius and speed to be variable? I'd really like to take a look and learn from someone who knows what they are doing. :) If not, I've uploaded my code to <HERE> for scrutiny and ridicule. If someone would like to look it over and maybe give me some tips, that would be wonderful. ;  edit: I'm tired sorry. This post was no help :-p   ;  I'd avoid using angles like the plague, when someone posts something to do with angles its usually a hell of alot easier to solve the problem without angles (using vectors/matrices/quaternions).As far as I can tell a boid is free to rotate as it pleases BUT its still going to continue in its previous direction due to momentum. It then accelerates in the new direction untill its acceleration and velocity are paralelel.With that in mind, you just need to use basic physics:acceleration (worked out from your steering behaviours)velocity += accelerationposition += velocityallign your boids facing direction with its current velocity and it'll look good. The factor that dictates the turning cirle (which might end up being 0 :/ with this approach) is the acceleration. Higher acceleration and you change direction faster. If you do do a complete 180 degree turn then it will slow down and turn right on the spot which perhaps you won't want.The other option I believe is to use a matrix/quaternion, one representing desired orientation and one representing current orientation and interpolate between them. You can then change the turn rate by modifying the timestep and dragging the interpolation out over fewer or more game ticks. I beleive this is quite a common thing so its probably already in most maths libraries if you wanted to save yourself the work.Whatever your approach, I strongly advise avoiding angles (obviously you need them in places initially).   ;Quote:Original post by NanohaI'd avoid using angles like the plague, when someone posts something to do with angles its usually a hell of alot easier to solve the problem without angles (using vectors/matrices/quaternions).As far as I can tell a boid is free to rotate as it pleases BUT its still going to continue in its previous direction due to momentum. It then accelerates in the new direction untill its acceleration and velocity are paralelel.With that in mind, you just need to use basic physics:acceleration (worked out from your steering behaviours)velocity += accelerationposition += velocityActually the code is all in vectors currently. The only time angles are involved is when I determine if the difference in angle between two vectors is > the max possible rotation.I'm just using length of the vector for acceleration and have put in the slowdown and speedup functions which adjust the speed and max possible rotation by * or / .95 respectively. There is likely a better way to do this, so I'll see if I can't find some code or documentation on the velocity and vectors. Part of my problem though, is weighting the cohesion vectors with the avoidance vectors to get the proper behavior. Since the vectors already contain a length, I'm not sure how velocity/acceleration will come into play. I'm really surprised to find so little code on this topic. Plenty of psudocode, and code that does something similar but there is always at least one missing component to the problem. No turn radius, no group cohesion, and/or no speed adjustments. Le sigh. :PI'm likely going to leave it as it and call it "okay" for now until I can figure something out so I can move on to other aspects of the project. Would really like some advice/help/guidance though.Thanks!   