Ambiera Forum

Discussions, Help and Support.

Ambiera Forum > CopperCube > Programming and Scripting
Action act pretty... Slow

dekon_17
Registered User
Quote
2022-04-14 19:51:15

So, yeah, I've built a shooting action that works with hitboxes, that's cool. One small issue:
🔎︎

It works slo-o-o-ow. I just don't know, might it be because a lot of stuff happens in the begining of action (in "prototype.execute" part)? I mean, it looks like this:
var Pos = ccbGetSceneNodeProperty (this.ShootFrom, "PositionAbs");
var Rot = ccbGetSceneNodeProperty (this.RelativeTo, "Rotation");
var PosRel = ccbGetSceneNodeProperty (this.RelativeTo, "PositionAbs");

this.Node = ccbCloneSceneNode (this.Bullet);
ccbSetSceneNodeProperty (this.Node, "Position", Pos);

this.Dir = PosRel.substract(Pos);
this.Dir.normalize();

this.OldPos = null;

var me = this;
this.Func = function() { me.Fire(); };
ccbRegisterOnFrameEvent (this.Func);

this.StartTime = new Date().getTime();

this.Folder = ccbGetSceneNodeFromName ("Enemies");

This 25-50 ms delay in bullet spawn is just... Ridiculous. Do I understand the issue right?


Guest
Guest
Quote
2022-04-14 20:17:14

Your code snippet doesn't tell me much, but from what I can tell, it looks like your shooting is locked to your frame rate. You need to calculate delta time and multiply movements by it. When motion is frame dependent, it will cause all sorts of weirdness. At around 60 fps your delta time should be 0.016 or so.


sven
Registered User
Quote
2022-04-14 20:20:18

This script doesnt tell much..

You can use PRINT function to give you info about whats happening inside your code.


dekon_17
Registered User
Quote
2022-04-14 20:25:25

Alright, I'll... Leave the full code then? So, here it is:
/*
<action jsname = "action_Shoot" description = "Shoot a bullet, but more advance!">

<property name = "ShootFrom" type = "scenenode"/>
<property name = "RelativeTo" type = "scenenode"/>
<property name = "Bullet" type = "scenenode"/>
<property name = "Speed" type = "float"/>
<property name = "LiveTime" type = "int"/>
<property name = "DMG" type = "float"/>

</action>
*/

action_Shoot = function()
{
}

action_Shoot.prototype.execute = function()
{
var Pos = ccbGetSceneNodeProperty (this.ShootFrom, "PositionAbs");
var Rot = ccbGetSceneNodeProperty (this.RelativeTo, "Rotation");
var PosRel = ccbGetSceneNodeProperty (this.RelativeTo, "PositionAbs");

this.Node = ccbCloneSceneNode (this.Bullet);
ccbSetSceneNodeProperty (this.Node, "Position", Pos);

this.Dir = PosRel.substract(Pos);
this.Dir.normalize();

this.OldPos = null;

var me = this;
this.Func = function() { me.Fire(); };
ccbRegisterOnFrameEvent (this.Func);

this.StartTime = new Date().getTime();

this.Folder = ccbGetSceneNodeFromName ("Enemies");
}

