Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1104925
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T01:33:07+00:00 2026-05-17T01:33:07+00:00

I’m working on a (rather) simple 2D project in OpenGL. It’s some sort of

  • 0

I’m working on a (rather) simple 2D project in OpenGL. It’s some sort of asteroids clone.

The ship is basically an isosceles triangle of height H, with the base have a length of H/2.

The way I’ve been doing it so far is simply storing the center point (CP) of the triangle and then calculating the final vertex positions on the fly. The ‘point’ of the ship is (vectors are x,y) the (CP.x, CP.y + H/2). The other two points are (CP.X – H/4, CP.Y – H/2) and (CP.X + H/4, CP.Y – H/2).

To get the ship facing the right direction, I first call glRotate on the current rotation angle.

This part is working fine however I’m running into issues with collision detection. Currently I’m trying to implement triangle-plane collision detection however to do that, I first need to figure out the actual points of the ship vertices after rotation. I’ve tried using trigonometry to calculate these points, however I’ve failed.

The way I’ve attempted is was to use the cosine rule to find the distance between the unrotated triangle and the triangle after rotation. To give an example, the following is how I’ve tried to calculate the ‘pointy’ vertex position after rotation:

//pA is a vector struct holding the position of the pointy vertex of the ship (centerPoint.x, centerPoint.y + height / 2)

//Distance between pA and the rotated pointy vertex - using the cosine rule
float distance = sqrt((2 * pow(size / 2, 2)) * (1 - cosf(rotAngle)));

//The angle to the calculated point
float newPointAngle = (M_PI / 2) - rotAngle;
float xDif = distance * cosf(newPointAngle);
float yDif = distance * sinf(newPointAngle);

//Actually drawing the new point
glVertex2f(pA.x - xDif, pA.y - yDif);

Any idea what I could be doing wrong?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T01:33:08+00:00Added an answer on May 17, 2026 at 1:33 am

    Thanks for the help guys but I think those explanations were a bit too technical for me. Nonetheless you made it clear to me that there’s no special case for a triangle (which, in hindsight, I should’ve known) so I tried my hand at searching and after trying a few methods, found one which worked for me.

    The post from estain at the GameDev forums did the trick. To quote his post (sorry for the c&p but may be useful for someone else who runs into a similar issue):

    Without getting to heavily into general solutions and maths (as the above posters and lots of articles have covered this already), I could give you an example on how to solve the problem “rotating a point A around point B by C degrees”.

    Now. First of all, as I described in the previous post, a point that is on the X axis, L distance from origo, is rotated C degrees around origo by

    x = L * cos(C)

    y = L * sin(C)

    Similarly, the formula for a perpendicular vector is x = -y | y = x, which means that a point that is on the Y axis (again, L from origo) would be rotated by C using the formula

    x = – L * sin(C)

    y = L * cos(C)

    As shown in the above image, the final solution is the sum of the rotations of the projected vectors, so we can derive the formula

    x’ = x * cos(C) – y * sin(C)

    y’ = y * cos(C) + x * sin(C)

    … but you knew that already, right? problem is, this formula only rotates around origo. So what we need to do is move the coordinate system we’re rotating around to origo, rotate and then move back. This can be done quickly with complex numbers or in general solutions with matrices, but we’re gonna stick to vector math on this one to keep it simple.

    first step; move the origin point.

    x’ = A.x – B.x

    y’ = A.y – B.y

    second step, perform rotation

    x” = x’ * cos(C) – y’ * sin(C) = (A.x-B.x) * cos(C) – (A.y-B.y) * sin(C)

    y” = y’ * cos(C) + x’ * sin(C) = (A.y-B.y) * cos(C) + (A.x-B.x) * sin(C)

    third and final step, move back the coordinate frame

    x”’ = x” + B.x = (A.x-B.x) * cos(C) – (A.y-B.y) * sin(C) + B.x

    y”’ = y” + B.y = (A.y-B.y) * cos(C) + (A.x-B.x) * sin(C) + B.y

    And presto! we have our rotation formula. I’ll give it to you without all those >calculations:

    Rotating a point A around point B by angle C

    A.x’ = (A.x-B.x) * cos(C) – (A.y-B.y) * sin(C) + B.x

    A.y’ = (A.y-B.y) * cos(C) + (A.x-B.x) * sin(C) + B.y

    If you’ve been following me here (and I’m a pretty lousy teacher, so sorry if you haven’t), you can se that the ordering in which you perform these operations is very important. Try to mix step 3 and 1 and see the difference in the formulae you get.

    Good luck and all!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.