Ambiera Forum

Discussions, Help and Support.

Ambiera Forum > CopperCube > Programming and Scripting
Change position, relative to player

veganpete
Registered User
Quote
2023-11-03 12:51:10

[SOLVED] (Thanks to Just_in_Case)
I've been trying for a long time but can't find a solution. How would I move something left or right, relative to the player's 3D position/rotation please?

I my game, I've made a special top-down (bird's eye) camera which rotates with the mouse, the player moves to wherever the mouse is pointing. The player is always facing North as the map rotates.

I want the character to strafe/side-step left and right by pressing keys (like the "a" and "d" keys of the 1st Person Camera, for example) but it just doesn't seem possible - everything I've tried always moves the character relative to the map (global), rather than relative to the character themselves (local).

How would I do this please? I've uploaded the demo to explain it better (updated link - Bird'sEyeShooter.exe)...
https://veganpete.itch.io/srateg...

Character movement forwards works, just left and right strafe needs solving.

Thank you.


just_in_case
Moderator
Quote
2023-11-03 15:26:57

The URL says file not found.
If you are using javascript, then you can simply get the current position of your character using the command.

 var pos = ccbGetSceneNodeProperty(character,"Position");


then you can simply use the `pos` variable to get the x,y,z positiion of the player and can then add or subtact the strafe amount to it and then set the position to the character.

 

// your code for key check

var strafeAmount = 1;
ccbSetSceneNodeProperty(character,"Position",pos.x+strafeAmount,pos.y,pos.z);



you can do the same for strafing to the other side, instead of adding the strafeAmount just subtract it.

Hope it makes sense and helps you out.


VP
Guest
Quote
2023-11-03 16:26:48

Ah, I see what's happened, the host is giving me the link but then it's automatically deleting the file because it's a win.exe

Thanks for the help Just_In_Case, nice one - I'll give that a try. Really appreciate your support.


VP
Guest
Quote
2023-11-03 16:46:54

Tried the code you posted, unfortunately it's causing the same issue - it's not really strafing the character, it just moves him in the same direction, no matter which way he's facing.

I think I need to check rotation, not just position. I'll keep playing around to see if I an nail it.

Thanks again.


just_in_case
Moderator
Quote
2023-11-03 18:23:07

ooo, so you want to strafe left and right on the basis of player facing direction.

You probably need to do some trignometry here, to get the Forward and Right vector for the player and then based on these two vector update the player position.



//Just like the previous code we need to grab the current position and rotation of the player and assign them to variables.

var pos = ccbGetSceneNodeProperty(player,"Position");
var rot = ccbGetSceneNodeProperty(player,"Rotation");

// Now we calculate the forward and right vectors based on the player's rotation on Y-asis

var forward = new vector3d(Math.sin(rot.y), 0, -Math.cos(rot.y));
var right = new vector3d(-Math.cos(rot.y), 0, -Math.sin(rot.y));


var strafeSpeed = 0.5; // change this as per your requirement


// This is to strafe left
if ( key == 65) { // change it according to your code
pos.x += right.x *strafeSpeed;
pos.z += right.z * strafeSpeed;
}

// This is to strafe right
if (key == 68) { // change it according to your code
pos.x -= right.x * strafeSpeed;
pos.z -= right.z * strafeSpeed;
}

//Now we will set the pos to player position.

ccbSetSceneNodeProperty(player, 'Position', pos);



the above code should work for you. It doesn't matter in which direction the player is facing, it will always Strafe Left and Right on the basis of the player's rotation(facing direction).

Let me know if you need a working example file that uses this code.
You can also use the same code above to create forward and backward movement as well for the player facing direction.

Hope this helps :)



VP
Guest
Quote
2023-11-03 20:34:18

Thanks Just_In_Case It's looks promising but it still doesn't quite seem to work properly for me (almost).

The new code, for me, makes the player sort of walk in a clockwise U-shape when I press A, and an anti-clockwise U-shape when I press D. Then if I keep holding the key, he starts walking backwards.

I've probably done something wrong - I couldn't get it to work - so I modified it - I've probably screwed it up somewhere along the way?...

🔎︎


Really appreciate the notes you've put on the code you posted - very helpful. Thanks! I'll send a donation for you hopefully tomorrow.


VP
Guest
Quote
2023-11-03 20:36:49

Do you think var forward could be the problem?


just_in_case
Moderator
Quote
2023-11-04 04:47:19

Hi there, my apologies, I checked the code and found that we have to convert the rotation of the player to radians, CC uses degrees. While the code that I provided it expect the rotation rot.y to be in radians.

For the var forward we didn't used it, it won't cause any issue.if you want you can completely remove it. I included it in case you want to create forward and backward movement on the basis of player facing direction.

Here is the corrected/updated code:-



//Just like the previous code we need to grab the current position and rotation of the player and assign them to variables.

// change the man as per your player name
var player = ccbGetSceneNodeFromName("man");

var pos = ccbGetSceneNodeProperty(player,"Position");
var rot = ccbGetSceneNodeProperty(player,"Rotation");

// We convert "rot.y" rotation from degrees to radians before using it for calculations in our right vector.
rot.y = rot.y * Math.PI / 180;


// Now we calculate the right vector based on the player's rotation on Y-asis
//we modified the vector calculation slightly, instead of using negative cos we are using positive cos otherwise it will just swap the direction at some point.

var right = new vector3d(Math.cos(rot.y), 0, -Math.sin(rot.y));

var strafeSpeed = 0.5; // change this as per your requirement


pos.x -= right.x * strafeSpeed;
pos.z -= right.z * strafeSpeed;


//Now we will set the pos to player position.

ccbSetSceneNodeProperty(player, 'Position', pos);



Use the above code for Left and for right change the negative sign to positive for pos.x and pos.z


Attached is also a test sample file, you can move to left and right using the A and D, and can rotate the player with F


https://cdn.discordapp.com/attac...

Hope this is what you are trying to do.


VP
Guest
Quote
2023-11-04 11:14:38

Thank you for this Just_In_Case - you are a shining star!
This script will be very useful for me.

By the way, your "Buy me a coffee" button doesn't seem to be working on your github page.

I'll make a €20 donation through itch.io instead.

Thanks again for all your help.


VP
Guest
Quote
2023-11-04 12:31:07

Works perfectly! Thank you.

I've included a quick Bird's-Eye Shooter demo here...

https://veganpete.itch.io/srateg...

Use Mouse to aim (no shoot yet).
Mouse-distance from player determines walk/run/sprint speed.
W and S walk forwards/backwards.
A and D strafe left/right and circle around the aim-point.


just_in_case
Moderator
Quote
2023-11-05 03:36:50

Thanks a lot @VP for your support, I tried the Demo, but for me the movement is not working, I am unable to move with keyboard, the cube only moves with mouse aiming.

Maybe there is something missing not sure.

Also I would like to invite you to test out my custom engine that I have been creating for past few weeks. It is not a complete game engine, but it is slowly getting shape and look and feel of an engine. Let me know if you want to try it out, and I will send you a copy of it in it's current state.




just_in_case
Moderator
Quote
2023-11-05 03:41:55

Just tried the updated version, and it works pretty good, I can shoot and move with WASD, Also liked that you did updated the laser sight so that it won't disappear when cursor is aimed at a very far distance, very clever.

Glad that it is coming out pretty nicely.


VP
Guest
Quote
2023-11-05 18:08:17

Thank you Just_In_Case! Yes, it's coming along. I limited the camera view angle to 90degrees down and 45degrees up so the view should never leave the play area. I'm just finishing making a quick code now to adjust the camera height and z-position, with the mouse, so the player never goes off-screen when the camera-angle changes.

Yes please! I'd love to try your game-engine. If you can sent a link to my proton email, that'd be great. I no longer have access to my gmail/google account. Thank you!


just_in_case
Moderator
Quote
2023-11-06 06:26:19

Done, sent you a download key to the game engine uploaded it on itch.io as well.
Hope you like the prototype.


VP
Guest
Quote
2023-11-06 09:11:17

Thank you!


Create reply:


Posted by: (you are not logged in)


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