Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

DarkBASIC Professional Discussion / Rotate one point about another

Author
Message
Lucas Tiridath
AGK Developer
17
Years of Service
User Offline
Joined: 28th Sep 2008
Location: Kings Langley, UK
Posted: 13th Jun 2011 12:58
Hi. I've been working on a piece of software that stores a number of "lines" in the format of the x and y coordinates of their starts and ends. One of the functions of this software is to rotate those lines around their centre point. However I can't seem to get this to work. My current algorithm kind of rotates but it distorts as it does so. A rotation of 0 or 180 works fine. A rotation of 45 gives a highly distorted result. A rotation of 90 seems to give all the lines the exact same coordinates so that only one line appears.

I wondered if anyone was able to tell me where I am going wrong with this algorithm. I have managed to create this bit of stand alone code that reproduces the error and uses the same algorithm as the piece of software I'm working on does. To see the problem, try compiling with differing values in the commented line.



Any help with getting this to rotate without distortion would be greatly appreciated. Thanks!
Green Gandalf
VIP Member
21
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 13th Jun 2011 14:59 Edited at: 13th Jun 2011 15:00
You have two quite different problems:

1. Make sure you are using an appropriate display resolution/aspect ratio combination so that your square is in fact square. (I didn't when I first ran your code.)

2. More importantly you are overwriting the values of AllLines as your program works through this bit of the code:



The simplest solution is to use a second copy for the new rotated values like this (with appropriate changes elsewhere in the code):



With those two issues fixed your code works fine:

Van B
Moderator
23
Years of Service
User Offline
Joined: 8th Oct 2002
Location: Sunnyvale
Posted: 13th Jun 2011 15:26
One thing I would suggest, if it's applicable to this, is why not store the angle and radius of your lines from centre. Rather than using a 2D location, you'd store the angle and radius, then work out the 2D line locations from that. It might make it quicker. I've used this technique with a 2D collision system, using lines for collision on rotated sprites, like an outline around each sprite made from lines from angle and radius data - worked out pretty well. I just applied the rotation and scale of each sprite when creating the line data.

Health, Ammo, and bacon and eggs!
Lucas Tiridath
AGK Developer
17
Years of Service
User Offline
Joined: 28th Sep 2008
Location: Kings Langley, UK
Posted: 13th Jun 2011 15:28 Edited at: 13th Jun 2011 15:30
Perfect

It was the second problem that was getting me. It's actually quite scary that I didn't realise that that is how calculations were done. I always assumed intermediate stages were held in some register somewhere rather than overwriting the variable. I dread to think how many things like this there are in my codes lol.

Anyhow thanks once again. I've been agonising over this one for quite some days now so it's really appreciated

EDIT:
@VanB
You must have posted at the same time as me. Thanks for the hint. In fact line collision is exactly what this is doing. I'll consider it but I always thought you had to use more trig when storing lines as an angle and a radius which might be quite slow, right?
Green Gandalf
VIP Member
21
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 13th Jun 2011 16:14
Quote: "I always assumed intermediate stages were held in some register somewhere rather than overwriting the variable."


I guess they are for a single line of code.

Your problem is that your second line in that snippet I posted should be using the original value of nX1 - but your previous line has changed the value.

I've seen similar errors in my own code from time to time and they are depressingly hard to spot.
Van B
Moderator
23
Years of Service
User Offline
Joined: 8th Oct 2002
Location: Sunnyvale
Posted: 13th Jun 2011 16:24
Thing is, the only trig you'd need is SIN and COS - which should be blisteringly fast. I think trig get's to be a bottleneck when you have to convert back and forth, like work out angles and distance.
I think though, it depends on how old-school you are - there are ways you can make things really quick. For example, pre-storing SIN in an array, and using that instead of sin() - if the engine can deal with the loss of accuracy, then things like that can really improve performance. Also, using SQRT to find distance, if you make a 2D array, you can pre-calculate that as well. I haven't done a lot of this stuff recently, as most of the time it's pointless, giving processor speeds. But if your checking hundreds of distances per loop, then this stuff can really help.

Scaling and rotation though, is the real reason why I suggested storing angle and rad. When I did this, I only used bytes, so instead of 360 degrees, I used 0-239 - so multiplying by 1.5 to get the real angle. This gives a snap of course, but it allows for exact 45 degrees, and is not too far from 360. The extra 240-255 values I used to signify the end of line loops etc. The radius was stored as 0-255 too, which got factored in the actual size of the sprite. So, with 2 bytes I was able to store a point that could adapt to scale and rotation quite easily and quickly. I made a little editor to let me trace the lines around sprites and save the data - storing the angle and radius of each point. One other benefit, is that you can store a collision set, and use it each time for each matching sprite - rather than rotating actual data, you might just have a big list of coordinates, which is filled from the angle and radius data, maybe keeping track of which line belongs to what sprite. This is a good way to avoid floating point problems, because the line data itself would be generated from angle and rad data - I can imagine that it would be quite hard to get an angle to exactly 0.0 with residual data.

Health, Ammo, and bacon and eggs!
Lucas Tiridath
AGK Developer
17
Years of Service
User Offline
Joined: 28th Sep 2008
Location: Kings Langley, UK
Posted: 13th Jun 2011 17:40
Quote: "I guess they are for a single line of code.

Your problem is that your second line in that snippet I posted should be using the original value of nX1 - but your previous line has changed the value."

Ah now that makes more sense! How did I miss that glitch though?? Anyhow I'm glad that one line calculations do behave as I thought. I thought for a moment there that I would have to go back through all my games and try and root out those errors!

@Van B
Thanks for the info! I hadn't thought of doing it way and having written a plugin and editor based on four coord lines, I'm probably going to stick with that for the moment but I'll certainly consider using angle and radius in future.

Login to post a reply

Server time is: 2026-07-10 22:55:10
Your offset time is: 2026-07-10 22:55:10