- #1
sciwizeh
- 25
- 0
So, I'm working on a 2D sim/game in which "people" aka circles travel around a "city" aka plane with boxes and line barriers. In the person step I'm passing a list of line segments, consisting of walls boundaries etc. and I'm having a bit of trouble when two lines meet at an acute angle. When the person heads toward the intersection point, it's velocity bisects the angle and goes directly through. I'm at a loss to how I should make the person turn around. My current stepping function follows, I'm probably not going about this in the most optimal way, but with the exception of the problem mentioned it works well.
the code is in Java, however it should not be difficult to interpret by people familiar with other languages.
any help would be appreciated. as I said, I know my method is sloppy at best, but I'm really at a loss.
EDIT: after more testing and commenting out the line vel.add(steer.mul(LINE_ADVERSION)); the acute problem is solved, and it does avoid the lines, but the motion is much too bouncy, like a spring, and it doesn't follow the line at all, which I do like. so i could still use some information if possible
the code is in Java, however it should not be difficult to interpret by people familiar with other languages.
Code:
public void step(double dt, ArrayList<Line> lines){
for(Line l : lines){
double dist = l.distance(loc);//gets distance from the point to the segment
if(dist<(rad*LINE_DIST)) {//consider the segment only if we are close
double dot = l.getVec().dot(vel);//segmentVector*velocity
double sig = (dot<0) ? -1 : 1;//sign of the dot product
Vector des = l.getVec().mul(sig);//velocity that would be parallel
Vector steer = des.diff(vel);//vector to transform velocity to dest
steer.normalize();//just the direction of steer kept
vel.add(steer.mul(LINE_ADVERSION));//transform velocity to dest frame by frame
Vector toP1 = loc.diff(l.p1);//vector from the first point of the segment to the location
sig = (l.getNormal().dot(toP1)<0) ? -1 : 1;//side of the segment point is on
vel.add(l.getNormal().mul(sig));//adjustment of velocity keeps right angles from causing current acute angle problem
}
}
// vel.noise();//this was the original fix for right angle
vel.limit(50);//speed limit
loc.add(vel.mul(dt));//location update
}
any help would be appreciated. as I said, I know my method is sloppy at best, but I'm really at a loss.
EDIT: after more testing and commenting out the line vel.add(steer.mul(LINE_ADVERSION)); the acute problem is solved, and it does avoid the lines, but the motion is much too bouncy, like a spring, and it doesn't follow the line at all, which I do like. so i could still use some information if possible
Last edited: