question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add utility to get/set ex.Vector size

See original GitHub issue

Context

It would be useful to set the magnitude of a vector, this allows easy scaling or capping at a maximum value. This is especially useful in physics simulations.

Proposal

I propose something like the following (there is probably a more efficient method of changing the size of the vector)

  /**
   * The magnitude (size) of the Vector
   * @obsolete magnitude will be removed in favor `.size` in version v0.24.0
   */
  public magnitude(): number {
    return this.distance();
  }
  
 /**
  * The size (aka magnitude) of the Vector
  */
  public get size(): number {
    return this.distance();
  }

  public set size(newLength: number) {
    this.normalize().scale(newLength);
  }

Usage:


const vel = new ex.Vector(3, 4);

console.log(vel.size); // 5
vel.size = 13;

// Automatically calculates to correct scale
console.log(vel.x); // 5
console.log(vel.y); // 12

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:15 (15 by maintainers)

github_iconTop GitHub Comments

1reaction
eonarheimcommented, Oct 9, 2019

@saahilk Let me know if this doesn’t cover everything https://github.com/excaliburjs/Excalibur#environment-setup, please let me know if there are any gaps in the readme 👍

0reactions
saahilkcommented, Oct 23, 2019

@eonarheim Could you check the modified pull request here.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to write C++ getters and setters - Stack Overflow
E.g.: Getting the size of a vector. You don't want to expose a data member, because it needs to be read only.
Read more >
C++ Encapsulation and Getters and Setters - W3Schools
Inside main() , we create an object of the Employee class. Now we can use the setSalary() method to set the value of...
Read more >
Python Set (With Examples) - Programiz
In this tutorial, you'll learn everything about Python sets; how they are created, adding or removing elements from them, and all operations performed...
Read more >
C++ Tutorial: A Beginner's Guide to std::vector, Part 1
A C++ programming tutorial that teaches developers how to use std::vector. Learn basic and intermediate C++ coding.
Read more >
How to use VBA Structures - Automate Excel
First we declare a variable array as an Employees variable. We then size the array and loop through a range of cells in...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found