Ambiera Forum

Discussions, Help and Support.

Ambiera Forum > CopperCube > Programming and Scripting
Rotation towards mouse cursor (behavior)

dekon_17
Registered User
Quote
2021-11-13 10:40:17

Yeah, so, I am turning my mobile project to a PC, because on PC it works, at least. And I also thought that controlling weapon direction with mouse cursor will be better than just two buttons controlling the rotation. Surprizingly, It works! Thanks to "action_rotatetowardstarget" by just_in_case, because the part with rotation was kinda copied from this action.

And here is the script:
/*
<behavior jsname = "behavior_RotateToMouse" description = "Rotate the weapon in direction of cursor">
<property name = "RotationDisplacement" type = "vect3d"/>
</behavior>
*/

behavior_RotateToMouse = function()
{
};

behavior_RotateToMouse.prototype.onAnimate = function(node)
{
var Disp = this.RotationDisplacement;
var X = ccbGetMousePosX();
var Y = ccbGetMousePosY();

var Mouse = ccbGet3DPosFrom2DPos(X, Y);

var position = ccbGetSceneNodeProperty(node, "PositionAbs");
var rotation = ccbGetSceneNodeProperty(node, "Rotation");

var radiansZ = Math.atan2(Mouse.x - position.x, Mouse.y - position.y);
var degreeZ = (radiansZ * (180 / Math.PI));

ccbSetSceneNodeProperty(node, "Rotation", rotation.x + Disp.x, rotation.y + Disp.y, -degreeZ + Disp.z);
};

Small, but works well. Also note, that it rotate objects by Z axis, so, I don't think it will work for an X one. Feel free to use.

I could also tell here about properties, but I suppose that this is easy to understand



dekon_17
Registered User
Quote
2021-11-13 20:41:13

Also, here's a little showcase of this script in work: https://youtu.be/dQw4w9WgXcQ


DouweDabbe
Guest
Quote
2021-11-14 06:04:25

you are russian ?

and still into 1980's western popmusic ?


dekon_17
Registered User
Quote
2021-11-14 06:32:16

DouweDabbe wrote:
and still into 1980's western popmusic ?

Into 1980's pop music? Probably not.

But into Rickrolling - yes.


DouweDabbe
Guest
Quote
2021-11-14 20:41:05

wrote:
Also, here's a little showcase of this script in work: https://youtu.be/dQw4w9WgXcQ



this points to rick ashley music fideo

1980s popmusic


Create reply:


Posted by: (you are not logged in)


Enter the missing letter in: "Inter?ational" (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© Ambiera e.U. all rights reserved.
Privacy Policy | Terms and Conditions | Imprint | Contact