I have created a Vector3() called ori, and I have populated its coordinates x, y and z. How, now, do I translate this vector, say along axis z, of the indicated value?
I tried this:
ori.translateZ( - 100);
This gets me an error (TypeError: Cannot read property 'translateZ' of undefined)
Matey gave the answer you need, but didn't tell you why your method didn't work. ori is a Vector3 and not a Object3D. translateZ() is a method of the Object3D class, but not a method of the Vector3 class. If the position member of an Object3D class had been set to equal ori (position is a Vector3) then translateZ on that Object3D instance would have worked.
Your understanding is correct. The answer juagicre gave would only change the z value to-100
If you want to translate by a single axis, it's as simple as adding the translation value:
ori.z += -100;
If you want to translate by a vector, it's again very simple:
var trans = new THREE.Vector3(-100,-200,-300);
ori.add(trans);
ori.setZ(-100);
Vector3 documentation
Related
I have a very basic third person controller:
func _physics_process(delta):
vel.x = 0
if Input.is_action_pressed("move_forward"):
vel.x += SPEED * delta
if Input.is_action_pressed("turn_left"):
rotate_y(deg2rad(ROTATION_INTENSITY))
move_and_collide(vel)
However on rotating and then moving forward, the player will move forward on the x axis instead of the direction the player is pointed. How do i fix this?
Thanks in advance!
Your KinematicBody is an Spatial, and as such it is positioned by a Transform.
You can get the Transform from the parent Spatial (or the root of the scene tree if there is no parent Spatial) from the transform property.
And you can get the Transform from root of the scene directly from the global_transform property.
The Transform has two parts:
A Vector3 called origin.
A Basis called basis.
Together they encode a coordinate system. In particular basis has three vectors that hold the direction and scale of each of the axis of the coordinate system. And origin has the position of the origin of the coordinate system.
Now, Godot's coordinate system in 3D is right handed with UP = Y and FORWARD = -Z. You can confirm this with the constants Vector3.UP and Vector3.FORWARD. Also notice this differs from Unity and Unreal which are left handed.
Ergo, if you want the direction of one of the axis, you can get it form the basis of the global transform. Like this:
var forward := -global_transform.basis.z
var backward := global_transform.basis.z
var left := -global_transform.basis.x
var right := global_transform.basis.x
var down := -global_transform.basis.y
var up := global_transform.basis.y
You could use those to build your velocity in global coordinates, for example:
velocity += global_transform.basis.x * SPEED
Alternatively, you can convert vectors from local to global space with to_global (and from global to local with to_local) so you can do this:
var forward := to_global(Vector3.FORWARD)
var backward:= to_global(Vector3.BACKWARD)
var left := to_global(Vector3.LEFT)
var right := to_global(Vector3.RIGHT)
var down := to_global(Vector3.DOWN)
var up := to_global(Vector3.UP)
This also means you may build your velocity in local coordinates, and convert it to global coordinates at the end:
move_and_slide(to_global(velocity))
And just to be clear, move_and_slide expects global coordinates.
I will also point out that it is possible to rotate a vector with rotated and project a vector onto another with project.
I have a reference to a Vec and I want to extract a reference to exactly one element from it, and panic if there are any more elements or zero. The equivalent if I had an array would be:
let [x] = list;
but Vecs are dynamically sized, so that won't work here. I can think of one way to do it with a reference to a Vec, and a couple more that require ownership, but I'm wondering if there's a shorter and simpler way.
Unowned Option 1: Use assert! and indexing
assert_eq!(list.len(), 1);
let x = &list[0];
Owned Option 1: Use try_into()
let [x]: [i32; 1] = list.try_into().unwrap();
Owned Option 2: Use assert! and pop
assert_eq!(list.len(), 1);
let x = list.pop();
So, is there a shorter and clearer way?
You can use a slice pattern (playground):
let v = vec![1u32];
let x = match v.as_slice() {
&[x] => x,
_ => panic!("expected single element"),
};
// ... use x ...
It's not necessarily shorter, but it's clear, doesn't require ownership or mutation, and can be nicely generalized to more than one element to extract.
If instead of panicking you want to check the element count and extract x if it's the only element, you can use if let:
if let &[x] = v.as_slice() {
// ... use x ...
}
I would like to compute the phase of a complex number using boost::compute
here is my attempt, I expect the result to be equal to atan2(0.5f):
namespace bc = boost::compute;
bc::vector<std::complex<float>> vec{ {1.0f, 2.0f} };
bc::vector<float> result(1);
bc::transform(vec.begin(), vec.end(), result.begin(), bc::atan2<float>());
but I get a compilation error claiming "Non-unary function invoked one argument"
boost::compute's atan2 would appear to be a binary function just like std::atan2.
I'm assuming you're trying to obtain the phase angle of your complex number? The standard C++ function for this would be std::arg() - I don't see this one being defined in boost::compute, though I might have missed it.
If arg() is indeed missing, you're quite right it's implemented via atan2 - you'll need to extract the imaginary (boost::compute::imag()) and real (boost::compute::real()) components first though, and pass them as individual arguments to atan2.
I think you can also use Boost.Compute's lambda expressions for this:
bc::vector<float2> input{ {1.0f, 2.0f}, {3.0f, 4.0f}, {5.0f, 6.0f} };
bc::vector<float> output(3);
using boost::compute::lambda::atan2;
using boost::compute::_1;
using boost::compute::lambda::get;
bc::transform(
float2_input.begin(),
float2_input.end(),
float_output.begin(),
atan2(get<1>(_1), get<0>(_1)),
queue
);
float2 is bassically a complex in Boost.Compute. You can also check test_lambda.cpp.
I found a way to make it work.
stage 1: allocate 2 vectors:
bc::vector<std::complex<float>> vec{ {1.0f, 2.0f}, {3.0f, 4.0f}, {5.0f, 6.0f} };
bc::vector<float> result(3);
stage 2: interpret the complex vector as a float buffer iterator
buffer_iterator is quite useful when you have a strongly typed vector and would like to pass it to an algorithm as a different type.
auto beginf = bc::make_buffer_iterator<float>(vec.get_buffer(), 0);
auto endf = bc::make_buffer_iterator<float>(vec.get_buffer(), 6); // note end point to final index + 1
stage 3: define strided iterators so that we can use the same buffer as the argument for tan2. each iterator iterates the buffers in strides of 2 indices, and they supply tan2 with interleaved access to the buffer:
auto begin_a = bc::make_strided_iterator(beginf + 1, 2); // access imaginary part
auto end_a = bc::make_strided_iterator_end(beginf + 1, endf , 2);
auto begin_b = bc::make_strided_iterator(beginf, 2); // access real part
finally, call transform:
bc::transform(begin_a, end_a, begin_b, result.begin(), bc::atan2<float>()); // atan(b/a)
bc::system::default_queue().finish();
I want to modify an element's value in a double-linked list, but I don't know how to get its pointer, because element's value is a nil interface defined by go-lang itself.
As far as I know is, I must do a type assertion before get element's value like:
val, ok := ele.Value.(TYPE)
if ok {
// do something...
}
but if I just modify val it will be useless.
So any hint?
Thanks.
There are two pretty straight forward options. They're all going to involve type asserting because you're using interface{}
You can store it as a pointer and type assert:
var q interface{}
var i int
q = &i
*(q.(*int)) = 5
You can simply reassign it:
var q interface{}
q = 5
b := q.(int)
q = 2*b
I personally think reassigning it makes the most sense. If you're doing it in a function you probably need to return the new value. I'm sure there are other ways to change it around, but I think simple is best.
Of course in the real work some checking would be nice.
private function getPercentage(max:Number, value:Number):int
{
return Number((value*100) / max);
}
I call the above function to assign a percentage to an object.
var max:Number = findMax();
p.percentage = getPercentage(max, p.value);
Assume that p is some object with percentage defined as
public var percentage:Number;
When I put a breakpoint and check for the value returned in getPercentage it will something like 1.22343342322 but when I assign it to p.percentage it automatically becomes 1, losing the precision.
How do I handle this kind of a situation?
It says in the LiveDocs
To store a floating-point number,
include a decimal point in the number.
If you omit a decimal point, the
number will be stored as an integer.
But how do I do that? Any insight to this problem is highly appreciated.
Your method getPercentage() returns int. Change it to Number.