Utilize ThreeJS to incorporate a positional offset into a Matrix4 as part of a series of

I need to handle a ThreeJS Matrix4 that holds the position of an element, along with another Matrix4 containing an offset. I want to add this offset to the position in my first Matrix4. Currently, I'm doing it like this:

baseMatrix4.setPosition(new THREE.Vector3().setFromMatrixPosition(baseMatrix4).add(new THREE.Vector3().setFromMatrixPosition(offsetMatrix4)))

The issue is that accessing baseMatrix4 inside the method hinders chaining with previous methods, as baseMatrix4 may be undefined or outdated by then.

Any suggestions on how to improve this and still allow for chaining?

Answer №1

I find it intriguing, why must they be connected by chains? Wouldn't it be more efficient to allow for a few extra lines of code? Is the desire to have everything condensed into one statement that crucial?

let initialPosition = new THREE.Vector3().setFromMatrixPosition(initialMatrix4);
let offsetPosition = new THREE.Vector3().setFromMatrixPosition(offsetMatrix4);
initialMatrix4.setPosition(initialPosition.add(offsetPosition));

Update:

If you're hesitant about creating additional Vector3s, consider directly manipulating the matrix by emulating the functionality of Vector3.setFromMatrixPosition()

i = initialMatrix4.elements;
o = offsetMatrix4.elements;
i[12] += o[12];
i[13] += o[13];
i[14] += o[14];

... or opt for the += operator if that aligns better with your preferences.

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

Issue with using async await in map function: async function may not complete before moving on to the next item in the

Currently, I have an array that needs to be mapped. Inside the mapping function, there is an asynchronous function being called, which conducts an asynchronous request and returns a promise using request-promise. My intention was for the first item in the ...

Uncontrolled discord bot flooding with messages despite being set to send messages only once every 60 seconds

const Discord = require('discord.js'); const { Client, MessageAttachment } = require('discord.js'); const client = new Discord.Client(); client.once('ready', () => { console.log("Ready!") }) client.on(&apos ...

"Implement image uploading and retrieval functionality in your application by utilizing Parse and Back4App with the help of Node

It seems like I have a question that may have already been answered, but I can't find the right solution for my issue. I'm facing trouble with my code and can't figure out what's wrong. The problem arises when I try to upload specific ...

Steps for triggering a click event on a div with a button role within a class containing multiple elements

Can anyone help me figure out how to auto-click every button in Instagram's "hide story from" settings using console? I tried the following code: for (let i = 0; i < 300; i++) { document.getElementsByClassName('wbloks_1')[i] ...

Activate div2 visibility upon hovering over div1 using jQuery

I have a shopping basket on my website, and I want to display a list of products when a user hovers over the basket icon. However, I am facing an issue where the list disappears as soon as the mouse leaves the basket logo. Is there a way to keep the list ...

unable to retrieve access-token and uid from the response headers

I am attempting to extract access-token and uid from the response headers of a post request, as shown in the screenshot at this https://i.sstatic.net/8w8pV.png Here is how I am approaching this task from the service side: signup(postObj: any){ let url = e ...

Tips for managing open and closed components within a React accordion and ensuring only the clicked component is opened

Unique Accordion component: const CustomAccordion = (props: AccordionProps) => { const { label, levels, activeId, id } = props const [isExpand, setIsExpand] = useState(false) const onPress = useEvent(() => { setIsExpand( ...

Determine the exact width of text without rounding in Javascript

I'm facing an issue with a div element that I'm manipulating using Javascript to change its position. The problem is that it's rounding off incorrectly. #hi{ background-color: blue; width: 2px; height: 10px; position: absolu ...

Using a function as an argument within an Angular directive

Looking for a solution to pass a promise-returning function into a directive? Here's what I'm currently doing: In the parent controller, I've created a callback: $scope.myCb = function(data) { console.log(data); } Directive Scope: sco ...

Eliminate the ending slash from your Nuxt application

Hello, I am currently working on a Nuxt Application and encountering an issue. After running the npm run build command and starting it with npm start, there seems to be an inconsistency with trailing slashes. During the build, the URL appears without a t ...

What is the best way to incorporate checkbox properties to monitor updates on individual rows in a table without relying on the Grid object or jQuery.Data(Input)?

Is there a way to perform form requests selectively on changed lines without relying on Grid or jQuery.Data(input) logic? Because the program is outdated, AJAX isn't suitable for this scenario as I don't want each line triggering a change event ...

Encountered a 400 error when attempting to send a request to a RestController in Spring MVC

I keep getting a "bad request 400" error when sending a request with parameters to the controller. I have double-checked the syntax but can't find any mistakes. Can someone please review my code and see what's wrong? var url = contextPath+"/bill ...

The AngularJS templates' use of the ternary operator

Is there a way to implement a ternary operation in AngularJS templates? I am looking for a way to apply conditionals directly in HTML attributes such as classes and styles, without having to create a separate function in the controller. Any suggestions wo ...

Is there a way to stabilize a section or content that bounces as I scroll?

Whenever I scroll up or down, there is a section on my page where I have implemented scroll magic. However, as I scroll through this section, it starts to jump until it reaches the position where I want it to be with transform:translateY(0). I am unsure h ...

Node.js and Express: Delegate routes to different endpoints for handling by the Single Page Application, instead of just the root route

I have my node.js/Express server configured to host my Vue.js single page application from the root URL path (localhost:3333) within the public folder. When navigating through my app, everything works smoothly thanks to the vue.js History API handling all ...

Use setTimeout and setInterval with the initial input without parentheses or double quotation marks

<!DOCTYPE HTML> <html> <head> <script type="text/javascript"> var count=0; function increaseCount(){ document.getElementById('txt').value=count; count++; setTimeout(increaseCount(),1000); } </script> </head&g ...

Preventing Ng-repeat from refreshing after deleting using $http request

Once I remove an item from my list, the deleted object's data disappears, but the placeholder (empty row) lingers. (I tried using apply() with no luck) I have a standard CRUD interface displaying expenses. <table class="table table-striped"> ...

What could be causing Wordpress Jquery to only function properly after a page refresh?

Here is the code snippet I am working with: <script type="text/javascript"> jQuery( document ).ready(function() { jQuery('#bookbtn_loc_1').on('click', function(event){ jQuery("a.da-close").click(); ...

Tips for confirming receipt of a message through socket.io on the client side

What is the process for ensuring that a message sent using the socket.io library has been successfully received by the client? Does socket.io provide a specific method for confirming receipt? Appreciate any insights you can provide! ...

Issue arised while trying to open Bootstrap modal window due to conflict with surrounding elements

I'm facing a challenge with getting the bootstrap modal window to pop up on my website. Despite trying various solutions and troubleshooting steps, I haven't been able to resolve the issue. Even after eliminating all scripts except for the boots ...