Jump to content


Photo

Smooth Rotation that won't glitch up


  • Please log in to reply
15 replies to this topic

#1 kikjezrous

kikjezrous

    Seer of Spaaace

  • GMC Member
  • 637 posts
  • Version:GM8.1

Posted 02 March 2012 - 02:30 AM

This question has been asked a thousand times, so I'm dragging my code out of the heart of Crash! and sticking it here.

This is a function that you can stick in a destination angle, the current angle, a speed cap, and a motion speed, and it will return the new angle.
Use -1 for no cap.
//rot_smooth(dest_angle,angle_value,speed,cap)
var dest_angle, diff;
dest_angle = argument0; angle_value = argument1; diff = dest_angle - angle_value;

//This is where the action is!
if (diff >= 180 && angle_value < 180) { angle_value += 360; }
else if (diff <= -180) { angle_value -= 360; return angle_value; }

//Check the cap
if (argument3 == -1) { angle_value += diff / argument2; }
else {
	if (diff / argument2 < argument3) { angle_value += diff / argument2; }
	else { angle_value += argument3*sign(diff); } }
	
//Spit it out...
return angle_value;;

Now, if you don't want it to have variable speed, and instead have it rotate at a constant speed until it hits the destination, then:
//rot_constant(dest_angle,angle_value,speed)
var dest_angle, diff;
dest_angle = argument0; angle_value = argument1; diff = dest_angle - angle_value;

//This is where the action is!
if (diff >= 180 && angle_value < 180) { angle_value += 360; }
else if (diff <= -180) { angle_value -= 360; }
else { 
	if (angle_value != dest_angle) {
	var diff; diff = dest_angle - angle_value;
	if (abs(diff) < argument2) { angle_value += diff; }
	else { angle_value += argument2*sign(diff); } } }

//Spit it out...
return angle_value;

This will have one small hiccup, but it is hardly noticeable. Take these codes and paste them into a script, and to call them in an object, just use the instructions in the first line of each script.

BTW: When speed = 1, it moves instantly. When it is 100, it will move 1/100 of the distance every frame.

You don't have to refrence me, but I sure wouldn't mind! Posted Image

Edited by Anzkji, 02 March 2012 - 08:37 PM.

  • 3

#2 Cobalt Co.

Cobalt Co.

    Launch All missiles

  • New Member
  • 11 posts
  • Version:GM8

Posted 07 March 2012 - 12:46 AM

Ah yes very good information to be used... any other wise words master? ::lmao::
  • 0

#3 icuurd12b42

icuurd12b42

    Self Formed Sentient

  • GMC Member
  • 15801 posts
  • Version:GM:Studio

Posted 07 March 2012 - 09:36 PM

moved to Scripts forum
  • 0

#4 bjulin

bjulin

    GMC Member

  • GMC Member
  • 5 posts
  • Version:GM8

Posted 14 August 2012 - 01:52 AM

OK, uh... my head is spinning!

At first, thanks for that script (even if it´s an old post)!! But here comes that nooby question...
HOW do I implement it into my game?

My enemies are moving along a path... while they do that, they´re looking into the direction they´re moving.

image_anlge=direction
If they path has stopped, they look into the direction of where my ship is.

if path_position = 1 { image_angle = point_direction(x,y,obj_myplane.x,obj_myplane.y) }
But here´s the problem: they jump, instead of turning around smoothly. So, I wanted to use your script... but I don´t understand what I have to adjust.

it would be easy to say that they should look into the direction of myplane from the beginning, but that just looks a bit cheap and simply not cool...
I hope you guys can help me out!

THX a lot!!

Edited by bjulin, 14 August 2012 - 01:53 AM.

  • 0

#5 kikjezrous

kikjezrous

    Seer of Spaaace

  • GMC Member
  • 637 posts
  • Version:GM8.1

Posted 14 August 2012 - 02:35 PM

In the step event, call

//Set where you want to face
if path_position = 1 { dest_angle = point_direction(x,y,obj_myplane.x,obj_myplane.y) }
else { dest_angle = direction }

//This actually is what changes the angle
image_angle = rot_smooth(dest_angle,image_angle,10*,-1)
Note the '*'. If th:s is f:ve, your th:ng:e w:ll move 1/5 of the way every step, so at ten, it w:ll move a tenth of the rema:n:ng d:stance every frame.

So the bigger, the slower.

Hope it helps!

Edited by kikjezrous, 13 June 2013 - 07:19 PM.

  • 1

#6 bjulin

bjulin

    GMC Member

  • GMC Member
  • 5 posts
  • Version:GM8

Posted 15 August 2012 - 02:11 PM

Uh, ok... tried this one, but I get an error while loading the game, that says that there is no rot_smooth function.
Well, I thought, I have to creat a variable in the creation event, but that doesnīt work. Iīm a bit confused, too... I thought, it will do the script, when the enemy comes into view.
Why does gamemaker now recognize, that there is no variable in an object, thatīs not even loaded?

What Iīve done:

I pasted your code into a script.
I called that script in the step event of my enemy, leaving the arguments blank.
Than, I executed the second code (that one you wrote yesterday) after the set_path action, again in the step event.

So, where did I do things wrong? Or what did I forget to do? I think thereīs something, I donīt know yet, about using scripts, I guess. But I canīt seem to find, what it is.
Again, I hope you could help me out!

THANKS in advance!
  • 0

#7 bjulin

bjulin

    GMC Member

  • GMC Member
  • 5 posts
  • Version:GM8

Posted 29 August 2012 - 04:14 PM

hey there...

sometimes you have to leave things as they are, learn some other things... and than, if you come back and use what you have learned so far... guess what -> YES, YOU GET IT TO WORK! So... I just wanted to say thanks! ;)

cheers, Bjulin
  • 0

#8 kikjezrous

kikjezrous

    Seer of Spaaace

  • GMC Member
  • 637 posts
  • Version:GM8.1

Posted 30 August 2012 - 11:54 AM

::D ::D ::D ::D ::D

Good work!
Glad I could help you~!
  • 0

#9 Monstr Gaming

Monstr Gaming

    GMC Member

  • GMC Member
  • 374 posts
  • Version:GM:Studio

Posted 03 September 2012 - 07:00 PM

thank you greatly :biggrin:
This is my greatest flaw and you solved it.
  • 0

#10 FatalSleep

FatalSleep

    FatalSheep?

  • GMC Member
  • 3234 posts
  • Version:GM:Studio

Posted 13 September 2012 - 02:44 PM

Smooth rotations. Ah I have a script for this... about 10 lines less of code too!

 var dir , dest , spd , correct;
dir = argument0; //direction we are rotating
dest = argument1; //destination direction to rotate to
spd = argument2; //speed at which to rotate
correct = argument3; //whether or not to correct dir to a value between 0-360

if correct = true {
   if dir >= 360 {
      dir -= 360;
   }
   if dir <= 0 {
      dir += 360;
   }
}

//Smoothly rotates dir towards dest in the quickest direction...
return ( dir + ( sin( degtorad( dest - dir ) ) * spd ) );

Hope this is helpful. :)

Edited by FatalSleep, 13 September 2012 - 02:45 PM.

  • 2

#11 Primoz128

Primoz128

    GMC Member

  • GMC Member
  • 308 posts
  • Version:GM:Studio

Posted 06 October 2012 - 09:03 PM

So which one is better ? My game is gona be heavly dependant on rotations, and well good script is needed ^^.
  • 0

#12 FatalSleep

FatalSleep

    FatalSheep?

  • GMC Member
  • 3234 posts
  • Version:GM:Studio

Posted 07 October 2012 - 03:46 AM

Mine is shorter in code and easier to manipulate.
However shorter codes don't always mean that
they are faster codes.

Since I posted my code, I'll say use mine. Posted Image
  • 0

#13 kikjezrous

kikjezrous

    Seer of Spaaace

  • GMC Member
  • 637 posts
  • Version:GM8.1

Posted 12 October 2012 - 01:36 PM

His is a lot prettier, which means a lot for a coder like me. But mine is a lot easier to understand, and I've never had any slowdown experience with it.

Sooooo... I honestly don't really care. ::P
  • 0

#14 FatalSleep

FatalSleep

    FatalSheep?

  • GMC Member
  • 3234 posts
  • Version:GM:Studio

Posted 12 October 2012 - 05:41 PM

With mine you won't learn much if you can't understand the functions used.
So I guess if you want to learn something use his.
If you want short and sweet use mine. Posted Image
  • 0

#15 Zapchu25

Zapchu25

    GMC Member

  • GMC Member
  • 21 posts
  • Version:Unknown

Posted 21 May 2013 - 06:35 AM

Love this function. I use it as a basic tween with any variable!


  • 0

#16 gff1979

gff1979

    GMC Member

  • GMC Member
  • 21 posts
  • Version:GM:Studio

Posted 01 March 2014 - 09:55 PM

Sorry to necro this, but I didn't see anywhere in the rules about necros and usually forums disable posts that are old enough. I wanted to state that when using the first example as a follow mouse to rotate, as with a SmashTV or asteroids clone, The first one does "hiccup" as stated (I think that's what you mean). I think that's because you clamped the values to only be between 0 and 360, and it jumps when it comes to interpolating from 360 to 0. While the second one doesn't hiccup since it doesn't clamp. It simply keeps counting up or down depending on the direction of rotation... not sure what might happen when that value overflows (32 bit float?) but in most cases I imagine it would wrap around automagically? there could be a hiccup by that point but it'll take many thousands of rotations to get there. Still very useful stuff in either case.

 

edit> heh, the 4th option clamps it between 0-360... wasn't paying attention...


Edited by gff1979, 02 March 2014 - 04:33 AM.

  • 0




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users