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.

Newcomers DBPro Corner / determine which way to rotate object in smallest angle?

Author
Message
Kiaurutis
13
Years of Service
User Offline
Joined: 19th Jan 2011
Location: Lithuania
Posted: 31st Jan 2011 20:06
I cant find any threads containing knowledge about how to get which way should object be rotated.

lets say i have an object (box) which can move forwards. i have box coordinates:

Rx#=object angle x (ID)
Ry#=object angle y (ID)
Rz#=object angle z (ID)

then i rotate box to target point:

point object ID, Unit(ID).dx, Unit(ID).dy, Unit(ID).dz

get y angle to target:

Ry2#=object angle y (ID)

and rotate object back to its original angle:

rotate object ID, Rx#,Ry#,Rz#

Then i rotate a little and move object:

if Ry#< Ry2#-10
yrotate object ID, Ry#+1
else
if Ry#> Ry2#+10 then yrotate object ID, Ry#-1
endif
move object ID, Unit(ID).Speed

but this doesn't work ok. box starts moving in circles. i tried to fix this with maximum difference of angle:

if Ry#< Ry2#-10 and abs(Ry#-Ry2#)<180
yrotate object ID, Ry#+1
else
if Ry#> Ry2#+10 and abs(Ry#-Ry2#)<180
yrotate object ID, Ry#-1
endif
endif

this works to turn in small angle okey, but when there is big angle (>90*) box don't turn, it just go straight

Any ideas how to fix this, or maybe there is some more efficient way to turn objects? i want to imitate car movement when it can turn in variable angle and while moving forwards and turning come to destination point (atm with no obstacles). later i would use this to travel between waypoints
Neuro Fuzzy
17
Years of Service
User Offline
Joined: 11th Jun 2007
Location:
Posted: 31st Jan 2011 20:54
If you like math, then you should check this out:
http://www.neurofuzzydev.com/V/D/43a.htm
Sry, don't have time to write a big post.

There are a lot of ways to do this.

LBFN
17
Years of Service
User Offline
Joined: 7th Apr 2007
Location: USA
Posted: 1st Feb 2011 04:00
Here's something else you could look at. It really does not have the maths that NeuroFuzzy incorporates, but it will rotate the object reasonably well. Five boxes are created and placed randomly. The 'player' is the white box and the camera follows it from overhead. It will target the five boxes one at a time in the order they are created. It is possible that the player box can go through one of the other five boxes, as it is only collision checking against the target box. Once it reaches the target box, it will be deleted and the player box will go to the next target. Once it reaches the fifth box, it ends. I hope that this will give you something to work with:


LB

Kiaurutis
13
Years of Service
User Offline
Joined: 19th Jan 2011
Location: Lithuania
Posted: 1st Feb 2011 13:02
LBFN's code works goood, but when i put it into my program...
those *swear-word* boxes just don't move like LBFN's boxes... i don't understand whats wrong
anyway thanks LBFN that is nice try to help. if u read this post once more, i add my program, maybe u can find whats wrong

i will try to read the Neuro Fuzzy's tutorial now- i don't like maths, but there is nothing else to do

Attachments

Login to view attachments
LBFN
17
Years of Service
User Offline
Joined: 7th Apr 2007
Location: USA
Posted: 1st Feb 2011 15:44
The variable, direction, needed to be global. I tweaked the angle check a little bit and reduced it to 5%. It seems to work now (I clicked on probably 50 different locations and it worked every time). However, it sometimes does spin an unnecessary circle (i.e. more tweaking needed) sometimes.



NeuroFuzzy's tut makes my head hurt LOL

Kiaurutis
13
Years of Service
User Offline
Joined: 19th Jan 2011
Location: Lithuania
Posted: 1st Feb 2011 16:51
thx for your both help. i think will stick to neuro fuzzy's vectors. in fact i don't understand how those vectors work, but can handle the box movement quite easy i used only 1 function from fuzzy, because in my own code there already exist object to act as a pointer. now all the program looks like this:



if for someone vectors are too difficult u can use LBFN's posted version- it needs a little bit more editing, but should use less pc resources.
hope this thread helps for other's to take control over their boxes as well
Neuro Fuzzy
17
Years of Service
User Offline
Joined: 11th Jun 2007
Location:
Posted: 2nd Feb 2011 02:58
Quote: "NeuroFuzzy's tut makes my head hurt LOL"

