AntonSynytsia / MSPhysics

A NewtonDynamics wrapper for SketchUp.
http://sketchucation.com/forums/viewtopic.php?f=323&t=56852#p516427
MIT License
55 stars 20 forks source link

Using socket connections #8

Open jameswagoner opened 7 years ago

jameswagoner commented 7 years ago

Is there support for controlling joints sliders from messages received in a socket connection?

AntonSynytsia commented 7 years ago

That's something I want to, and intend to add, in the upcoming releases. I has been a long dream of mine for multiplayer games in MSPhysics.

jameswagoner commented 7 years ago

Actually I was able to just define a variable within UI.start_timer and then use that for the control in a joint.

I'll post a gist

James Wagoner

On Mar 14, 2017 8:33 AM, "Anton" notifications@github.com wrote:

That's something I want to, and intend to add, in the upcoming releases. I has been a long dream of mine for multiplayer games in MSPhysics.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/AntonSynytsia/MSPhysics/issues/8#issuecomment-286459308, or mute the thread https://github.com/notifications/unsubscribe-auth/AC6VNsuXwIOqbRnTm50tEtka3kVLCC_zks5rlrNDgaJpZM4McUb3 .

AntonSynytsia commented 7 years ago

Okay! I'm looking forward to checking it out.

jameswagoner commented 7 years ago

https://gist.github.com/jameswagoner/b83a4bb50db03fdca9cf80d2e6dc30d3

In the Joint settings I set the controller to one of the $axis{X} variables. Some maths are done but that is specific to my model.

New to Ruby scripting so the connection script could be better I am sure.

AntonSynytsia commented 7 years ago

That's a fine looking code. The script I will implement into MSPhysics will also use sockets, but will be slightly different to suit the general purpose.

For your purpose, however, I assume you're loading that file separately from your model. The code could be reorganized and attached as a script to one of the groups so that you won't have to load it as a separate file. If you select a desired group/component instance, open MSPhysics UI, and activate the Script tab, you can paste the following code there, which does exactly the same thing as in your previous code:

# Blockless code is triggreed only once, slighy before the onStart
require 'socket.so'

def translate(f)
  return (f / 255) * 180
end

# Triggered when simulation starts
onStart {
  socket = UDPSocket.new
  socket.bind("127.0.0.1", 3157)

  @connreset = false
}

# Triggered when simulation updates
onTick {
  if !@connreset
    begin
      message, sender = socket.recvfrom_nonblock(100)
      new_message = true
    rescue Errno::EWOULDBLOCK
      new_message = false
    rescue Errno::ECONNRESET
      new_message = false
      # You may reset simulation in case there is such error
      #simulation.reset
      # Will just use a variable to prevent this from being called.
      @connreset = true
    end
    if new_message
      message1, message2 = message.split(";")
      $axis1 = translate message1.to_f
      $axis2 = translate message2.to_f
      # Instead of global varaibles, which are considered a poor coding habit,
      # you may use get_var/set_var
      #set_var('axis1', translate(message1.to_f))
      #set_var('axis2', translate(message2.to_f))
      # In the Joint controller section you could obtain the values by
      # get_var('axis1') or get_var('axis2')
    end
  end
}

onEnd {
  # Code for closing the socket...
}

I did not test it, but any errors should cause the simulation to terminate, displaying the error message in the messagebox and Ruby Console (Menu->Window->Ruby Console), which I assume you will be able to fix.

Also, the group your pasting the code to should be part of simulation; it may be set to static, but it cannot be set to ignored.

jameswagoner commented 7 years ago

That would be ideal because it loads as a plugin and don't need it all the time.

Would I do a script for each group? Feel like that would run into conflicts. Specially since only one socket can attach itself to a port.

James Wagoner

On Mar 15, 2017 2:46 PM, "Anton" notifications@github.com wrote:

That's a fine looking code. The script I will implement into MSPhysics will also use sockets, but will be slightly different to suit the general purpose.

For your purpose, however, I assume you're loading that file separately from your model. The code could be reorganized and attached as a script to one of the groups so that you won't have to load it as a separate file. If you select a desired group/component instance, open MSPhysics UI, and activate the Script tab, you can paste the following code there, which does exactly the same thing as in your previous code:

Blockless code is triggreed only once, slighy before the onStartrequire 'socket.so'

def translate(f) return (f / 255) * 180end

Triggered when simulation starts

onStart { socket = UDPSocket.new socket.bind("127.0.0.1", 3157)

@connreset = false }

Triggered when simulation updates

onTick { if !@connreset begin message, sender = socket.recvfrom_nonblock(100) new_message = true rescue Errno::EWOULDBLOCK new_message = false rescue Errno::ECONNRESET new_message = false

You may reset simulation in case there is such error

  #simulation.reset
  # Will just use a variable to prevent this from being called.
  @connreset = true
end
if new_message
  message1, message2 = message.split(";")
  $axis1 = translate message1.to_f
  $axis2 = translate message2.to_f
  # Instead of global varaibles, which are considered a poor coding habit,
  # you may use get_var/set_var
  #set_var('axis1', translate(message1.to_f))
  #set_var('axis2', translate(message2.to_f))
  # In the Joint controller section you could obtain the values by
  # get_var('axis1') or get_var('axis2')
end

end }

I did not test it, but any errors should cause the simulation to terminate, displaying the error message in the messagebox and Ruby Console (Menu->Window->Ruby Console)

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/AntonSynytsia/MSPhysics/issues/8#issuecomment-286889664, or mute the thread https://github.com/notifications/unsubscribe-auth/AC6VNnGddQpfyltT2MBQN_wz0N3xSwlxks5rmFwWgaJpZM4McUb3 .

AntonSynytsia commented 7 years ago

The script must be attached to only one of the groups within the model, say to a static and non-collidable box. I don't see a reason for it to be attached to multiple groups.

jameswagoner commented 7 years ago

Okay so I drew a new object and grouped it, added the script via the UI, and ran the simulation. Didn't get any response or logs in the ruby console that I added.

I marked the group ignored so that it wouldn't drop out of the sky. Is that the issue, marking it ignore makes the script not execute?

AntonSynytsia commented 7 years ago

Exactly. That's why you instead mark it to static and noncollidable. On Mar 16, 2017 9:32 AM, "James Wagoner" notifications@github.com wrote:

Okay so I drew a new object and grouped it, added the script via the UI, and ran the simulation. Didn't get any response or logs in the ruby console that I added.

I marked the group ignored so that it wouldn't drop out of the sky. Is that the issue, marking it ignore makes the script not execute?

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/AntonSynytsia/MSPhysics/issues/8#issuecomment-287114428, or mute the thread https://github.com/notifications/unsubscribe-auth/ACtZDzr_lEvK3zZMzqgOV9bg3_KjMEwsks5rmWQOgaJpZM4McUb3 .

sriranjanr commented 1 year ago

Okay so I drew a new object and grouped it, added the script via the UI, and ran the simulation. Didn't get any response or logs in the ruby console that I added.

I marked the group ignored so that it wouldn't drop out of the sky. Is that the issue, marking it ignore makes the script not execute?

Hello could you please post your code as how you were able to command the joint angle? I am unable to get the set_slider_value to work. Any input would be appreciated