AFRAME event listener for "raycaster-intersected" capturing

I am trying to capture the event raycaster-intersected every time it happens.

To handle collisions, I need to know the distance to the first entity detected, but the event only triggers the first time the raycaster hits an entity.

If the raycaster continues to touch the entity, the event does not trigger again.

Here is the code snippet I have:

AFRAME.registerComponent("follow-body", {
  'schema': {
    entityId: {
      type:'string',
      default:''
    }
  },
  init: function(){
    this.pibot = document.querySelector("#" + this.data.entityId);
  },
  tick: function(){
    let pibotPos = this.pibot.object3D.position;
    let pibotRotation = this.pibot.object3D.rotation;
    let el = this.el;
    pibotPos.y += 0.2;

    el.object3D.position.set(pibotPos.x, pibotPos.y, pibotPos.z);
    el.object3D.rotation.set(pibotRotation.x, pibotRotation.y, pibotRotation.z);
    el.addEventListener('raycaster-intersection', function(evt){
      var e = new CustomEvent('intersection-detected', {detail: evt.detail});
      this.dispatchEvent(e);
    });
  }
});

Answer №1

What do you think about implementing a custom component with the following functionalities:
- On receiving the raycaster-intersected event, save the raycaster
- When the raycaster-intersected-cleared event occurs, reset the raycaster reference
- During the tick event, if the raycaster reference is present, determine the intersection point and perform necessary calculations.

AFRAME.registerComponent("customComponent", {
  init: function() {
    this.el.addEventListener("raycaster-intersected", evt => {
      this.intersectingRaycaster = evt.detail.el.components.raycaster;
    });
    this.el.addEventListener("raycaster-intersected-cleared", () => {
      this.intersectingRaycaster = null;
    });
  },
  tick: function() {
     if (!this.intersectingRaycaster) {
        return;
     }

     const intersection = this.intersectingRaycaster.getIntersection(this.el);
     if (intersection) {
        let point = intersection.point;
        // Add your custom calculations here
     }
  }
});

Explore the implementation in this fiddle.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Having difficulty with Angular's ng-options feature in a Select element

I'm currently tackling an issue with using ng-options to iterate through an array of objects and display specific properties in a select element. Upon querying the api/admin endpoint, I receive JSON data containing details of all users holding the ad ...

Ways to block WebSocket access on a personal computer

Is it possible to protect my server resources from being accessed by other websites, such as example.com, via WebSocket? I want to prevent them from accessing the server using a URL like "ws://47.80.151.189:1234", and utilizing its resources (bandwidth, me ...

Direct all paths to the base path "/" using Express

Is there a way to redirect all URLs containing "/something" in Express to the base path "/:=", while still maintaining additional paths to their respective pages? For instance, I would like to implement the following redirects: "/something" redirects to ...

Choose a name to show when adding a new user in Firebase

Implementing authentication in my React app using Firebase has been successful for signing up and logging in. However, I have been facing challenges trying to include additional information during the sign-up process. I explored solutions provided on Stack ...

Preventing User Input in Autocomplete TextField using React Material UI

Currently, I am utilizing the Material UI component Autocomplete to display options. However, I would like for Autocomplete to function more like a select dropdown, where users do not need to type anything to receive suggestions. Is there a way to modify A ...

How can I wait for an onclick action to pause, loop, or continue inside of a loop?

The form's onsubmit function triggers a pop-up message asking the user if they want to proceed before submitting the form. This requires the onsubmit function to wait for the user's final input in order to fully execute the form. Here is the cod ...

Refresh DataTable while making a fetch request

Here is the HTML code snippet where I want to apply DataTable: <div id="reportrange" style="background: #fff; cursor: pointer; padding: 5px 10px; border: 1px solid #ccc;"> <span></span> </div> <table id=" ...

Leveraging data from various Fetch API calls to access a range of

As a beginner with Fetch API and Promises, I have encountered an issue that I hope someone can help me with. My code involves fetching event data with a token, extracting team ids, and using these ids to fetch more information from another endpoint. Every ...

Verify if Angular 2 route parameters have a legitimate value

Within an angular2 component, I have the following code: ngOnInit() { this.route.params .switchMap((params: Params) => this.elementsService.getElement(+params['id'])) .subscribe(element => { this.elementToEd ...

Encountering issues with integrating NPM package into Meteor.js

Having installed both the meteor-npm package and the crypto npm package, I encountered some issues while using it in Meteor. npm: updating npm dependencies -- crypto Despite seeing this output in the console, when trying to use the npm package on the ser ...

Is there a way for me to receive the response from the this.$store.dispatch method in vue.js 2?

Here is the structure of my component : <script> export default{ props:['search','category','shop'], ... methods: { getVueItems: function(page) { this.$store.disp ...

Tips for maintaining the backslash character while transforming a string into a regular expression

Received some regular expressions in a JSON object like this: { config: { regEx: "/^$/" } } Attempting to convert the string into a regex: const re = new RegExp(config.regEx); The issue is that RegExp escapes the / characters resulting in //^$ ...

Tips for adjusting UI with Media queries based on screen dimensions

Looking for a better solution I currently have 5 sets of items. Button Text Dropdown Pagination Icon For larger screen sizes, specifically @media (min-width: 990px) {} I would like all items to be in a single row, like so: [button][Text][Dropdown][Pagi ...

I often find myself yearning for the good old days of prototyping functions when initializing a new

I just created a module in nodejs called Test.js with the following code: function Test() { this.key = 'value'; } Test.prototype.foo = function () { return 'foo'; } module.exports = Test; Then, in B.js file: var Test = require(&a ...

Creating a Dynamic Tree View Component in AngularJS Using JSON Data

I am new to AngularJS and I need help creating a TreeView Structure from a JSON Object. Here is an example of my Return JSON Object: var categoryTree = [{Name:'Item1', Childnodes : {}, id: 1}, {Name:'Item2', Childnod ...

Sliding divider across two div containers with full width

Seeking a JavaScript/jQuery function for a full page slider functionality. The HTML structure I'm working with is as follows: My objectives are twofold: Both slide1 and slide2 should occupy the full width of the page. Additionally, I am looking for ...

Instructions for utilizing float:left and list-style-type within HTML are as follows: I am currently experiencing issues with implementing the float:left and list-style-type properties in my code

I'm attempting to align a button list to the left using the float: left property and also remove list styles, but for some reason it's not working as expected. //CSS #tus{margin:5px;padding:0;width:640px;height:auto;} #tus ul{margin:0px;padding: ...

Guide to implementing PCF (SOFT) shadows with three.js

Is it possible to apply the PCF (SOFT) shadow type, like the one found in the Three.js online editor, to your renderer using javascript code? https://i.sstatic.net/x0QmH.png ...

Learn how to incorporate tooltips into Vuetify datatable headers for enhanced user experience

Previously, in older versions of Vuetify, it was possible to access the headerCell slot and easily add tooltips. You can refer to https://codepen.io/nueko/pen/dZoXeZ for more details. In the latest version, named slots are used, requiring knowledge of the ...

I'm having trouble with my controller - not sure what the problem is

My controller seems to be malfunctioning. I have created a controller but it is not functioning properly. Even though I have reviewed it multiple times, the issue persists. Could someone please assist me with this problem? Angular Code var myPanelSearch ...