Argh, It's not my tutorial, I'm just mirroring it 'cuz it's awesome! xD

And It figures... it's a thing at the END of a giant tutorial, so most of it relies on that stuff, and the problem that like 60% of the article is talking about is unrelated to this post, and actually pretty complicated (like, it really needs the knowledge taught in earlier sections).

Here's a link to it (I have it mirrored on my site like I said but... it seems to be super laggy ATM T_T)
http://forum.thegamecreators.com/?m=forum_view&t=133126&b=7
Odds are you'll need to know some of it sooner or later, but if you don't have much interest in *how* 3d math is handled, then it won't hurt too much if you don't learn it (When you get to other rotation stuff, EZRotate is there for you!)

Kiaurutis
13
Years of Service
User Offline
Joined: 19th Jan 2011
Location: Lithuania
Posted: 2nd Feb 2011 10:43
I tried to navigate in your site, but it too difficult for me... LOL

I found this in "about" section:
Quote: "The papers section is a directory of things I've written up, about math, or programming, or about something I've seen. Basically, any purposeful document is a "paper"."


So i go to "papers" section, willing to find some wonderful treasures there...
and i get this:

Quote: "Error 404: not found.
My site doesn't have all pages finished! This error probably means I just haven't got the page up yet."


I have just downloaded Your vectorsCHM.rar file. When have some spare time will read it all, maybe then vectors don't bite me

As i understand, it contains all the other pages of Your tutorial, that U linked in first post.

The only thing that can't be too much is knowledge

Join The dark Side! We have cookies :-P
Sven B
19
Years of Service
User Offline
Joined: 5th Jan 2005
Location: Belgium
Posted: 2nd Feb 2011 13:54 Edited at: 3rd Feb 2011 17:10
I have recently written a tutorial about vectors in this thread. Maybe it's worth it to check it out as well.

To get the angle you might as well use the function:
[edit] Corrected


Cheers!
Sven B

Kiaurutis
13
Years of Service
User Offline
Joined: 19th Jan 2011
Location: Lithuania
Posted: 2nd Feb 2011 21:00
mmm... this looks much more compact and resource friendly

i don't understand what makes these two functions:

ccw vector2(a,b)
found only "prod# = CCW Vector2(IDa, IDb) See chapter 3.d" in tutorial

