@@ -265,76 +265,74 @@ void MapRayCast(MapRay ray) {
265265 continue ;
266266 }
267267
268- if (ray -> is_colliding ) {
269- // Calculate billboard collisions first because they are before the wall
270- // Get possible Billboard collisions
271- List billboards = MapGetBillboardsAt (ray -> map , mapX , mapY );
272-
273- ListMoveToStart (billboards );
274-
275- while (ListCanOperate (billboards )) {
276- Billboard bb = ListGetCurrent (billboards );
277-
278- // Calculate circle-line intersection
279- double r = BillboardGetSize (bb );
280- // In the original the coordinates of the circle were on (0, 0), so I'm accounting for that
281- double x1 = ray -> posX - (double ) BillboardGetX (bb );
282- double y1 = ray -> posY - (double ) BillboardGetY (bb );
283- double x2 = (ray -> posX + ray -> length * rayDirX ) - (double ) BillboardGetX (bb );
284- double y2 = (ray -> posY + ray -> length * rayDirY ) - (double ) BillboardGetY (bb );
285-
286- double dx = x2 - x1 ;
287- double dy = y2 - y1 ;
288- double dr = sqrt (dx * dx + dy * dy );
289- double det = x1 * y2 - x2 * y1 ;
290- double delta = r * r * dr * dr - det * det ;
291-
292- if (delta < 0 ) { // No collision
293- ListMoveToNext (billboards );
294- continue ;
295- }
296-
297- int sgn_dy = dy < 0 ? -1 : 1 ;
298- // There can be 2 collision points, but only one is needed
299- double col_x = (det * dy + sgn_dy * dx * sqrt (delta )) / (dr * dr );
300- double col_y = (- det * dx + fabs (dy )* sqrt (delta )) / (dr * dr );
301-
302- // Re-account for the actual world coords
303- col_x += (double ) BillboardGetX (bb );
304- col_y += (double ) BillboardGetY (bb );
305-
306- rayCollision * col = malloc (sizeof (rayCollision ));
307- * col = (rayCollision ) {
308- .collisionX = col_x ,
309- .collisionY = col_y ,
310- .collisionGridX = (int ) (col_x ) / MapGetTileSize (ray -> map ),
311- .collisionGridY = (int ) (col_y ) / MapGetTileSize (ray -> map ),
312- .collisionType = COLLISION_BILLBOARD ,
313- .billboard = bb
314- };
315- ListAppendFirst (ray -> collisions , col );
316-
268+ // Calculate billboard collisions first because they are before the wall
269+ // Get possible Billboard collisions
270+ List billboards = MapGetBillboardsAt (ray -> map , mapX , mapY );
271+
272+ ListMoveToStart (billboards );
273+
274+ while (ListCanOperate (billboards )) {
275+ Billboard bb = ListGetCurrent (billboards );
276+
277+ // Calculate circle-line intersection
278+ double r = BillboardGetSize (bb );
279+ // In the original the coordinates of the circle were on (0, 0), so I'm accounting for that
280+ double x1 = ray -> posX - (double ) BillboardGetX (bb );
281+ double y1 = ray -> posY - (double ) BillboardGetY (bb );
282+ double x2 = (ray -> posX + ray -> length * rayDirX ) - (double ) BillboardGetX (bb );
283+ double y2 = (ray -> posY + ray -> length * rayDirY ) - (double ) BillboardGetY (bb );
284+
285+ double dx = x2 - x1 ;
286+ double dy = y2 - y1 ;
287+ double dr = sqrt (dx * dx + dy * dy );
288+ double det = x1 * y2 - x2 * y1 ;
289+ double delta = r * r * dr * dr - det * det ;
290+
291+ if (delta < 0 ) { // No collision
317292 ListMoveToNext (billboards );
293+ continue ;
318294 }
319295
320- ListDestroy (& billboards );
296+ int sgn_dy = dy < 0 ? -1 : 1 ;
297+ // There can be 2 collision points, but only one is needed
298+ double col_x = (det * dy + sgn_dy * dx * sqrt (delta )) / (dr * dr );
299+ double col_y = (- det * dx + fabs (dy )* sqrt (delta )) / (dr * dr );
300+
301+ // Re-account for the actual world coords
302+ col_x += (double ) BillboardGetX (bb );
303+ col_y += (double ) BillboardGetY (bb );
321304
322- Tile collidingTile = MapGetTileObject (ray -> map , MapGetTile (ray -> map , mapX , mapY ));
323305 rayCollision * col = malloc (sizeof (rayCollision ));
324306 * col = (rayCollision ) {
325- .collisionX = ray -> posX + ray -> length * rayDirX ,
326- .collisionY = ray -> posY + ray -> length * rayDirY ,
327- .collisionGridX = mapX ,
328- .collisionGridY = mapY ,
329- .collisionType = COLLISION_MAP_TILE ,
330- .tile = collidingTile ,
331- .hitSide = hitSide
307+ .collisionX = col_x ,
308+ .collisionY = col_y ,
309+ .collisionGridX = (int ) (col_x ) / MapGetTileSize (ray -> map ),
310+ .collisionGridY = (int ) (col_y ) / MapGetTileSize (ray -> map ),
311+ .collisionType = COLLISION_BILLBOARD ,
312+ .billboard = bb
332313 };
333314 ListAppendFirst (ray -> collisions , col );
334- // If the colliding tile is transparent, then just continue
335- ray -> is_colliding = ! TileIsTransparent ( collidingTile );
315+
316+ ListMoveToNext ( billboards );
336317 }
337318
319+ ListDestroy (& billboards );
320+
321+ Tile collidingTile = MapGetTileObject (ray -> map , MapGetTile (ray -> map , mapX , mapY ));
322+ rayCollision * col = malloc (sizeof (rayCollision ));
323+ * col = (rayCollision ) {
324+ .collisionX = ray -> posX + ray -> length * rayDirX ,
325+ .collisionY = ray -> posY + ray -> length * rayDirY ,
326+ .collisionGridX = mapX ,
327+ .collisionGridY = mapY ,
328+ .collisionType = COLLISION_MAP_TILE ,
329+ .tile = collidingTile ,
330+ .hitSide = hitSide
331+ };
332+ ListAppendFirst (ray -> collisions , col );
333+ // If the colliding tile is transparent, then just continue
334+ ray -> is_colliding = !TileIsTransparent (collidingTile );
335+
338336 if (i == MAX_RAY_STEPS ) {
339337 break ;
340338 }
0 commit comments