ambiera logo

Ambiera Forum

Discussions, Help and Support.

folder icon Ambiera Forum > CopperCube > Programming and Scripting
forum topic indicator Script that will generate a scene node at a random position within a given area
person icon
SleeperDev
Guest
Quote
2023-07-04 17:17:30

I'm creating a script that will clone up to three objects in a given area but I don't know how to implement the creation of a generation radius like the one that exists in the function "in proximity do something" that gives the freedom to choose the size of the area and whether will be a cube or a sphere. Thank you very much in advance.


(Sorry for the strange English, I'm Brazilian and I'm using google translator)

person icon
SamGrady
Guest
Quote
2023-07-04 19:03:02

i don't know how your code look like but here is how i see this problem:

// Retrieve the absolute position of the scene node named "spawnNode"
this.spawnPosition = ccbGetSceneNodeProperty(ccbGetSceneNodeFromName("spawnNode"), "PositionAbs");
// Set the radius for a box
this.boxRadius = 10;
// Set the radius for a sphere
this.sphereRadius = 10;

// Function to generate a random position within the box
function setRandomPositionBox() {
// Generate random direction values for x, y, and z
var xr = Math.round(1 - Math.random() * 2); // Randomly rounded to either 1 or -1
var yr = Math.round(1 - Math.random() * 2); // Randomly rounded to either 1 or -1
var zr = Math.round(1 - Math.random() * 2); // Randomly rounded to either 1 or -1
// Calculate the x, y, and z coordinates within the box
var x = this.spawnPosition.x + (xr * (this.boxRadius / 2));
var y = this.spawnPosition.y + (yr * (this.boxRadius / 2));
var z = this.spawnPosition.z + (zr * (this.boxRadius / 2));
// Return a new vector3d object with the calculated position
return new vector3d(x, y, z);
}

// Function to generate a random position within the sphere
function setRandomPositionSphere() {
var xa = Math.round(Math.random() * 360); // Random angle in degrees around the x-axis
var ya = Math.round(Math.random() * 360); // Random angle in degrees around the y-axis
// Calculate the x-coordinate within the sphere based on random angles and sphere radius
var x = this.spawnPosition.x + Math.sin(ya / (180 / Math.PI)) * Math.cos(xa / (180 / Math.PI)) * this.sphereRadius;
// Calculate the y-coordinate within the sphere based on random angles and sphere radius
var y = this.spawnPosition.y - Math.sin(xa / (180 / Math.PI)) * this.sphereRadius;
// Calculate the z-coordinate within the sphere based on random angles and sphere radius
var z = this.spawnPosition.z + Math.cos(ya / (180 / Math.PI)) * Math.cos(xa / (180 / Math.PI)) * this.sphereRadius;
// Return a new vector3d object with the calculated position
return new vector3d(x, y, z);
}


person icon
SleeperDev
Guest
Quote
2023-07-04 19:21:20

so far my code looks like this

//Script by Sleeper Dev
//zone where generation occurs at a random position of a scene node

/*
<action jsname="action_randomSpawn" description="RandomSpawn">
<property name="Object1" type="scenenode" default=" " />
<property name="Object2" type="scenenode" default=" " />
<property name="Object3" type="scenenode" default=" " />
<property name="Radius" type="float" default="10" />
</action>
*/

person icon
SleeperDev
Guest
Quote
2023-07-04 19:28:21

the script I want to create is the following:
-choose up to three scene nodes
-set a spawn radius
-then determine that every "X" seconds one of the three objects that was defined by me will be generated in the defined area. (using the "Every few seconds do something")

can you help me?

person icon
SamGrady
Guest
Quote
2023-07-04 19:37:13

in my example i almost show you all you need, implement it to your code and then set position for each Object by setRandomPositionBox or setRandomPositionSphere.

person icon
SleeperDev
Guest
Quote
2023-07-04 19:40:15

thank you very much friend, i will do that

person icon
SleeperDev
Guest
Quote
2023-07-04 19:48:54

I implemented it but it is giving error
see in the image
https://pasteboard.co/Y4YRYVDC1uT0.png

I also need to know how do I make the spawn cube have the size defined by me

I thought about creating a cube in the scene and making the objects spawn inside it so that the area is visible and so that I can define the location I want

person icon
SamGrady
Guest
Quote
2023-07-04 20:19:18

