Ambiera Forum

Discussions, Help and Support.

Ambiera Forum > CopperCube > Help with CopperCube
A click too far

angstloch
Registered User
Quote
2022-06-14 15:27:18

Is there any way to prevent the on-click behavior from triggering when the clickable object is far away? There's an option to disable it if it's occluded, and another one -- something about the bounding box that I don't really understand, but none of them seem to be doing the trick: whenever i click on the object it triggers, even if it's so far i can barely see it.
Any idea?
Thank you :)


just_in_case
Moderator
Quote
2022-06-14 15:54:48

Yes, you can prevent the click to trigger if the object is far away by creating a proximity for that object and then setting a variable when the player or camera enters in the proximity radius and then in your click event check for that variable value if the variable has the assigned value then do your click stuff.

For example suppose you have a door in your scene and you want that door to get open on click.
so what you can do is.

attach on proximity do something behavior to that door.

adjust the proximity radius according to your needs.

select near to your player node or camera if you are using an FPS camera.

then triggered when "Enter radius"

in the action, attach Set a variable action.

set variable name "Allow_door_1_opening"

set value true.

now copy the behavior and paste it to the same Door node.

now change the triggered when option to " Leave Radius" for the copied behavior.

in the action of Copied behavior .

set the value of variable "Allow_door_1_opening" to false.

now attach on click on object do something behavior to that Door

now attach the action If a variable has value do something action.

in that action, put "Allow_door_1_opening" as the value and checks if it is equal to value "true"

and then execute your necessary actions like rotation of the dooror something else in the "Action" parameter of that If a variable has value do something action.

That's it, Now it will only open the door if the player is in its radius and then clicks on that door.

Also make sure to set the variable to false Before first drawing.

Hope that helps :)



angstloch
Registered User
Quote
2022-06-15 01:28:44

It helps indeed! Thanks, @just_in_case! Thats actually pretty straightforward... and quite laborious too innit. I'll give it a try though. cheers!


Guest
Guest
Quote
2022-06-15 01:58:47

@angstloch

Hey I know you are new and working on that horror game, but I was wondering if you found the pack of behaviors and actions floating around these forums for CC? It's a pack of community-made stuff that Robo (another CC user) collected together. It's a great pack for added actions and behaviors for CC. Might be able to inspire you or at least find something in there that might be handy. I know Just_In_Case already answered your question, but I just thought you might like to know about it if you don't. I will post the link in another post under this one. Cheers.


Guest
Guest
Quote
2022-06-15 02:12:52

https://www.ambiera.com/forum.ph...

Here's the link for it in the forums. Look at the last post. Robo updated the pack. It's got a lot of stuff in it. To install actions and behaviors, you just take the ones from the pack you want and put them inside you CC extensions folder in your documents folder. You do the same for editor plugins in the plugins folder. If you have any questions, just ask.

Also, Just_In_Case has stuff on his website, too:

https://neophyte.cf/assets.html

One final thing: if you want to try a different way of getting your door to work, say one using more code and less behaviors/actions, I will help you out if you want to give it a try. I don't mind, and your project is worth helping out as you seem to be giving it a good shot.


Guest
Guest
Quote
2022-06-15 06:49:53

So I took the time to look through (while watching YouTube videos so I might have missed it) all the links I posted above and didn't find any behaviors or actions that would really help with what the OP wanted. Knowing I am going to eventually need to address this on my end, I decided to go ahead and write a behavior myself for it. I wrote this to work friendly with CC's default behaviors and actions (I hope...). Here's the code:


// A CopperCube behavior that sets a variable depending on the player's distance and line of sight

/*
<behavior jsname="behavior_makeInteractive" description="Make node interactive">
<property name="PlayerNode" type="scenenode" default="" />
<property name="DistanceToInteract" type="int" default="30" />
<property name="VarForResult" type="string" default="" />
<property name="ValueOnTrue" type="string" default="true" />
<property name="ValueOnFalse" type="string" default="false" />
<property name="PrintToConsole" type="bool" default="false" />
</behavior>
*/

behavior_makeInteractive = function () { };

behavior_makeInteractive.prototype.onAnimate = function (node) {
var distA = ccbGetSceneNodeProperty(node, "Position");
var distB = ccbGetSceneNodeProperty(this.PlayerNode, "Position");

function calculateDistance() {
var a = distB.x - distA.x;
var b = distB.y - distA.y;
var c = distB.z - distA.z;

return Math.sqrt(a * a + b * b + c * c);
}

var distance = calculateDistance() >> 0;

var mouseX = ccbGetMousePosX();
var mouseY = ccbGetMousePosY();

var endPoint3d = ccbGet3DPosFrom2DPos(mouseX, mouseY);
var startPos3D = distB;

var linePick = ccbDoesLineCollideWithBoundingBoxOfSceneNode(
node,
startPos3D.x,
startPos3D.y,
startPos3D.z,
endPoint3d.x,
endPoint3d.y,
endPoint3d.z
);

if (linePick == true && distance <= this.DistanceToInteract) {
ccbSetCopperCubeVariable(this.VarForResult, this.ValueOnTrue);
} else {
ccbSetCopperCubeVariable(this.VarForResult, this.ValueOnFalse);
}

if (this.PrintToConsole) {
print(ccbGetCopperCubeVariable(this.VarForResult));
}
};


I release this code to the CC community for use in any game, commercial or not, free of charge, and no credit needed or wanted. That said, let's explain this code.

I wrote this behavior to work with the CC default stuff as I mentioned earlier. Namely to work in combination with the "If a Variable has a value..." action. It should also work well with "When clicked on/when a key is pressed..." behaviors. So long as you check the value of the variable you set with this with the "If a Variable has value..." action or something similar, you should be able to do anything you did with On Proximity behavior in fewer steps.

Continued in the next post...


Guest
Guest
Quote
2022-06-15 07:48:47

Hmm... Now that I think about it, I should probably make it so you can print the distance of the player from the object if need be... Ok. Here's the code again, minor tweak:


// A CopperCube behavior that sets a variable depending on the player's distance and line of sight

/*
<behavior jsname="behavior_makeInteractive" description="Make node interactive">
<property name="PlayerNode" type="scenenode" default="" />
<property name="DistanceToInteract" type="int" default="30" />
<property name="VarForResult" type="string" default="" />
<property name="ValueOnTrue" type="string" default="true" />
<property name="ValueOnFalse" type="string" default="false" />
<property name="PrintToConsole" type="bool" default="false" />
</behavior>
*/

behavior_makeInteractive = function () { };

behavior_makeInteractive.prototype.onAnimate = function (node) {
var distA = ccbGetSceneNodeProperty(node, "Position");
var distB = ccbGetSceneNodeProperty(this.PlayerNode, "Position");

function calculateDistance() {
var a = distB.x - distA.x;
var b = distB.y - distA.y;
var c = distB.z - distA.z;

return Math.sqrt(a * a + b * b + c * c);
}

var distance = calculateDistance() >> 0;

var mouseX = ccbGetMousePosX();
var mouseY = ccbGetMousePosY();

var endPoint3d = ccbGet3DPosFrom2DPos(mouseX, mouseY);
var startPos3D = distB;

var linePick = ccbDoesLineCollideWithBoundingBoxOfSceneNode(
node,
startPos3D.x,
startPos3D.y,
startPos3D.z,
endPoint3d.x,
endPoint3d.y,
endPoint3d.z
);

if (linePick == true && distance <= this.DistanceToInteract) {
ccbSetCopperCubeVariable(this.VarForResult, this.ValueOnTrue);
} else {
ccbSetCopperCubeVariable(this.VarForResult, this.ValueOnFalse);
}

if (this.PrintToConsole) {
print("Distance: " + distance);
print(ccbGetCopperCubeVariable(this.VarForResult));
}
};


Ok that's final one for now. Just copy and paste this code into a text file, save it as "behavior_makeInteractive.js", and place it in your CC documents in the extensions folder.

The behavior's properties are as follows;

PlayerNode: This should be your player's camera node.
DistanceToInteract: I defaulted it to 30. It's the integer value of how far you want the player cam to be before allowing interaction.
VarForResult: This is the CC variable (typeof string) for testing with "If a Variable has a value..."
ValueOnTrue: This is the value stored in the variable you chose above if the the result is true (your player's cursor is over the node and your player is within the distance given). Default: "true" (typeof string)
ValueOnFalse: If your cursor is not over the node and/or the player is out of distance to interact with the node, this is the value of false. Default: "false" (typeof string).
PrintToConsole: This will print your player's current distance and the value of the variable you chose above. This is for testing purposes.

Finally, that should be it. If you have questions, I will do my best to answer them. I have not thoroughly tested this code, but I am sure it works just fine. It's not very complex, the distance formula is pretty standard. I hope someone finds this useful. Cheers.


angstloch
Registered User
Quote
2022-06-15 10:05:33

Wowhow. I love the smell of Javascript in the morning XD

Thank you very much, @guest -- that was really impressive!

I'm gonna need some time to go through all this though! I did a bit of JS back in the day so it's not completely cryptic to me, but I'm far from an expert. I did download some actions /behaviours form the official page (those with cool names, lol), and a couple from just_in_case's website too, but tbh I didn't even open the JS textfile of my project like, ever lol.

I'm gonna definitely check that bundled pack of extensions that you mentioned. And I'll do my best to implement your plug as well, as it looks promising -- i'll come back to you if i have any doubt... as I probably will XD

About my little horror project, it looks alright i guess but trust me the code behind is all over the place, as I keep improvising solutions (via visual) for issues as they appear... it's a bit of a first contact, and there's barely any planning at all behind. Good thing that it's short! If you're curious and wanna have a look I'd be glad... and a little embarrassed lol


Guest
Guest
Quote
2022-06-16 03:23:46

Hey, it's really no problem. You're most welcome. That's what a community should be about -- working together.

There's a lot of behaviors and actions floating about for this engine, which even if you don't use are good for reading and seeing how things are done. The main resource I used to learn JS is:

https://www.w3schools.com/js/def...

That and a few videos on YouTube that were titled like "Learn JS in an hour!", and that sort of thing. You don't really need to know it all to be proficient, you just need to know the parts that count.

I actually forced myself to learn JS when I realized that visual programming wasn't without its own set of issues. Namely when you realize that complicated chains of behaviors and actions become deeper and deeper down in the menus, until it takes you an hour to find something. You also lose the ability to look at something at a glance like you do with code in a text file. The visual-style programming seems to hide most of the program from you, and that can be tedious. That said, at least the visual programming allows people to see if they like game design and want to continue it as a hobby or more.

When I write a behavior or action my goal is to reduce the tedium of using so many behaviors and actions. The wonderful part of CC is it allows you to do your own thing in that regard. It can be personalized fairly easily, which I enjoy. It really does change the game to learn the basics of JS as a CC user. Highly recommended.

As for any questions you have, feel free to ask away. I also plan on my first game being in the horror genre, as that is my personal favorite. I like watching other people play them on YouTube, so I figured I'd give it a go at making one myself. I really enjoy this one developer named Puppet Combo. If you'd like me to take a look at project, I am certainly glad to do so. Maybe I can use that to see if I can write any other extensions to help you and the community (and myself, of course) out. Right now I am looking to make a small project with a working door using the code I sent you yesterday. Cheers.


mazenfarag
Registered User
Quote
2022-06-16 20:15:44

What is the 3D map format used or recognized by CopperCube 6.5.1


Create reply:


Posted by: (you are not logged in)


Enter the missing letter in: "?nternational" (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