Soundstone

Member

12

191 Neutral

• Rank
Member
1. Linear collision question

I figured I would post the full solution in case anyone ever needs to use this again. Here is how I was able to solve the collision checks for both the ceiling and the floor - both of which are generated randomly with array of Vector3.   In the game loop I calculate if the player is close enough to the ceiling to be worthy of checking and the same goes for the floor. If the player is close enough I use a for loop to cycle through the vector3 positions of the ceiling / floor points. It continues over the points that are too far from the player. Once it finds a pair of contiguous points it sends a call out to check the ceiling or floor for collision based on these two points. Here is the cycle in game loop: for (int i = 0; i < NUM_POINTS; i++) { if (ship.shipPos.x > topPoints[i + 1].x) continue; if (ship.shipPos.x < topPoints[i].x) continue; collide = CheckCollisionsTop(ship, topPoints[i], topPoints[i + 1]); }  Below you'll see the collision check for my ceiling. It takes a reference to a SpaceShip because eventually it will alter the ship's position based on collision. It also takes 2 Vector3 positions.  The line segment is just a new vector that contains the distance between pointTwo and pointOne. The shipWidthPosition alters the ship position to test based on which side of the ship we want to test. This matters for the direction of the line in question. If the line is upwards - pointTwo.y is higher than pointOne.y then we will use normal ship position since the (X,Y,Z) of shipPos is the upper left corner of the ship. For a downward line (pointOne.y is higher than pointTwo.y) then we use the other side by adding the width to the ships coords.  bool CheckCollisionsTop(SpaceShip &ship, Vector3 pointOne, Vector3 pointTwo) { Vector3 lineSegment = GetVectorDistance(pointOne, pointTwo); Vector3 shipWidthPosition(ship.shipPos.x + ship.GetWidth(), ship.shipPos.y, ship.shipPos.z); if (pointTwo.y < pointOne.y) //upwards line { Vector3 lineNormal(lineSegment.y, -lineSegment.x, 0); double u = GetVectorDistance(ship.shipPos, pointTwo) * lineNormal; if (u > 0) return false; else if (u < 0) return true; } else if (pointTwo.y > pointOne.y) //downwards line { Vector3 lineNormal(lineSegment.y, -lineSegment.x, 0); double u = GetVectorDistance(shipWidthPosition, pointOne) * lineNormal; if (u > 0) return false; else if (u < 0) return true; } } And here is the collision check for the floor - differs slightly in ship testing coordinates, the sign of "u" and the normal of the line is different. bool CheckCollisionsBottom(SpaceShip &ship, Vector3 pointOne, Vector3 pointTwo) { Vector3 lineSegment = GetVectorDistance(pointOne, pointTwo); Vector3 shipHeightPosition(ship.shipPos.x, ship.shipPos.y + ship.GetHeight(), ship.shipPos.z); Vector3 shipWidthHeightPosition(ship.shipPos.x + ship.GetWidth(), ship.shipPos.y + ship.GetHeight(), ship.shipPos.z); if (pointTwo.y < pointOne.y) //upwards line { Vector3 lineNormal(-lineSegment.y, lineSegment.x, 0); double u = GetVectorDistance(shipWidthHeightPosition, pointTwo) * lineNormal; if (u > 0) return false; else if (u < 0) return true; } else if (pointTwo.y > pointOne.y) //downwards line { Vector3 lineNormal(-lineSegment.y, lineSegment.x, 0); double u = GetVectorDistance(shipHeightPosition, pointTwo) * lineNormal; if (u > 0) return false; else if (u < 0) return true; } } If you have needed linear collision, then I hope this has been some help to you.
2. Linear collision question

Thanks Haegarr,   I finally got the algorithm working for the ceiling. It kind of works for the floor at this moment, but I'm sure I'm pretty close to it. Once again thanks for the help.    Regards
3. Allegro bitmaps at runtime?

Hi Fastcall,    Thanks for the reply! The information was a relief to hear. Would you recommend printing the entire thing to one bitmap and then drawing a region of that bitmap with al_draw_bitmap_region() or just keep a bitmap the size of my screen and keep clearing / drawing the new sets of points to it before blitting to the display? Thanks again.
4. Allegro bitmaps at runtime?

Hello everyone,   I have recently run into a problem that I'm not sure Allegro 5 can handle. I was curious if anyone knew whether or not the following functionality existed for the Allegro graphics library.   I am aware that to get a scrolling background one can provide an image and load it in as a ALLEGRO_BITMAP *background. I have done this just fine. However, for the particular game I am trying to code I generate the level at random.   Basically I generate two arrays of points, one for my ceiling and one for my floor. These arrays are size top[333] and bottom[333]. The distance between each point is between 36 - 48 pixels, thus my final point in both arrays is someone just over 12,050 as the X coord.   My question is this, does Allegro have a way to create an image at runtime based on the plot placement of these randomly generated points? I've tried looking at the Allegro5 manual and it doesn't really provide any information on bitmap creation at runtime. Does anyone have experience with this sort of thing, or am I doomed to choose a new graphics library or create static images instead of random?   Thanks for taking the time to read and any advice that may be offered.
5. Linear collision question

Hi Haegarr, thanks for the continued input. I feel like I'm really close to getting this, but there is part of it that eludes me visually. To better explain where I am getting lost, I'd like to provide an example and work the steps out here for it. As you know I am not necessarily trying to draw a perpendicular line, rather instead use the math behind the perpendicular line to decide whether or not my square has collided with a line.   I have an array of points P[0] through P[4000]. Each of these points in the array are end points for a line. They are all spaced 36 units away from each other along the X axis.   lets say P[42] = (105, 50) and P[43] = (141, 82)   The distance vector is represented by Dn = (Dx , Dy) where as in this example Dx = (141 - 105) = 36 and Dy = (82 - 50) = 32;   Thus Dn = (36, 32)   From DnI can gather that the magnitude (length) of the line L = (P[42], P[43]) is equal to sqrt(36^2 + 32^2) = 48.17   If I'm understanding everything correctly so far then Vn = either (-32, 36) or (32, -36) depending on the direction I want my perpendicular vector to go. One is good for my ceiling and the other is good for my floor.   In this example, I'm worried about the ceiling so I'll choose Vn = (32, -36).   This is where my mind loses its grasp.    I believe I normalize this new vector Vn by  (32 / -36) / sqrt(32^2 + (-36)^2) giving me -.019.   If I plot similarly scaled coords on graph paper, I can see that my perpendicular line is correct based on the point Dn but it is way off based on the coords of P[42]  and P[43].   I am not grasping how to determine the "start" point of this perpendicular line. By this I mean the perpendicular line will run infinitely parallel to itself across the line P[42], P[43] (for all the points on the line segment), How does this apply to getting the relative point on the original line?   Once I grasp this step (above), I think that all I have to do is generate the distance between the point of interest (my square) and the line, and as long as its above 0 I'll be below the line.   I apologize if this is convoluted but this is the best way I could pose a question to you to further understand it. As always thanks for your time.   Regards

7. Linear collision question

Thanks for the response Trienco.   I see what your saying using the floats. It makes sense now as to why it's not working given your example. Given what you have said and what Haegarr said before you, would you suggest finding the perpendicular off of the wall line to the point of interest. Then use the two line equations to find the point of intersection on the first line. Then use that point of intersection along with the initial point of interest to calculate distance between the two. Finally testing this distance to be within a range of say 0 and 1? Does that make sense?   I also just realized that the link containing the photo image of this scenario isn't able to be used in this thread for some reason. Is it easy enough to understand the action I'm looking for without the visual? If not, I can try and figure out another way to get the image posted on here.   Thanks again.
8. Linear collision question

Thanks for the response Haegarr.   I follow that having two separate float values equaling the same is a low probability, but doesn't it at some point (despite how brief) have to equal a point on the line, say just before it crosses over the line. Does this mean that the update loop itself is too slow to catch the point as is?   I've taken a look at your proposal of using the perpendicular dot product of the line segment, but fear I don't fully comprehend it in this situation. To walk myself through it I'd like to mock up a scenario really quick.    Lets say that a line consisting of these two points A (108, 157) and B (144, 132) exists and that we are trying to find if another point collides with said line. If I'm understanding this correctly, I would want to create a perpendicular vector from this initial line to the point of interest, calculate the magnitude of this vector, if positive its on side A and if negative its on side B - resulting in collision. Is this correct? If this is not correct (albeit - perhaps even far off) would / could you provide me with a trivial example to demonstrate how the perpDot would be used correctly in this situation?   I must admit that I've been trying to re-teach myself math as it's been over a decade since my last formal math class. Its tough to re-learn after so long, hard to find a good book or resource that walks one through it much like they did in High School / College.    Once again, thank you so much for taking time to look into this. I greatly appreciate it.    Regards