Skip to main content
Make clearer the question
Source Link
Alex CB
  • 386
  • 2
  • 14

How can I calculate the intersection points taking into account thatexact point where the Y-axis is invertedaerolite will enter the screen?

How can I calculate the intersection points taking into account that the Y-axis is inverted?

How can I calculate the exact point where the aerolite will enter the screen?

Removed the angles in code so they dont give sqrt(3):1 aspect ratio as @Mangata said.
Source Link
Alex CB
  • 386
  • 2
  • 14
// project a point from the position and the delta_v. Useful for working with lines / segments
const b2Vec2 point_b {aerolite_pos + delta_v};
float slope{}, distance{};
b2Vec2 intersect_point {};
// arrow: calculate the angle of attack respect to the screen's center
const auto theta {atan2Normalized(aerolite_pos.y - screen_center.y, aerolite_pos.x - screen_center.x)};
// asign an incoming direction so we can position the arrow
if (theta >= b2_pi / 6.ftop_right && theta < 5.f * b2_pi / 6.ftop_left) {
  // TOP
  aerolite->arrow_->incoming_direction_ = Direction::Top;
  slope = (point_b.y - aerolite_pos.y) / (point_b.x - aerolite_pos.x);
  const auto y_intersect = aerolite_pos.y - (slope * aerolite_pos.x);
  intersect_point.Set(-y_intersect / slope, b2_screen_size_.y);

} else if (theta >= 5.f * b2_pi / 6.ftop_left && theta < 7.f * b2_pi / 6.fbottom_left) {
  // LEFT
  aerolite->arrow_->incoming_direction_ = Direction::Left;
  slope = (point_b.y - aerolite_pos.y) / (point_b.x - aerolite_pos.x);
  intersect_point.Set(0.f, aerolite_pos.y - (slope * aerolite_pos.x));

} else if (theta >= 7.f * b2_pi / 6.fbottom_left && theta < 11.f * b2_pi / 6.fbottom_right) {
  // BOTTOM
  aerolite->arrow_->incoming_direction_ = Direction::Bottom;
  slope = (point_b.y - aerolite_pos.y) / (point_b.x - aerolite_pos.x);
  const auto y_intersect = aerolite_pos.y - (slope * aerolite_pos.x);
  intersect_point.Set(-y_intersect / slope , 0.f);

} else {
  // RIGHT
  aerolite->arrow_->incoming_direction_ = Direction::Right;
  slope = (point_b.y - aerolite_pos.y) / (point_b.x - aerolite_pos.x + b2_screen_size_.x);
  intersect_point.Set(b2_screen_size_.x, aerolite_pos.y - (slope * (aerolite_pos.x + b2_screen_size_.x)));
}
// distance from spawn point to intersect point
distance = b2Distance(aerolite_pos, intersect_point);
// calculate the time for the aerolite to enter the screen
aerolite->arrow_->time_to_enter_ = distance / delta_v.Length();
// project a point from the position and the delta_v. Useful for working with lines / segments
const b2Vec2 point_b {aerolite_pos + delta_v};
float slope{}, distance{};
b2Vec2 intersect_point {};
// arrow: calculate the angle of attack respect to the screen's center
const auto theta {atan2Normalized(aerolite_pos.y - screen_center.y, aerolite_pos.x - screen_center.x)};
// asign an incoming direction so we can position the arrow
if (theta >= b2_pi / 6.f && theta < 5.f * b2_pi / 6.f) {
  // TOP
  aerolite->arrow_->incoming_direction_ = Direction::Top;
  slope = (point_b.y - aerolite_pos.y) / (point_b.x - aerolite_pos.x);
  const auto y_intersect = aerolite_pos.y - (slope * aerolite_pos.x);
  intersect_point.Set(-y_intersect / slope, b2_screen_size_.y);

} else if (theta >= 5.f * b2_pi / 6.f && theta < 7.f * b2_pi / 6.f) {
  // LEFT
  aerolite->arrow_->incoming_direction_ = Direction::Left;
  slope = (point_b.y - aerolite_pos.y) / (point_b.x - aerolite_pos.x);
  intersect_point.Set(0.f, aerolite_pos.y - (slope * aerolite_pos.x));

} else if (theta >= 7.f * b2_pi / 6.f && theta < 11.f * b2_pi / 6.f) {
  // BOTTOM
  aerolite->arrow_->incoming_direction_ = Direction::Bottom;
  slope = (point_b.y - aerolite_pos.y) / (point_b.x - aerolite_pos.x);
  const auto y_intersect = aerolite_pos.y - (slope * aerolite_pos.x);
  intersect_point.Set(-y_intersect / slope , 0.f);

} else {
  // RIGHT
  aerolite->arrow_->incoming_direction_ = Direction::Right;
  slope = (point_b.y - aerolite_pos.y) / (point_b.x - aerolite_pos.x + b2_screen_size_.x);
  intersect_point.Set(b2_screen_size_.x, aerolite_pos.y - (slope * (aerolite_pos.x + b2_screen_size_.x)));
}
// distance from spawn point to intersect point
distance = b2Distance(aerolite_pos, intersect_point);
// calculate the time for the aerolite to enter the screen
aerolite->arrow_->time_to_enter_ = distance / delta_v.Length();
// project a point from the position and the delta_v. Useful for working with lines / segments
const b2Vec2 point_b {aerolite_pos + delta_v};
float slope{}, distance{};
b2Vec2 intersect_point {};
// arrow: calculate the angle of attack respect to the screen's center
const auto theta {atan2Normalized(aerolite_pos.y - screen_center.y, aerolite_pos.x - screen_center.x)};
// asign an incoming direction so we can position the arrow
if (theta >= top_right && theta < top_left) {
  // TOP
  aerolite->arrow_->incoming_direction_ = Direction::Top;
  slope = (point_b.y - aerolite_pos.y) / (point_b.x - aerolite_pos.x);
  const auto y_intersect = aerolite_pos.y - (slope * aerolite_pos.x);
  intersect_point.Set(-y_intersect / slope, b2_screen_size_.y);

} else if (theta >= top_left && theta < bottom_left) {
  // LEFT
  aerolite->arrow_->incoming_direction_ = Direction::Left;
  slope = (point_b.y - aerolite_pos.y) / (point_b.x - aerolite_pos.x);
  intersect_point.Set(0.f, aerolite_pos.y - (slope * aerolite_pos.x));

} else if (theta >= bottom_left && theta < bottom_right) {
  // BOTTOM
  aerolite->arrow_->incoming_direction_ = Direction::Bottom;
  slope = (point_b.y - aerolite_pos.y) / (point_b.x - aerolite_pos.x);
  const auto y_intersect = aerolite_pos.y - (slope * aerolite_pos.x);
  intersect_point.Set(-y_intersect / slope , 0.f);

} else {
  // RIGHT
  aerolite->arrow_->incoming_direction_ = Direction::Right;
  slope = (point_b.y - aerolite_pos.y) / (point_b.x - aerolite_pos.x + b2_screen_size_.x);
  intersect_point.Set(b2_screen_size_.x, aerolite_pos.y - (slope * (aerolite_pos.x + b2_screen_size_.x)));
}
// distance from spawn point to intersect point
distance = b2Distance(aerolite_pos, intersect_point);
// calculate the time for the aerolite to enter the screen
aerolite->arrow_->time_to_enter_ = distance / delta_v.Length();
Clarified the movement of the aerolites
Source Link
Alex CB
  • 386
  • 2
  • 14

Edit: the aerolites always go to the center of the screen, no matter where they spawn.

Edit: the aerolites always go to the center of the screen, no matter where they spawn.

Source Link
Alex CB
  • 386
  • 2
  • 14
Loading