Physion

  • Increase font size
  • Default font size
  • Decrease font size
Welcome, Guest
Username Password: Remember me

Get current force from spring?
(1 viewing) (1) Guest
  • Page:
  • 1

TOPIC: Get current force from spring?

Get current force from spring? 3 years, 11 months ago #837

  • Gandalf
  • OFFLINE
  • Fresh Boarder
  • Posts: 2
I'd like to read out the current Force a spring is producing. Is there a direct way to do this? Getting the current length would be sufficient. I do not want to calculate this from the Anchor strings ....

Re: Get current force from spring? 3 years, 10 months ago #838

  • Gandalf
  • OFFLINE
  • Fresh Boarder
  • Posts: 2
Dear traveler of the webs from the far distant future: You sought an answer for your troubles, maybe you have come to the right place ....

I came up with a small script that changes the color of a spring based on strain. Put it into OnUpdate to work.

// Tangens hyperbolicus function. 
// Linear for small value, than converges slowly to 1.
 
function tanh (arg) {
return (Math.exp(arg) - Math.exp(-arg)) / (Math.exp(arg) + Math.exp(-arg));
}
 
//Get Koordinates of springs
x1= parseFloat(this.Anchor1.split(",")[0]);
y1= parseFloat(this.Anchor1.split(",")[1]) ;
x2= parseFloat(this.Anchor2.split(",")[0]) ;
y2= parseFloat(this.Anchor2.split(",")[1]) ;
 
// calculate springs length
d = Math.sqrt(Math.pow((x1-x2),2) + Math.pow((y1-y2),2));
 
// factor to exaggerate visual represention of strain
exaggeration_factor = 12;
 
// calculate strain
strain =(d - this.DesiredDistance) / this.DesiredDistance;
 
// calculate color
stress = tanh(exaggeration_factor * strain)
 
// change Springs color based on strain.
// More red for stretch
// More yellow for compression
if (stress>0) this.Color = new QColor(stress *255,0,0);
if (stress<0) this.Color = new QColor(-stress * 255,-stress * 255,0);
Last Edit: 3 years, 10 months ago by Gandalf.

Re: Get current force from spring? 3 years, 9 months ago #843

  • Lynxx
  • OFFLINE
  • Moderator
  • Java/C#-Developer
  • Posts: 223
Nicely done !
Infinity is vast, especially near end.
bye, Lynxx
  • Page:
  • 1
Moderators: Lynxx, Glenn555
Time to create page: 0.10 seconds