Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Retrieving correct coordinates  (Read 2677 times)

0 Members and 1 Guest are viewing this topic.

dabo

  • Sr. Member
  • ****
  • Posts: 260
    • View Profile
    • http://www.dabostudios.net
Retrieving correct coordinates
« on: July 03, 2009, 03:23:35 pm »
Hello, let's start off by showing a screenshot.



Above you see my beautiful car with only 3 wheels :). I know I read an article a while back about how to make proper steering for a car. Iirc the car should be rotated around the point circled on the picture above (the intersection point of the left wheels' lines) and not around the center of the car.

In order to do this I need to know two points on each line (for the intersection test I'm using), but I don't know how to retrieve them. First I tried to get the points from the sf::shape::line but I can't use those values directly, I guess because of the way I have structured my code. This is how i do it (this code is only temporary, it looks like s*it I know :)):

Code: [Select]
struct Sub: public sf::Drawable
{
Sub(float _dir = 0): dir(_dir) {}

void Render(sf::RenderTarget& Target) const
{
Target.Draw(subSprite);
Target.Draw(centerDot);
Target.Draw(directionLine);
}

float dir;
sf::Shape centerDot;
sf::Shape directionLine;
sf::Sprite subSprite;
};

struct Base: public sf::Drawable
{
Base(float _dir = 0): dir(_dir) {}

void Render(sf::RenderTarget& Target) const
{
Target.Draw(baseSprite);
Target.Draw(centerDot);
Target.Draw(back);
Target.Draw(frontLeft);
Target.Draw(frontRight);
}

float dir;
Sub back;
Sub frontLeft;
Sub frontRight;
sf::Shape centerDot;
sf::Sprite baseSprite;
;
};


In this case the wheels are of type Sub and the body of type Base. This is how I create the objects (some redundant code here as well):

Code: [Select]
Base base;
base.baseSprite.SetImage(baseImage);
base.SetPosition(400, 300);
base.SetCenter(base.baseSprite.GetSubRect().GetWidth() / 2, base.baseSprite.GetSubRect().GetHeight() / 2);
base.centerDot = sf::Shape::Circle(base.GetCenter().x, base.GetCenter().y, 2, sf::Color::White);

Sub sub;
sub.subSprite.SetImage(subImage);
sub.SetPosition(96, 0);
sub.SetCenter(sub.subSprite.GetSubRect().GetWidth() / 2, sub.subSprite.GetSubRect().GetHeight() / 2);
sub.centerDot = sf::Shape::Circle(sub.GetCenter().x, sub.GetCenter().y, 2, sf::Color::White);
sub.directionLine = sf::Shape::Line(sub.GetCenter().x, sub.GetCenter().y - 200, sub.GetCenter().x, sub.GetCenter().y + 200, 1, sf::Color::Black);

base.frontLeft = sub;

sub.subSprite.SetImage(subImage);
sub.SetPosition(96, 64);
sub.SetCenter(sub.subSprite.GetSubRect().GetWidth() / 2, sub.subSprite.GetSubRect().GetHeight() / 2);
sub.centerDot = sf::Shape::Circle(sub.GetCenter().x, sub.GetCenter().y, 2, sf::Color::White);
sub.directionLine = sf::Shape::Line(sub.GetCenter().x, sub.GetCenter().y - 200, sub.GetCenter().x, sub.GetCenter().y + 200, 1, sf::Color::Black);

base.frontRight = sub;

sub.subSprite.SetImage(subImage);
sub.SetPosition(32, 0);
sub.SetCenter(sub.subSprite.GetSubRect().GetWidth() / 2, sub.subSprite.GetSubRect().GetHeight() / 2);
sub.centerDot = sf::Shape::Circle(sub.GetCenter().x, sub.GetCenter().y, 2, sf::Color::White);
sub.directionLine = sf::Shape::Line(sub.GetCenter().x, sub.GetCenter().y - 200, sub.GetCenter().x, sub.GetCenter().y + 200, 1, sf::Color::Black);

base.back = sub;


I think the problem is that when I retrieve the points for for example
Code: [Select]
base.frontLeft.directionLine
their coordinates are relative to frontLeft (???) and not to the 0,0 coordinate of the window as I would want.

Any help is appreciated. Other ways of doing it are welcome as well (finding two points on each drawn line, if that was unclear :))

EDIT: when I retrieve a point position of a line using
Code: [Select]
directionLine.GetPointPosition(0)
are the point coordinates stored always the inital values? or do they change as the shape is rotated etc.?

EDIT2: the screenshot above is slightly wrong (found the article), the line of the right front wheel should intersect in the same point as the left. But it doesn't change the problem.

dabo

  • Sr. Member
  • ****
  • Posts: 260
    • View Profile
    • http://www.dabostudios.net
Retrieving correct coordinates
« Reply #1 on: July 03, 2009, 07:29:40 pm »
After reading up on the article again, I think there is another easier (possibly) way to do this. I will investigate further.