Discuss Scratch
- Discussion Forums
- » Help with Scripts
- » 3d test problems
- WiggleDJim
-
Scratcher
13 posts
3d test problems
I made this 3d test where there's a player in a 3d space. https://scratch.mit.edu/projects/1135861368/
my problem is that whenever you rotate the 3d space (or the camera), the player controls moves the player along the same axis no matter which way it is facing. In most games when you press forward, you move in the direction of the camera. This is how I need it to be while the x/z values still are accurate no matter how the 3d space is rotated.
my problem is that whenever you rotate the 3d space (or the camera), the player controls moves the player along the same axis no matter which way it is facing. In most games when you press forward, you move in the direction of the camera. This is how I need it to be while the x/z values still are accurate no matter how the 3d space is rotated.
- AntoinoSills
-
Scratcher
21 posts
3d test problems
you can use sin and cos per exemple “add (sin(camera rot) to cam x”, “add (cos(camera rot) to cam z”
- RokCoder
-
Scratcher
1000+ posts
3d test problems
This looks more like a top-down test that only rotates around the Y-axis. I don't see any calculations for rotations around other axes, perspective calculations, etc.
The player's movement is exactly as expected. You're altering the X and Y coordinates, and they are rotated, so you would expect the movement to occur aligned with the direction of the rotated axes.
With this effectively 2D system, modify the X and Y coordinates in the camera's direction to achieve what you want.
This would be -
That's quite lengthy, so you can simplify it like this -
And from that, you can get to the final equation of -
The player's movement is exactly as expected. You're altering the X and Y coordinates, and they are rotated, so you would expect the movement to occur aligned with the direction of the rotated axes.
With this effectively 2D system, modify the X and Y coordinates in the camera's direction to achieve what you want.
This would be -
if <key [a v] pressed?> then
change [z v] by ((-2) * ([sin v] of (angle switcher)))
change [x v] by ((2) * ([cos v] of (angle switcher)))
end
if <key [d v] pressed?> then
change [z v] by ((2) * ([sin v] of (angle switcher)))
change [x v] by ((-2) * ([cos v] of (angle switcher)))
end
if <key [w v] pressed?> then
change [z v] by ((2) * ([cos v] of (angle switcher)))
change [x v] by ((2) * ([sin v] of (angle switcher)))
end
if <key [s v] pressed?> then
change [z v] by ((-2) * ([cos v] of (angle switcher)))
change [x v] by ((-2) * ([sin v] of (angle switcher)))
end
That's quite lengthy, so you can simplify it like this -
set [sideways movement v] to ((<key [d v] pressed?> - <key [a v] pressed?>) * (2)) // -2 when a pressed, 2 when d pressed
change [z v] by ((sideways movement) * ([sin v] of (angle switcher)))
change [x v] by (((0) - (sideways movement)) * ([cos v] of (angle switcher)))
set [forwards movement v] to ((<key [w v] pressed?> - <key [s v] pressed?>) * (2)) // -2 when s pressed, 2 when w pressed
change [z v] by ((forwards movement) * ([cos v] of (angle switcher)))
change [x v] by ((forwards movement) * ([sin v] of (angle switcher)))
And from that, you can get to the final equation of -
set [sideways movement v] to ((<key [d v] pressed?> - <key [a v] pressed?>) * (2)) // -2 when a pressed, 2 when d pressed
set [forwards movement v] to ((<key [w v] pressed?> - <key [s v] pressed?>) * (2)) // -2 when s pressed, 2 when w pressed
change [z v] by (((sideways movement) * ([sin v] of (angle switcher))) + ((forwards movement) * ([cos v] of (angle switcher))))
change [x v] by (((forwards movement) * ([sin v] of (angle switcher))) - ((sideways movement) * ([cos v] of (angle switcher)))
- Discussion Forums
- » Help with Scripts
-
» 3d test problems