Turning on physics for objects in a list on activate


#1

Hi there, I was wondering if there is a way to ‘turn on’ physics for objects in blockly?

I have balls being fired out of a cannon in a space blocks that appear semi-transparent on hover and turn solid when activated. I’d like the ball to only bounce off blocks after they have been activated - and pass through all the semi-transparent ones - https://cospac.es/edu/KNAJ

I’ve found a post from @Luc_Roghi on some javascript that can be used - var.addToPhysics() and have used that on the created balls, but am not sure how to apply it to my list of deflector blocks.


#2

Looks like you’re doing it right (not that I would know). I can’t see these physics API references anywhere in the API reference docs, however, looking at the source using Developer Tools, we see the functions below. The code is obfuscated to cut down filesize, and the resulting function for addToPhysics just has lots of math, so not terribly useful.

p.shiftPhysicsPosition = function(a, c, d) {
    o.zB(a, c, d)
}
;
p.setPhysicsPosition = function(a, c, d) {
    o.rB(a, c, d)
}
;
p.shiftPhysicsPositionLocal = function(a, c, d) {
    o.AB(a, c, d)
}
;
p.setPhysicsGroupPosition = function(a, c, d) {
    o.qB(a, c, d)
}
;
p.shiftPhysicsGroupPositionLocal = function(a, c, d) {
    o.yB(a, c, d)
}
;
p.getPhysicsPosition = function() {
    return o.zA()
}
;
p.getPhysicsGroupPosition = function() {
    return o.yA()
}
;
p.rotatePhysicsBodyLocal = function(a, c, d, e) {
    o.VA(a, c, d, e)
}
;
p.setPhysicsPositionCorrection = function(a, c, d) {
    o.sB(a, c, d)
}
;
p.removePhysicsBody = function() {
    o.RA()
}
;
p.removePhysicsGroup = function() {
    o.SA()
}
;
p.removeFromPhysicsGroup = function() {
    o.QA()
}
;
p.addToPhysics = function() {
    o.fA()
}
;
p.setModelPreciseCollision = function(a) {
    o.nB(a)
}
;
p.addToPhysicsCollisionFilter = function(a) {
    o.gA(a.ref)
}
;
p.removeFromPhysicsCollisionFilter = function(a) {
    o.PA(a.ref)
}
;
p.convertToLocalVector = function(a, c, d) {
    return o.mA(a, c, d)
}
;
p.setVelocity = function(a, c, d) {
    o.wB(a, c, d)
}
;
p.setMass = function(a) {
    o.jB(a)
}
;
p.setModelMass = function(a) {
    o.mB(a)
}
;
p.setModelDensity = function(a) {
    o.kB(a)
}
;
p.setGroupMass = function(a) {
    o.fB(a)
}
;
p.setVelocityLocal = function(a, c, d) {
    o.xB(a, c, d)
}
;
p.setGroupVelocityLocal = function(a, c, d) {
    o.hB(a, c, d)
}
;
p.getVelocity = function() {
    return o.AA()
}
;
p.getVelocityLocal = function() {
    return o.BA()
}
;
p.getGroupVelocityLocal = function() {
    return o.vA()
}
;
p.getAngularVelocity = function() {
    return o.rA()
}
;
p.getAngularVelocityLocal = function() {
    return o.sA()
}
;
p.getGroupAngularVelocityLocal = function() {
    return o.uA()
}
;
p.setAngularVelocity = function(a, c, d) {
    o.XA(a, c, d)
}
;
p.setAngularVelocityLocal = function(a, c, d) {
    o.YA(a, c, d)
}
;
p.setGroupAngularVelocityLocal = function(a, c, d) {
    o.aB(a, c, d)
}
;
p.setDensity = function(a) {
    o.ZA(a)
}
;
p.setGroupDensity = function(a) {
    o.bB(a)
}
;
p.setRestitution = function(a) {
    o.tB(a)
}
;
p.setModelRestitution = function(a) {
    o.oB(a)
}
;
p.setGroupRestitution = function(a) {
    o.gB(a)
}
;
p.setFriction = function(a) {
    o._A(a)
}
;
p.setModelFriction = function(a) {
    o.lB(a)
}
;
p.setGroupFriction = function(a) {
    o.dB(a)
}
;
p.distanceConstraint = function(a, c, d, e, f, g, h, i) {
    return o.pA(a.ref, c, d, e, f, g, h, i)
}
;
p.attractorConstraint = function(a, c, d) {
    return o.kA(a.ref, c, d)
}
;
p.positionConstraint = function(a, c, d) {
    return o.NA(a, c, d)
}
;
p.rotationConstraint = function(a, c, d, e) {
    return o.WA(a, c, d, e)
}
;
p.planeConstraint = function(a, c, d) {
    return o.MA(a, c, d)
}
;
p.sliderConstraint = function(a, c, d) {
    return o.BB(a, c, d)
}
;
p.curveHandlerConstraint = function(a, c, d) {
    return o.oA(a, c, d)
}
;
p.curveConstraint = function(a, c, d) {
    return o.nA(a.ref, c, d)
}
;
p.gearConstraint = function(a, c, d, e, f, g, h, i) {
    return o.qA(a.ref, c, d, e, f, g, h, i)
}
;
p.jointToItem = function(a, c, d, e) {
    return o.HA(a.ref, c, d, e)
}
;
p.jointToGround = function(a, c, d) {
    return o.GA(a, c, d)
}
;
p.hingeJointToItem = function(a, c, d, e, f, g, h, i, j, k, l, m, n) {
    return o.DA(a.ref, c, d, e, f, g, h, i, j, k, l, m, n)
}
;
p.motorJointToItem = function(a, c, d, e, f, g, h, i, j, k, l, m, n) {
    return o.IA(a.ref, c, d, e, f, g, h, i, j, k, l, m, n)
}
;
p.removeFromConstraints = function() {
    o.OA()
}
;
p.applyImpulse = function(a, c, d, e, f, g) {
    o.iA(a, c, d, e, f, g)
}
;
p.applyImpulseLocal = function(a, c, d, e, f, g) {
    o.jA(a, c, d, e, f, g)
}
;
p.applyForce = function(a, c, d) {
    o.hA(a, c, d)
}
;
p.setForce = function(a, c, d) {
    o.$A(a, c, d)
}
;
p.setGroupForce = function(a, c, d) {
    o.cB(a, c, d)
}
;
p.setLayer = function(a) {
    o.iB(a)
}
;
p.setGroupLayer = function(a) {
    o.eB(a)
}
;
p.setStatic = function(a) {
    typeof a == Jlo ? o.vB(a) : o.uB()
}
;
p.setModelStatic = function(a) {
    o.pB(a)
}
;
p.isStatic = function() {
    return o.FA()
}
;
p.onPhysicsCollisionEnter = function(a) {
    return o.KA(a)
}
;
p.onPhysicsCollisionExit = function(a) {
    return o.LA(a)
}
;
p.onPhysicsCollision = function(a, c) {
    return o.JA(a, c)
}
;
p.clearCollisionHandlers = function() {
    return o.lA()
}
;
p.getMass = function() {
    return o.wA()
}
;
p.getDensity = function() {
    return o.tA()
}
;
p.getModelMass = function() {
    return o.xA()
}
;
p.isInGroup = function() {
    return o.EA()
}
;
p.hasPhysicsBody = function() {
    return o.CA()
}
;
p.restrictMovementAxis = function(a, c, d) {
    o.TA(a, c, d)
}
;
p.restrictRotationAxis = function(a, c, d) {
    o.UA(a, c, d)
}

#3

Thanks for having a look at it! I’m afraid I’m completely unfamiliar with javascript (and programming) so would can’t make sense of the code ><. I wonder if it is because the ‘block’ variable refers to a list rather than a specific object though?


#4

Hi yvonne!

turning a physics item back to normal is a method not available in Blockly yet, but you can access it via the code removePhysicsBody(). Below you can find a code example that builds on top of the code you made. I hope it’s helpful to you. Keep in mind that this method could change in later versions of CoSpaces (e.g. you don’t need to use the script-block anymore but a dedicated block for this functionality)

As you can see I created a function block called “bindWallBehaviour” and passed the ‘item’ as a parameter. In this function we set the hover and click behaviour for all walls from the “block” list. Next to changing its opacity onHover, it also turns the wall into a physics item on click. Then, when the item is clicked again we turn the item back normal and re-bind the behaviour on itself again.

This is the code in action

(You can use parameters for functions like this)

Hope this helps!


#5

Thanks @Stefan! this helps a lot! Didn’t realise I could specify parameters for functions - will be trying it out :slight_smile: