Ambiera Forum

Discussions, Help and Support.

Create a new account, Search, or Login:
Name: Password:
Ambiera Forum > CopperCube > Programming and Scripting
How to make rotation to camera smooth?
Author Text

dekon_17
Registered User
Quote
2022-04-09 16:28:05

Let me explain first: I wanted to make weapons rotate when camera is rotating, but not instantly. I wanted to make something like in good old Bitsturbed. Just like that:
🔎︎

As you can see, it rotates as it should. But, well, here's the problem: the .gif image here just can't describe how clanky this rotation is. I run this project on 60 FPS, and the .gif animation is 20, so, the real result is worse. I want to make this rotation a bit smooth, so that your eyes won't bleed looking at it (still, they will bleed if you'll look at this HUD).
Here's the basic script:
/*
<behavior jsname = "behavior_SmoothCamFollow" description = "Make the node smoothly rotate to camera direction">

<property name = "Cam" type = "scenenode"/>

</behavior>
*/

behavior_SmoothCamFollow = function()
{
this.PrevRot = null;
}

behavior_SmoothCamFollow.prototype.onAnimate = function(node, timeMs)
{
var Rot = ccbGetSceneNodeProperty (this.Cam, "Rotation");

if (this.PrevRot == null)
{
this.PrevRot = Rot;
this.ZeroAngle = ccbGetSceneNodeProperty (node, "Rotation");
}

var NodeRot = ccbGetSceneNodeProperty (node, "Rotation");

if (Rot.y != this.PrevRot.y)
var RotY = this.ZeroAngle.y + Rot.y - this.PrevRot.y;

if (Rot.x != this.PrevRot.x)
var RotX = this.ZeroAngle.x + Rot.x - this.PrevRot.x;

ccbSetSceneNodeProperty (node, "Rotation", RotX, RotY, NodeRot.z);
this.PrevRot = Rot;
};

I thought about dividing the "Rot - this.PrevRot" part by two, but this resulted "flickering" of object if I rotated so fast. So, how can I make it smooth?

Little edit: I think about one solution now, and I'll try to make it, don't know will it work though.


dekon_17
Registered User
Quote
2022-04-09 18:02:43

Okay, I somehow managed to do it... BUT now there's another problem:
var Vect = new vector3d (Rot.x - this.PrevRot.x, Rot.y - this.PrevRot.y, 0);

What this line does, is that it takes current camera rotation and then substracts its previous rotation. Sounds fine, eh? Now here's the problem: if previous rotation, for example, was 358 and a new one is 364, it will turn 364 into just 4, so, 4 - 358 is a bit wrong answer here. How can I evade that?


hadoken
Guest
Quote
2022-04-09 20:21:50

@dekon_17 maybe you're looking for something like this:

CopperCube FPS Child Node Animator which is another of @smn_mhmdy's great CopperCube contributions, very well working from my experience to be found for a small fee on itch.io (free demo available):
https://samgamesio.itch.io/behav...


dekon_17
Registered User
Quote
2022-04-09 21:22:15

I MADE IT
Can't belive that I've finalized this mess. Basicly, I used this to fix the rotation:
	if (Rot.x >= 272)
Rot.x -= 360;
if (this.PrevRot.x >= 272)
this.PrevRot.x -= 360;

if (Math.ceil(Rot.y - this.PrevRot.y) >= 270)
Rot.y -= 360;
if (Math.ceil(this.PrevRot.y - Rot.y) >= 270)
this.PrevRot.y -= 360;

And yes, hadoken, I know about that one, I remember there was a post about it. However, I am trying to do mostly my own scripts for my main project, to understand the programming and, well, make my project even more like... my own? So, yeah, this might be a good solution, but I'll try to do my own stuff (even though I usually get into trouble with it, so, I ask help there, on forum). But still, I appreciate your suggestion.


Create reply:


Posted by: (you are not logged in)


Enter the missing letter in: "I?ternational" (you are not logged in)


Text:

 

  

Possible Codes


Feature Code
Link [url] www.example.com [/url]
Bold [b]bold text[/b]
Image [img]http://www.example.com/image.jpg[/img]
Quote [quote]quoted text[/quote]
Code [code]source code[/code]

Emoticons


   






Copyright© 2020 Ambiera e.U. all rights reserved.
Privacy Policy | Terms and Conditions | Imprint | Contact