I am looking to improve my throwing mechanic in VR. I want to be able to have more control over the feel and smoothness of the throw.
Currently using the windows mixed reality platform with open VR.
I have a simple scene.
I got the hand to throw the red triangular cube and it works. sometimes when testing I can't figure out why i get weird directional changes when throwing in some scenarios? how does the impulse/velocity choose a direction on button press?
I used the vr starter tutorial on the docs and
Thank you Bastiaan Olij for making such an awesome tutorial!
I tried to find a way to average the velocity but I could never get it to work better then just this basic velocity function.
'''func _process(delta):
controller_velocity = (global_transform.origin - last_position) /delta
last_position = global_transform.origin'''
Here is the code for the VR controller/3darea.
extends Area
var impulse_factor = .4
var object_in_area = Array()
var picked_up_object = null
var last_position = Vector3(0.0,0.0,0.0)
export var velocity = Vector3(0.0,0.0,0.0)
var controller_velocity = Vector3(0, 0, 0)
var prior_controller_position = Vector3(0, 0, 0)
var prior_controller_velocities = []
func _on_Area_body_entered(body):
if body.has_method('pickup') and object_in_area.find(body) == -1:
object_in_area.push_back(body)
print(object_in_area)
print(body)
func _on_Area_body_exited(body):
if object_in_area.find(body) != -1:
object_in_area.erase(body)
print(object_in_area)
func _on_button_pressed(button):
print(button)
if button == 2:
if picked_up_object:
picked_up_object.let_go(controller_velocity * impulse_factor)
picked_up_object = null
elif !object_in_area.empty():
picked_up_object = object_in_area[0]
picked_up_object.pickup(self)
func _ready():
get_parent().connect("button_pressed",self,"_on_button_pressed")
func _process(delta):
#this section was me trying to find a way to average velocities?
# controller_velocity = Vector3(0, 0, 0)
#
# if prior_controller_velocities.size() > 0:
# for vel in prior_controller_velocities:
# controller_velocity += vel
#
# controller_velocity += (global_transform.origin - prior_controller_position) / delta
#
# prior_controller_velocities.append((global_transform.origin - prior_controller_position) / delta)
# prior_controller_position = global_transform.origin
#
# if prior_controller_velocities.size() > 15:
# prior_controller_velocities.remove(0)
##
# controller_velocity = controller_velocity / prior_controller_velocities.size()
##
#
controller_velocity = (global_transform.origin - last_position) /delta
last_position = global_transform.origin
Here is what i got for the triangular cube.
extends RigidBody
onready var original_parent = get_parent()
var original_collision_layer
var original_collision_mask
var picked_up_by = null
onready var triangle_block_start_postion = self.global_transform.origin
func pickup(by):
if picked_up_by == by:
return # quit function if body is already picked up?
if picked_up_by:
let_go()
picked_up_by = by
mode = RigidBody.MODE_STATIC
collision_layer = 0
collision_mask = 0
original_parent.remove_child(self)
picked_up_by.add_child(self)
transform = Transform()
func let_go(impulse = Vector3(0.0,0.0,0.0)):
if picked_up_by:
var t = global_transform
picked_up_by.remove_child(self)
original_parent.add_child(self)
global_transform = t
mode = RigidBody.MODE_RIGID
collision_layer = original_collision_layer
collision_mask = original_collision_mask
apply_impulse(Vector3(0.0,0.0,0.0), impulse)
yield(get_tree().create_timer(5.0),"timeout")
global_transform.origin = triangle_block_start_postion
picked_up_by = null
func _ready():
original_collision_layer = collision_layer
original_collision_mask = collision_layer
print(original_parent)
print(picked_up_by)
hopefully this helps explain everything .. I will try to find out how to save my scene if more info is needed.