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 / Question about 2D Rotation

Author
Message
TheMeq
13
Years of Service
User Offline
Joined: 5th Sep 2012
Location: Nottingham
Posted: 6th Sep 2012 02:15
Hello, My name is TheMeq

I'll dive straight into the problem I am having if you don't mind..

I have a sprite pasted nearly in the center of my screen. I just need to add the sprite width / 2 and sprite height / 2, thats ok I can do that. The problem I am having is making my sprite face my mouse, I have searched the forums before registering and alot of the code seems slightly over complex for what I am after.

Can anyone lend me a hand?

10 PRINT "Hello World"
20 GOTO 10
Sasuke
20
Years of Service
User Offline
Joined: 2nd Dec 2005
Location: Milton Keynes UK
Posted: 6th Sep 2012 04:31
Firstly, division is slow, so when halving use multiplication: * .5 instead of / 2.

As of rotation in 2d, you need to use Atanfull to find the angle between 2 points. Here's an example of what you want using it:



I suggest looking into atanfull or atan2 on the interwebs for further information or look into 2d rotations via Google. Even if it's written in a different language, it's relatively easy to port to DBP.
Kezzla
17
Years of Service
User Offline
Joined: 21st Aug 2008
Location: Where beer does flow and men chunder
Posted: 6th Sep 2012 07:44 Edited at: 6th Sep 2012 07:45
Quote: "Firstly, division is slow, so when halving use multiplication: * .5 instead of / 2."


I had never though of the difference in speed between different types of sums! I made up a little program to test out the speed difference and found that division was actually faster than multiplication.



miso
16
Years of Service
User Offline
Joined: 16th Jun 2010
Location: Budapest, Hungary, 127.0.0.1
Posted: 6th Sep 2012 09:08 Edited at: 6th Sep 2012 09:09
In the above example, you check dividing with integer with multiplying with float, so the two loops are not exactly equal in the above example, try this instead.
( still, the results shows so little differences )



No place like 127.0.0.1 .
Kezzla
17
Years of Service
User Offline
Joined: 21st Aug 2008
Location: Where beer does flow and men chunder
Posted: 6th Sep 2012 10:49
of course, so if you are dividing by a whole number then you are still better off dividing than multiplying.

so it comes down to
more bytes in the variable = more time to process
on equal footing multiply is slightly faster than divide.

why is that?

TheMeq
13
Years of Service
User Offline
Joined: 5th Sep 2012
Location: Nottingham
Posted: 6th Sep 2012 11:23
Hi Sasuke, many thanks for your code, seems to working brilliantly, apart from one small thing.

My Sprite appears to be rotating in the wrong direction.

Thanks

10 PRINT "Hello World"
20 GOTO 10
TheMeq
13
Years of Service
User Offline
Joined: 5th Sep 2012
Location: Nottingham
Posted: 6th Sep 2012 11:50
Oh, I fixed, it, it needed to be

angle = -atanfull( curMouseY, curMouseX )

and I now need to flip all my sprites so they face downwards.

Thank you very much!

10 PRINT "Hello World"
20 GOTO 10
miso
16
Years of Service
User Offline
Joined: 16th Jun 2010
Location: Budapest, Hungary, 127.0.0.1
Posted: 6th Sep 2012 22:30 Edited at: 6th Sep 2012 22:35
@Kezzla

Quote: "of course, so if you are dividing by a whole number then you are still better off dividing than multiplying."


Yes, you clearly pointed out that. I never realized or tested this before.

I think the first example shows so much difference because the program must convert the integer 100 to float 100.0 when multiplying with a float. I'm not sure, but the sizes are the same 4 bytes for integers and floats, so size can't make the differences in this case.

Generally computing a multiply is faster than computing a divide, but your test realized me, ( 9999999 iterations meant 1 ms time difference in my computer on equal footing occasionally) that it doesn't worth at all to optimize that way. So I agree with you.

@TheMeq

Quote: "angle = -atanfull( curMouseY, curMouseX )
and I now need to flip all my sprites so they face downwards."


or you can add 180 to the value of the angle.

No place like 127.0.0.1 .
Sasuke
20
Years of Service
User Offline
Joined: 2nd Dec 2005
Location: Milton Keynes UK
Posted: 7th Sep 2012 15:10
Quote: "that it doesn't worth at all to optimize that way"


Believe me, when you've got thousands of massive variables that all need dividing... every optimization is key!

Quote: "Hi Sasuke, many thanks for your code"


miso
16
Years of Service
User Offline
Joined: 16th Jun 2010
Location: Budapest, Hungary, 127.0.0.1
Posted: 7th Sep 2012 21:59
Hehe, I belive in optimization, but that 1 ms gain for a billion divide is just not enough. In that case I would rethink what else could I do differently to gain more, and still completing the goal.

Sorry for the offtopic.

No place like 127.0.0.1 .
Sasuke
20
Years of Service
User Offline
Joined: 2nd Dec 2005
Location: Milton Keynes UK
Posted: 8th Sep 2012 02:28
Quote: "In that case I would rethink what else could I do differently to gain more, and still completing the goal."


Bitshifting would be the next step I guess depend on the variable and yeah, I guess your right. Optimizing everything is just personal preference. Does the program require it, probably not but hey, least it runs the best it can

Login to post a reply

Server time is: 2026-07-07 10:13:15
Your offset time is: 2026-07-07 10:13:15