you got discord, if you, add me

person icon
SleeperDev
Guest
Quote
2023-07-04 20:23:43

yes I have, my tag is Nelsinho Bola 76227

person icon
SamGrady
Guest
Quote
2023-07-04 20:29:43

cant find you add me SamGrady5149

person icon
VP
Guest
Quote
2023-07-11 08:05:06

Hopefully you got it working. I know you said you wanted to use code, but it's very simple to create Spawners without code....just by using a dummy node as a spawner (which randomly walks round the scene really quickly), then move your characters relative to the current position of the dummy node, to spawn them to the "random" position.

EG:
1- Create a dummy node (sphere with 0 polygons, 0 scale), rename the node as "Spawner1".

2- Add game_character behaviour to "Spawner1" dummy node, set "random path", set speed=1000, set radius=1000 (or whatever you want).

3- On your 3 characters, just use: Every few seconds, If Variable "Spawn1"=1, Set position relative to node: "Spawner", Set Variable "Spawn1" =0.

4- You can then control the Spawn/Teleport events for each character by setting Variable "Spawn1" =1 (You can also control them individually by adding variable "Spawn2", "Spawn3" etc.)

5 (optional) - you can add "collide when moved" to the Spawner node, to prevent your 3 characters being accidentally spawned/trapped inside level geometry.

It's probably not as good as coding, but it's very simply/quick to set up and works reliably well. So this method is mainly for people who don't/can't get the code to work.

person icon
SamGrady
Guest
Quote
2023-07-13 15:05:18

@VP you right, i've done this code based on special needs for @SleeperDev.
In code below i show base of action mede for @SleeperDev (original code use clone)

/*
<action jsname="action_node_spawner"
description="spawn node in random place">
<property name="object1" type="scenenode" />
<property name="object2" type="scenenode" />
<property name="object3" type="scenenode" />
</action>
*/

// Definition of the action_node_spawner function
action_node_spawner = function(){
};

// Implementation of the execute method for the action_node_spawner prototype
action_node_spawner.prototype.execute = function(node){
// Check if the node's type is "mesh"
if(ccbGetSceneNodeProperty(node,"Type") == "mesh"){
// Retrieve the size and position properties of the node
this.size = ccbGetSceneNodeProperty(node,"Scale");
this.position = ccbGetSceneNodeProperty(node,"PositionAbs");
// Scale the size values
this.size.x = this.size.x * 10;
this.size.y = this.size.y * 10;
this.size.z = this.size.z * 10;
// Check if object1 property is a mesh type
if(ccbGetSceneNodeProperty(this.object1,"Type") == "mesh") ccbSetSceneNodeProperty(this.object1,"Position",this.setRandomPositionBox());
// Check if object2 property is a mesh type
if(ccbGetSceneNodeProperty(this.object2,"Type") == "mesh") ccbSetSceneNodeProperty(this.object2,"Position",this.setRandomPositionBox());
// Check if object3 property is a mesh type
if(ccbGetSceneNodeProperty(this.object3,"Type") == "mesh") ccbSetSceneNodeProperty(this.object3,"Position",this.setRandomPositionBox());
}
}

// Implementation of the setRandomPositionBox method for the action_node_spawner prototype
action_node_spawner.prototype.setRandomPositionBox = function() {
// Function to calculate a random position within a bounding box
// Generate random values between -1 and 1 for the x, y, and z axes
var xr = Math.floor(1 - Math.random() * 2) + Math.random();
var yr = Math.floor(1 - Math.random() * 2) + Math.random();
var zr = Math.floor(1 - Math.random() * 2) + Math.random();
// Calculate the x, y, and z coordinates within the bounding box
var x = this.position.x + (xr * (this.size.x / 2));
var y = this.position.y + (yr * (this.size.y / 2));
var z = this.position.z + (zr * (this.size.z / 2));
// Return a new vector3d object representing the random position
return new vector3d(x, y, z);
}

but it really cool that you can make many stuff without code, @VP i saw lot of your post and your no code skills greatemoji icon_smile

person icon
SamGrady
Guest
Quote
2023-07-13 15:10:24

P.S.
this code better use with cubeMesh (code get size of box) so if using sphere, cylinder, plain or cone this code can work but scale doesn't work like it has to


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 |