action_Shoot.prototype.Fire = function()
{
var Time = new Date().getTime();
if (this.LastTime == null)
this.LastTime = Time;
var Delta = Time - this.LastTime;

var Pos = ccbGetSceneNodeProperty (this.Node, "PositionAbs");
if (this.OldPos == null)
{
ccbSetSceneNodeProperty (this.Node, "Visible", true);
this.OldPos = Pos;
};

Pos.x += this.Dir.x * Delta * this.Speed;
Pos.y += this.Dir.y * Delta * this.Speed;
Pos.z += this.Dir.z * Delta * this.Speed;

var Check = Pos;

var Col = ccbGetCollisionPointOfWorldWithLine (this.OldPos.x, this.OldPos.y, this.OldPos.z, Pos.x, Pos.y, Pos.z);
if (Col || Time >= this.StartTime + this.LiveTime)
{
ccbRemoveSceneNode (this.Node);
if (Col)
var Check = Col;
ccbUnregisterOnFrameEvent (this.Func);
}
else
ccbSetSceneNodeProperty (this.Node, "Position", Pos);

var Count = ccbGetSceneNodeChildCount (this.Folder);

XD: for (var h = 0; h < Count; ++h)
{
var Guh = ccbGetChildSceneNode (this.Folder, h);
var Name = ccbGetSceneNodeProperty (Guh, "Name");

if (ccbDoesLineCollideWithBoundingBoxOfSceneNode (Guh, this.OldPos.x, this.OldPos.y, this.OldPos.z, Check.x, Check.y, Check.z))
{
var HBs = ccbGetSceneNodeChildCount (Guh);
for (var E = 0; E < HBs; ++E)
{
var num = E + 1;
var Eh = ccbGetSceneNodeFromName (Name + "HB" + num);

if (ccbDoesLineCollideWithBoundingBoxOfSceneNode (Eh, this.OldPos.x, this.OldPos.y, this.OldPos.z, Check.x, Check.y, Check.z))
{
ccbUnregisterOnFrameEvent (this.Func);
ccbRemoveSceneNode (this.Node);

var HP = ccbGetCopperCubeVariable ("" + Name + ".health");
ccbSetCopperCubeVariable ("" + Name + ".health", HP - this.DMG);

break;
break XD;
};
};
}
};
};

I must agree, this looks probably stupid. And I kinda understand it, not gonna lie...


sven
Registered User
Quote
2022-04-14 20:46:04

Check you Delta value.

Print it out with print command.. is it fixed.
Then check what will be your bulletspeed if multiplyed with Delta value.


Guest
Guest
Quote
2022-04-14 20:48:37

What parameters are you using for your shoot action in CC? Speed/LiveTime?


sven
Registered User
Quote
2022-04-14 22:21:23

Your code:
var Time = new Date().getTime();
if (this.LastTime == null)
this.LastTime = Time;
var Delta = Time - this.LastTime;
print(Delta);


My version:
var Time = new Date().getTime();
if (this.LastTime == null)
{
this.LastTime = Time;
}
var Delta = Time - this.LastTime;
this.LastTime = Time;

print(Delta);



dekon_17
Registered User
Quote
2022-04-15 12:54:37

Wait... Did I just forgot to make LastTime update in the end? I just... Okay, that's terrible from me. Thanks, sven.

Also, I thought about setting the LastTime to "new Date().getTime()" in the action start ("prototype.execute" part), so that there will be no need to make the "if" statement. Going to try it as well.

And, Guest, I use different parameters for each weapon. But for this one (SMG) the speed is 0.075 and live time is 5000 ms (the same as for pistol, but that doesn't really matter).

Little edit: after fixing the mistakes I did, the 0.075 is way too slow for a bullet.


dekon_17
Registered User
Quote
2022-04-15 13:03:11

So, I now checked how it works and... It does it's job well. But still: bullet appears pretty late as I think. But I think this might be because it starts from a scenenode that is attached to "Weapon" bone of a model. It might follow the node with a bit of a delay, so, that's what I think. Still, I'm not exactly sure.

Also, here's how I edited the code:
/*
<action jsname = "action_Shoot" description = "Shoot a bullet, but more advance!">

<property name = "ShootFrom" type = "scenenode"/>
<property name = "RelativeTo" type = "scenenode"/>
<property name = "Bullet" type = "scenenode"/>
<property name = "Speed" type = "float"/>
<property name = "LiveTime" type = "int"/>
<property name = "DMG" type = "float"/>

</action>
*/

action_Shoot = function()
{
}

action_Shoot.prototype.execute = function()
{
this.LastTime = new Date().getTime();

var Pos = ccbGetSceneNodeProperty (this.ShootFrom, "PositionAbs");
var Rot = ccbGetSceneNodeProperty (this.RelativeTo, "Rotation");
var PosRel = ccbGetSceneNodeProperty (this.RelativeTo, "PositionAbs");

this.Node = ccbCloneSceneNode (this.Bullet);
ccbSetSceneNodeProperty (this.Node, "Position", Pos);

this.Dir = PosRel.substract(Pos);
this.Dir.normalize();

this.OldPos = null;

var me = this;
this.Func = function() { me.Fire(); };
ccbRegisterOnFrameEvent (this.Func);

this.StartTime = new Date().getTime();

this.Folder = ccbGetSceneNodeFromName ("Enemies");
}

action_Shoot.prototype.Fire = function()
{
var Time = new Date().getTime();

var Delta = Time - this.LastTime;

var Pos = ccbGetSceneNodeProperty (this.Node, "PositionAbs");
if (this.OldPos == null)
{
ccbSetSceneNodeProperty (this.Node, "Visible", true);
this.OldPos = Pos;
};

Pos.x += this.Dir.x * Delta * this.Speed;
Pos.y += this.Dir.y * Delta * this.Speed;
Pos.z += this.Dir.z * Delta * this.Speed;

var Check = Pos;

var Col = ccbGetCollisionPointOfWorldWithLine (this.OldPos.x, this.OldPos.y, this.OldPos.z, Pos.x, Pos.y, Pos.z);
if (Col || Time >= this.StartTime + this.LiveTime)
{
ccbRemoveSceneNode (this.Node);
if (Col)
var Check = Col;
ccbUnregisterOnFrameEvent (this.Func);
}
else
ccbSetSceneNodeProperty (this.Node, "Position", Pos);

var Count = ccbGetSceneNodeChildCount (this.Folder);

XD: for (var h = 0; h < Count; ++h)
{
var Guh = ccbGetChildSceneNode (this.Folder, h);
var Name = ccbGetSceneNodeProperty (Guh, "Name");

if (ccbDoesLineCollideWithBoundingBoxOfSceneNode (Guh, this.OldPos.x, this.OldPos.y, this.OldPos.z, Check.x, Check.y, Check.z))
{
var HBs = ccbGetSceneNodeChildCount (Guh);
for (var E = 0; E < HBs; ++E)
{
var num = E + 1;
var Eh = ccbGetSceneNodeFromName (Name + "HB" + num);

if (ccbDoesLineCollideWithBoundingBoxOfSceneNode (Eh, this.OldPos.x, this.OldPos.y, this.OldPos.z, Check.x, Check.y, Check.z))
{
ccbUnregisterOnFrameEvent (this.Func);
ccbRemoveSceneNode (this.Node);

var HP = ccbGetCopperCubeVariable ("" + Name + ".health");
ccbSetCopperCubeVariable ("" + Name + ".health", HP - this.DMG);

break;
break XD;
};
};
};
};
this.LastTime = Time;
};



sven
Registered User
Quote
2022-04-15 14:04:43

Bullet moves fast thats why it seems like it appears late.
I tested your code and seems to be working as it should.

Bullet appears at location "ShootFrom" then moves at direction "RelativeTo" with no delay.


dekon_17
Registered User
Quote
2022-04-15 14:15:46

Huh... Well, if it works as it should... Then I'll try to make bullet speed even faster. You know, when I move forward, the bullet seems to shoot from... Like, center of a gun instead from its barrel? But still, thank you, sven, for telling me the reason of this problem (and, of course, for helping me out with that LastTime thing that I forgot).


sven
Registered User
Quote
2022-04-15 14:40:44

Yes if player moves then it makes it look like it spawns out from wrong spot,because you are following the bullet.
Somehow it can be fixed for sure-with some math.

if you shoot out slow moving bullet and you move fast then you will catch up it.

If your bullet speed is fast then you dont need long liveTime for it.. because it can travel huge distance in small time (5000ms is huge number to use) .


Guest
Guest
Quote
2022-04-16 05:07:25

Is it it even a good idea to use a bullet node for player shooting? I have heard people in the past say use ray-casting for player shooting and bullets for enemies. I have heard them say ray-casting (hit-scan) enemies are the worst as well. Just something to think about.


Arcanjo
Guest
Quote
2022-04-16 05:18:31

My friend, use the internal action of Coppercube named: "Shoot". It's so simple to use!


dekon_17
Registered User
Quote
2022-04-16 08:06:16

Good opinion.

One small issue.

IT DOESN'T WORK WITH HITBOXES.


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