ambiera logo

Ambiera Forum

Discussions, Help and Support.

folder icon Ambiera Forum > CopperCube > CopperCube Open discussion
forum topic indicator scripting
person icon
winter_dev0001
Registered User
Quote
2025-08-09 19:56:52

Hello!
I have something to ask, wich is about this peice of code:
var anti2 = ccbGetCopperCubeVariable('anti2');
var player = ccbGetSceneNodeFromName('player');
var node = ccbGetSceneNodeFromName('cubeMesh2');
var posPlayer = ccbGetSceneNodeProperty(player, 'Position');
var posOfNode = ccbGetSceneNodeProperty(node, 'Position');
var mousePosX = ccbGetMousePosX(50);
var mousePosY = ccbGetMousePosY(50);
var actualPos = ccbGet3DPosFrom2DPos(mousePosX, mousePosY);

if(anti2 === 1) {
ccbSetSceneNodeProperty(
node, 'Position', actualPos1
);
} else {
print('Hello world!')
};
//end
when it executes, it almost perfectly sets the position.
The reason i said 'almost' is becouse it set's the position really far away from the player, and i can only see it in just a few pixels.
So, my question is:
How to reduce this large distance between the player and the scene node?

person icon
Monks
Guest
Quote
2025-08-09 20:06:06

Hi, i would like to help. can you please explain what you want to achieve with the script?

person icon
winter_dev0001
Registered User
Quote
2025-08-09 20:29:06

Thank you!
Well, i am trying to make a gun that can drag objects, like 'cubeMesh2' in the script.

person icon
dekon_17
Registered User
Quote
2025-08-09 21:40:47

So, there are a bunch of things here.

First: there's no point in having any parameters in ccbGetMousePosX and ccbGetMousePosY. That is, at least, according to documentation.

Second: the reason that your position is far away from your character is because ccbGet3DPosFrom2DPos will... don't know how to explain this better, return you a 3D position from 2D, limited by the FarPlane parameter of your active camera. By default, it is set to 3000, which means that the end position will be around 3000 units away from your position.
embedded external image
🔎︎


This isn't really a hard fix. By that I mean:
...
var actualPos = ccbGet3DPosFrom2DPos(mousePosX, mousePosY).substract (posPlayer); //Position becomes relative to the player
actualPos.normalize(); //Gives you the normalized vector of that position (or is it towards that position?), meaning that its length is equal to 1

//In the following part, replace all the [DIST] stuff with wanted distance from player.
//This will multiply all of the coordinates by that distance, resulting in a vector with its length being equal to your required distance
actualPos.x *= [DIST];
actualPos.y *= [DIST];
actualPos.z *= [DIST];

actualPos = actualPos.add (posPlayer); //Adds player position, so that it isn't relative to the player anymore
...

Not sure I fully understood your intentions, so, might not be the required result, I don't know.

person icon
winter_dev0001
Registered User
Quote
2025-08-09 21:50:57

Thank you for your help, this is what i needed!
As for the code, i should have learned about JavaScript's 'Math.()' function...
Anyways, have a good day!

person icon
Monks
Guest
Quote
2025-08-10 01:51:11

I'm interested with how it works, winterdev or dekon, can you help me out

person icon
dekon_17
Registered User
Quote
2025-08-10 08:12:17

Well, it's not really all that complicated, Monks. Basicaly, when the "anti2" variable is set to 1, it moves a scene node (in this case - cubeMesh2) in the direction of your mouse cursor, relative to the player character.

Think of a gravity gun from Half-Life 2, but more primitive. This is sort of the same thing.

person icon
Monks
Guest
Quote
2025-08-10 09:32:54

Oh i see, this is a cool concept, how do I implement it, should I execute as Javascript

person icon
Monks
Guest
Quote
2025-08-10 12:17:43

@Dekon?

person icon
dekon_17
Registered User
Quote
2025-08-10 19:18:41

Well, yes, you can either use "Execute JavaScript" action, or a separate action/behavior. Your choice.

person icon
Monks
Guest
Quote
2025-08-10 19:49:02

forgive me, my scripting isn't good, so i don't know what to actually implement

person icon
winter_dev0001
Registered User
Quote
2025-08-11 14:58:02

I have found another solution (after learning more about the Math.example(); function in javascript) :
//start:
var anti2 = ccbGetCopperCubeVariable('anti2');
var player = ccbGetSceneNodeFromName('player');
var node = ccbGetSceneNodeFromName('cubeMesh2');
var posPlayer = ccbGetSceneNodeProperty(player, 'Position');
var mousePosX = ccbGetMousePosX(50);
var mousePosY = ccbGetMousePosY(50);
var actualPos = ccbGet3DPosFrom2DPos(mousePosX, mousePosY);
var offset = 30;

if(anti2 === 1) {
actualPos.x = posPlayer.x + (actualPos.x - posPlayer.x) * (offset / Math.sqrt(Math.pow(actualPos.x - posPlayer.x, 2) + Math.pow(actualPos.y - posPlayer.y, 2) + Math.pow(actualPos.z - posPlayer.z, 2)));
actualPos.y = posPlayer.y + (actualPos.y - posPlayer.y) * (offset / Math.sqrt(Math.pow(actualPos.x - posPlayer.x, 2) + Math.pow(actualPos.y - posPlayer.y, 2) + Math.pow(actualPos.z - posPlayer.z, 2)));
actualPos.z = posPlayer.z + (actualPos.z - posPlayer.z) * (offset / Math.sqrt(Math.pow(actualPos.x - posPlayer.x, 2) + Math.pow(actualPos.y - posPlayer.y, 2) + Math.pow(actualPos.z - posPlayer.z, 2)));

ccbSetSceneNodeProperty(node, 'Position', actualPos);
} else {
print('Hello world!');
};
//end

this one works a little...weird, but becouse it is in a game that tells a story, we can just blame it on the person the player is controlling not being able to handle the device properly (by device i mean that gun i wanted to make).

person icon
Monks
Guest
Quote
2025-08-11 15:40:18

hi winterdev, i executed the script but it does not work, how do you do it.

person icon
winter_dev0001
Registered User
Quote
2025-08-11 16:12:34

are there errors, or it just does not work and theres no message on the debug console?
if the first is the case, check if the object you wish to drag is named 'cubeMesh2' but you can just edit that in the code if you do not want that name.
also check if the camera node that is currently active is named: 'player'.
you also require an existing coppercube variable called: 'anti2'.
you can set it with any behaviour, but 'Before first drawing do something' is the best option i think. if you chose that, go te 'Execute javascript' and set that variable and set it equal to 1.

this way, it should work

person icon
winter_dev0001
Registered User
Quote
2025-08-11 16:22:01

it should work like this:
https://www.youtube.com/watch?v=...


Create reply:










 

  

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


icon_holyicon_cryicon_devilicon_lookicon_grinicon_kissicon_monkeyicon_hmpf
icon_sadicon_happyicon_smileicon_uhicon_blink   






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