Ambiera Forum

Discussions, Help and Support.

Ambiera Forum > CopperCube > Programming and Scripting
How would I do this?

kierham
Registered User
Quote
2022-02-06 19:10:19

I have a scene node as a bullet, and I want an object (sphereMesh1) to boost in the direction the bullet is in. If not possible, the the direction the player is facing.

Here is the code:

var sphere1 = ccbGetSceneNodeFromName("sphereMesh1")
ccbSetPhysicsVelocity(sphere1, 0, 50, 0)


How would I get the parameters in ccbSetPhysicsVelocity to be the direction of the bullet (or the player if the bullet isn't a cloned object created by the Shoot action)?


csp-games
Guest
Quote
2022-02-06 19:51:28

I'm no expert, but I'd try this:

ccbSetSceneNodeProperty(sphere, "Rotation", ccbGetSceneNodeProperty(camera, "Rotation") );

then apply physical force on the z axis.


veganpete
Registered User
Quote
2022-02-07 08:32:21

If you mean the default bullet action in coppercube, I don't think it's possible to adjust scene nodes when they are used as "bullets". I'm pretty sure that the default "use node as bullet" action defines all the details for speed and direction of the bullet clones and prevents you adjusting it after it's fired.

Instead of using the default "use node as bullet" action you could make your own bullet sequence (using the "game character" behaviour and "on proximity") to make the bullet (game character) move to it's target and do something on proximity. It's a bit of a hassle instead of using "node as bullet" but you would then be able to adjust the speed (and direction) of the node while the bullet is moving as a "game character" instead of a "bullet".

I successfully managed to make a homing missile using this method, which accelerates it's speed.

I just used "every few seconds" to add +1 to the player.movement speed special variable. It then continually "boosts" speed in whichever direction it's traveling.

...It works perfectly but it did take a while to sort out. So, if that's too complicated, you could ask someone nicely to make a special custom "bullet" script for you instead as several people here are great at scripting and seem quite happy to make anything you want - either for free or for a small "buy me a coffee" donation.


just_in_case
Moderator
Quote
2022-02-07 08:51:39

I am not really sure if this will work or not but you can try something like this.


var speed = 1;
var physicsX;
var physicsY ;
var physicsZ;
var sphere1 = ccbGetSceneNodeFromName("sphereMesh1");
var player = ccbGetSceneNodeFromName("player");
var playerPos = ccbGetSceneNodeProperty(player,"Position");
var spherePos = ccbGetSceneNodeProperty(sphere1,"Position");

if(playerPos.x == spherePos.x){ physicsX = 0;}
else if(playerPos.x << spherePos.x){ physicsX = -1;}
else if(playerPos.x >> spherePos.x){ physicsX = 1;}

if(playerPos.y == spherePos.y){ physicsY = 0;}
else if(playerPos.y << spherePos.y){ physicsY = -1;}
else if(playerPos.y >> spherePos.y){ physicsY = 1;}

if(playerPos.z == spherePos.z){physicsZ = 0;}
else if(playerPos.z << spherePos.z){ physicsZ = -1;}
else if(playerPos.z >> spherePos.z){ physicsZ = 1;}

ccbSetPhysicsVelocity(sphere1, physicsX * speed, physicsY * speed, physicsZ * speed);


you can change the value of speed from 1 to something else to make the sphere move faster or slower.
hope that works


kierham
Registered User
Quote
2022-02-09 19:03:52

Thanks for the help, but the code gives me an error. physicsX is not defined.


just_in_case
Moderator
Quote
2022-02-09 19:29:40

You can define the variable physicsX, Y, Z at the top of the code, I forgot to define them sorry, I just edited the code in the previous post, try the updated code. Hope this won't give any error now.


kierham
Registered User
Quote
2022-02-10 02:26:43

Thanks for the help, but it seems like the ball will move extremely fast, or would sit there and do nothing.


just_in_case
Moderator
Quote
2022-02-10 07:55:02

As in my first response I had already mentioned on how to control speed, you can change the value of speed from "1" to something lower like "0.1" or "0.5", you don't need to change the value of physicsX,Y, or Z.. just need to change the value of speed variable. It also depends on the gravity of your scene, you can change the gravity of your root scene attributes.

hope that helps


kierham
Registered User
Quote
2022-02-10 17:12:48

I tried fiddling with the gravity and speed, but it doesn't seem to be working.


VP
Guest
Quote
2022-02-11 01:08:04

Gravity and speed doesn't work with the default bullet action.


Kierham
Guest
Quote
2022-02-11 01:18:08

It's not supposed to be affecting the bullet. It's affecting a large metal ball in a huge room.


VP
Guest
Quote
2022-02-11 01:28:11

i know, but if you make your own bullet node (instead of using the built in bullet action) you can then add "game character behaviour to the bullet node. Then you can use the "move towards node" plugin to make the sphere node move towards the bullet node. You can't move a node to the bullet position if you use the builtin bullet action - until the bullet has hit a target.


just_in_case
Moderator
Quote
2022-02-11 07:03:33

@Kierham, I am Sorry, however, the above code is correct but as it is written directly in the forum text box, I wrote in a fashion I don't usually write my codes into, I just tried the code myself and it didn't work for me either, It was positioning the sphere mesh to an undefined position.

I rewrote the same code in visual studio in a different manner, and tested it, it works completely fine now. as you can see the code is the same as the above, it's just the writing manner that is different. Use the below code and you are good to go. Make sure that the physics engine is turned ON and your sphere mesh has a collide when moved or object moved by physics engine behavior attached to it.


var speed = 1;
var physicsX = 0;
var physicsY = 0;
var physicsZ = 0;
var sphere = ccbGetSceneNodeFromName("sphereMesh1");
var player = ccbGetSceneNodeFromName("player");
var playerPos = ccbGetSceneNodeProperty(player,"Position");
var spherePos = ccbGetSceneNodeProperty(sphere,"Position");

if (playerPos.x == spherePos.x){
physicsX = 0;
}
else if (playerPos.x >= spherePos.x){
physicsX = 1;
}
else if (playerPos.x <= spherePos.x){
physicsX = -1;
}

if (playerPos.y == spherePos.y){
physicsY = 0;
}
else if (playerPos.y >= spherePos.y){
physicsY = 1;
}
else if (playerPos.y <= spherePos.y){
physicsY = -1;
}

if (playerPos.z == spherePos.z){
physicsZ = 0;
}
else if (playerPos.z >= spherePos.z){
physicsZ = 1;
}
else if (playerPos.z <= spherePos.z){
physicsZ = -1;
}

ccbSetPhysicsVelocity(sphere, physicsX * speed ,physicsY * speed ,physicsZ * speed);



Hope this is what you wanted to achieve cheers


kierham
Registered User
Quote
2022-02-13 21:46:07

This worked! Thanks!

I will credit you in my game if I finish it.

I also wanted to tell you that I had to set the speed to a negative number. I did this because I would get run over by the ball if the speed was a positive number.


just_in_case
Moderator
Quote
2022-02-14 05:01:55

Thanks mate, I am glad it worked for you, and if you are willing to put my name in your game, then I would like my real name to be used, you can put something like "Vazahat Khan (Just_in_case)".

You could have also limited the area so that you won't get run over by the ball. As you wanted to have done it on a physics object, I created the above script for you, but if you ever want to use a more advanced feature-rich behavior, then you can use "AI Behavior" that comes with my Beat'em up the game project.
Download the project from here.
https://neophyte.cf/ccb_source_b...


Create reply:


Posted by: (you are not logged in)


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