gniziemazity / virtual-world

MIT License
199 stars 68 forks source link

Polygone break ... not broken ? #3

Open Lecrapouille opened 10 months ago

Lecrapouille commented 10 months ago

Hi! Last time I made a portage of your road envelope in C++ and SFML (I'm not yet sharing because broken. It almost working but I've some bugs that, by missing time not been fully investigated). When implementing the C++ I noticed an issue in my code concerning the break polygon function. I'm not a JS dev, so I'm not sure to understand how memory is managed, I'm thinking as a C++ dev maybe.

https://github.com/gniziemazity/virtual-world/blob/b9482cf3ec387c9d46b0b7c7a5976e85e91d0f72/4.%20Roads/js/primitives/polygon.js#L42-L65

gniziemazity commented 10 months ago
  1. A structure with 2 points (p1 and p2). In C++ you can consider these pointers to points from the graph itself (an array holds all the points).
  2. JavaScript doesn't have pointers, but segs1 is indeed, a reference here. Changing it's properties will affect poly1.segments. Const, in JavaScript means something that you don't want (can't) reassign to something else. Not that you can't change it's properties. Weird, I know...
  3. Hard to wrap my mind around this now, but... I think you are right, now we consider the added segment again... I suspect the reason why my code isn't breaking is that that 'reconsidered' segment is outside... and in your case, it might be inside, causing some kind of infinite loop (?) no idea... just a hunch, that it may depend on the order of the points in the polygon (or the segments).
  4. You are right, that would make for better code. I took few shortcuts here and there... so that if something wasn't needed / causing trouble, I didn't implement it... So that tutorial isn't too long... Still ended up long :-D
Lecrapouille commented 10 months ago

Tanks you for your answer ! Ok for the JS const not working like C++const ! If I remember well there is also something weird with Julia lang :) I'll test your code and log and compare with me, this will make me practice a little JS ;)

Lecrapouille commented 10 months ago

EDIT: I had time to investigate carefully. My code has still bugs (missing kept segments, potential infinite loop, or missing segments detection depending if you use float or double), https://github.com/Lecrapouille/BacASable/tree/master/GameEngine/SFML/Roads

I could just investigate today: