Monday, October 16, 2023

3D objects position - scoring

Now that we have a way to locate our objects (puck and net) in the screen grid space, it's time to make a function to check if someone has scored. To evaluate this condition, we need to know if the puck is in a certain range of the space:

(Note: I used the player object as a way to easily test the code.)
 
Is the puck between the net's lateral posts?
 
```
signal goal
func _process(delta):
    var netPos = netA.get_global_transform().origin.x
    var pPosition = get_global_transform().origin.x
    # Puck between the posts
    if pPosition > (netA_width/2) and pPosition < (netPos + netA_width):
        emit_signal("goal", pPosition)
       
onready var netA = .get_parent().get_node("GoalNet")
onready var netA_x = netA.get_node("StaticBody_postR").get_global_transform().origin.x
onready var netA_x2 = netA.get_node("StaticBody_postL").get_global_transform().origin.x
onready var netA_width = netA_x - netA_x2
```

Is the puck inside the net and not past the net?
 
```
signal goal
func _process(delta):
    var netPos = netA.get_global_transform().origin.z
    var pPosition = get_global_transform().origin.z
    # Puck inside the net
    if pPosition > (netPos - netA_depth) and (pPosition < netPos):
        emit_signal("goal", pPosition)

onready var netA = .get_parent().get_node("GoalNet")
onready var netA_z = netA.get_node("StaticBody_postZ").get_global_transform().origin.z
onready var netA_z2 = netA.get_node("StaticBody_netZ2").get_global_transform().origin.z
onready var netA_depth = netA_z - netA_z2
```
 

Is the puck between the rink's floor and the top post?

```
```

Finally, we merge all those evaluations into one script:

```
signal goal
func _process(delta):
    var netPos = netA.get_global_transform().origin
    var pPosition = get_global_transform().origin
    # Puck inside the net
    if pPosition.z > (netPos.z - netA_depth) and (pPosition.z < netPos.z) \
    and pPosition.x > (netA_width/2) and pPosition.x < (netPos.x + netA_width):
        emit_signal("goal", pPosition)

onready var netA = .get_parent().get_node("GoalNet")

onready var netA_x = netA.get_node("StaticBody_postR").get_global_transform().origin.x
onready var netA_x2 = netA.get_node("StaticBody_postL").get_global_transform().origin.x
onready var netA_width = netA_x - netA_x2

onready var netA_z = netA.get_node("StaticBody_postZ").get_global_transform().origin.z
onready var netA_z2 = netA.get_node("StaticBody_netZ2").get_global_transform().origin.z
onready var netA_depth = netA_z - netA_z2

```





No comments:

Post a Comment