newxvalue(x#, ay#, 1.0)

can u plz explain them shortly? i can't find them explained in "Vectors in DBP" nor in "Easy Transformations in DBP"

i will try to put it in my code later. now dealing with limb rotation- maybe this helps with limbs too
should take some rest, all day fighting with boxes who don't want do what i want

thanks

Join The dark Side! We have cookies
Sven B
19
Years of Service
User Offline
Joined: 5th Jan 2005
Location: Belgium
Posted: 2nd Feb 2011 21:45 Edited at: 4th Feb 2011 15:47
Hi Kiaurutis,

ccw vector2() basically returns the Z-component of a cross-product of those two vectors. With "3.d" I meant the part where it says:

Quote: "
The cross product doesn't exist in 2D. However, if we imagine ourselves an extra Z-axis perpendicular on the existing X and Y axis in 2D, then it's possible to have the following variant of the cross product: (...)"


newxvalue(x#, ay#, d#) is equivalent to (edit: has to be a sine)
x# + sin(ay#) * d#

newzvalue(z#, ay#, d#) is equivalent to
z# + cos(ay#) * d#
They're mainly used to make something move on a circle with center (x#, y#), radius d# and with an angle of ay#.

They're not really related to vectors which is why you couldn't find them in any tutorial .

Cheers!
Sven B

Neuro Fuzzy
17
Years of Service
User Offline
Joined: 11th Jun 2007
Location:
Posted: 2nd Feb 2011 21:53
^^ That has to do with vector cross product... The cross product takes two vectors and returns another vector perpendicular to both. If you have two vectors in the x/y plane, then the cross product of both will have 0 x and y value, and only a Z value. That function returns the Z value.


However... the atanfull function already does pretty much what that getShortestAngle does.
(If you use inverse cosine and inverse sine... well if I give you cos(x), there are probably multiple values of x on the interval [0,360], meaning if you use the atan(), acos() or asin() functions, you don't get the full angle, just "a guess"). atanfull determines the angle from -180 to 180.)



Kiaurutis
13
Years of Service
User Offline
Joined: 19th Jan 2011
Location: Lithuania
Posted: 3rd Feb 2011 08:38
Sven's version works good

but Fuzzy's version has some bug, it doesn't rotate 100% correct. sometimes rotates more than 180 degrees. I would like to use it in my program because of small number of calculations, if it works

here is modified code to show the bug:



bug appear randomly

Join The dark Side! We have cookies
Kiaurutis
13
Years of Service
User Offline
Joined: 19th Jan 2011
Location: Lithuania
Posted: 3rd Feb 2011 10:17
Sven, there is smth strange about angle returned- when i use text command to print it, i get "-1.#IND". i checked if input was wrong- in sven's original function i get error with these values too. but can't understand whats wrong?
That's the input and return values



i tried to add "normalize vector2 1, 1"
this made my object to spin in various fixed radius circles

vectors still bite me

Join The dark Side! We have cookies
Neuro Fuzzy
17
Years of Service
User Offline
Joined: 11th Jun 2007
Location:
Posted: 3rd Feb 2011 12:36 Edited at: 3rd Feb 2011 12:38
Arghh sorry... messed up X_X



Okay, basically... I assume you're a bit familiar with some trigonometry? If you have a right triangle, like this:


then tan(θ[b][/b])=y/x
So by definition, θ=atan(y/x)
The problem is that there are multiple solutions for θ. The thing is - if you just have y and x, you can determine θ with no ambiguity. Because this isn't as straightforward as the inverse tangent command (in fact whatever the function does is probably exactly what sven's code is doing), it's given the name "atan2" or "atanfull". so θ=atanfull(x,y)

atanfull returns a value between -180 and 180. The difference between angle and atanfull(x,y) will be the angle difference the object needs to turn, and then since we want to find the shortest angle (which will be under 180 degrees), we just make sure the result is also inbetween -180 and 180. I just didn't think that code through at first X_X

Kiaurutis
13
Years of Service
User Offline
Joined: 19th Jan 2011
Location: Lithuania
Posted: 3rd Feb 2011 14:08 Edited at: 3rd Feb 2011 14:19
no prob about Your messing... i think my code messes with your code- damn boxes aren't moving correctly
and i don't understand vectors and 3dmaths enough to fix it myself...


this is fuzzy's last code suggestion which is short, but unfortunately doesn't work:



I use unit(ID).dx and unit(ID).dz to store move target coordinates for any movable object, first object's coordinates and angle is taken directly with "object angle..." and "object position..." built in functions. have no clue what could be giving wrong answer from GetShortestAngle function


this is sven's code that doesn't work too:



i would prefer to use as economical function as possible, coz now have only 60 FPS after adding function to check distances between objects- for automatic enemy targeting.

all this program will be rewrited and structure changed later, as now i only learn and want to have "standalone" functions for everything: will make get distance function to use vectors, maybe 3dimensional matrix for holding all object-to-object distances if they aren't moving and much more

added media too -->

Join The dark Side! We have cookies

Attachments

Login to view attachments
Kiaurutis
13
Years of Service
User Offline
Joined: 19th Jan 2011
Location: Lithuania
Posted: 3rd Feb 2011 14:24 Edited at: 3rd Feb 2011 14:25
some "\\\" appeared in previous post's text after posting failure, i think my post was too long.
after i deleted part with my working code i could post. i removed those "\\\" with word program so text should be ok

now here is the working code from fuzzy's tutorial. it includes more functions, so slow down program a little, if used many times (it will be used many times)



Join The dark Side! We have cookies
Sven B
19
Years of Service
User Offline
Joined: 5th Jan 2005
Location: Belgium
Posted: 3rd Feb 2011 17:09 Edited at: 3rd Feb 2011 17:10
My apologies, the correct functions is:



Sven B

Kiaurutis
13
Years of Service
User Offline
Joined: 19th Jan 2011
Location: Lithuania
Posted: 4th Feb 2011 20:39
Thanks Sven... i can't make this work in my code anyway- but maybe it helps for other ppl

The longer version of vectors works ok, i will use it as long as have enough fps, if there is a must to fasten program, i will learn all the vectors and even ask a mathematics professor for help if there is no other way

on the other hand that "longer" function ain't that long to slow program decisively

Now rewriting all the program in other structure to fight back some fps

Join The dark Side! We have cookies

Login to post a reply

Server time is: 2024-11-24 07:39:58
Your offset time is: 2024-11-24 07:39:58