|
View:
New views
4 Messages
—
Rating Filter:
Alert me
|
|
|
Not so good at Math, need turning helpHi there,
I'm learning AI from Game AI by Example, and using Robocode as my sandbox to try out my learning. I am up to the section on steering, which discusses how to derive the angle to turn to point towards a certain (x,y) point. It says to do this by using the dot product of the heading *vector* and the vector between the current position and the given point. My math is rusty as all hell, so I am finding even this basic thing troublesome (even though Java3D provides a Vector2d class that I am using!) This code *doesn't work*, the robot will just go round in circles: --------- Vector2d toPoint = new Vector2d(newPosition.getX() - getX(), newPosition.getY() - getY()); turnRightRadians(toPoint.angle(new Vector2d(getHeadingRadians(), 0))); --------- I think the problem maybe that I don't know how to represent the heading vector correctly. What am I doing wrong here? Once I crack this part of it, I can carry on with the rest of the chapter, because it all makes sense after that. It all just hinges on this bit :( Thanks ever so much for any help, Chris Lewis |
|
|
Re: Not so good at Math, need turning help> Vector2d toPoint = new Vector2d(newPosition.getX() - getX(),
> newPosition.getY() - getY()); > > turnRightRadians(toPoint.angle(new Vector2d(getHeadingRadians(), 0))); > --------- > > I think the problem maybe that I don't know how to represent the > heading vector correctly. What am I doing wrong here? Once I crack > this part of it, I can carry on with the rest of the chapter, because > it all makes sense after that. It all just hinges on this bit :( You're right, you aren't constructing the heading vector correctly. You've got the x-coord as the heading and the y-coord as 0, which is pretty meaningless. You would be better off calculating the angle to the newPosition (take a look at the atan2 function) and minusing off the current heading (ie. not using vectors at all). |
|
|
Re: Not so good at Math, need turning helpmrlewisham wrote:
> --------- > Vector2d toPoint = new Vector2d(newPosition.getX() - getX(), > newPosition.getY() - getY()); > > turnRightRadians(toPoint.angle(new Vector2d(getHeadingRadians(), 0))); > --------- > Rather use: setTurnRightRadians(Math.asin(Math.sin( Math.atan2(newX - myX, newY - myY) - getHeadingRadians() ))); The asin/sin normalises the angle into the nearest 90 degrees, so if it is behind you you will turn away from it, and you can back up towards it instead. The Math.atan2 returns the *external* angle of the vector, which is what we are after. If you returned the *internal* angle, it would vary depending on the quadrant he was in, and wouldn't be right anyways. Hope that helps, Julian |
|
|
Re: Not so good at Math, need turning helpHi Chris,
You should have a look at the GoToBot described at the RoboWiki: http://robowiki.net/cgi-bin/robowiki?Movement/GoToBot One thing you should now is that the coordinate system in Robocode is mirrored around the y-axis compared to the "normal" coordinate system for graphical components, i.e. the bottom left corner is (0,0). Also note that headings and bearings in robocode is clockwise, i.e. not like normal math. So 12 o'clock is 0 degrees, 3 o'clock is 90 degress etc. This means that when you use e.g. sin & cos, you should use cos instead of sin and vice versa. This could explain the odd behavior of your robot. Best regards, - Flemming --- In Robocode@..., "mrlewisham" <cflewis@...> wrote: > > Hi there, > I'm learning AI from Game AI by Example, and using Robocode as my > sandbox to try out my learning. > > I am up to the section on steering, which discusses how to derive the > angle to turn to point towards a certain (x,y) point. > > It says to do this by using the dot product of the heading *vector* > and the vector between the current position and the given point. > > My math is rusty as all hell, so I am finding even this basic thing > troublesome (even though Java3D provides a Vector2d class that I am > using!) > > This code *doesn't work*, the robot will just go round in circles: > > --------- > Vector2d toPoint = new Vector2d(newPosition.getX() - getX(), > newPosition.getY() - getY()); > > turnRightRadians(toPoint.angle(new Vector2d(getHeadingRadians(), > --------- > > I think the problem maybe that I don't know how to represent the > heading vector correctly. What am I doing wrong here? Once I crack > this part of it, I can carry on with the rest of the chapter, because > it all makes sense after that. It all just hinges on this bit :( > > Thanks ever so much for any help, > Chris Lewis > |
| Free Forum Powered by Nabble | Forum Help |