[Newbies] Bounce Example

Bert Freudenberg bert at freudenbergs.de
Thu Nov 20 14:52:47 UTC 2014


On 20.11.2014, at 04:14, <summae3443 at mypacks.net> <summae3443 at mypacks.net> wrote:

> Hello,
> 
>  
> I once saw a workspace example which caused a morph to move along an arc in a realistic way - appearing to be affected by gravity. Possibly written by someone from Disney?  Now I search in vain to find it. Please give me a reference.

Morphic is not really set up for workspace-style programming. It assumes you will create your own morph class and implement methods to specify behavior.

It's certainly possible to do. But you will run into various problems that you wouldn't have if you did it properly.

That said, try the following. Assuming constant mass and gravity, the acceleration is constant (since F = ma).

m := Morph new openInWorld.
m position: World center.
speed := 5 @ 0.	"move horizontally at start"
acceleration := 0 @ 1.	"accelerate downwards"
move := [m owner ifNotNil: [
	"Acceleration is a 2nd-order (quadratic) function. Physics and Math FTW"
	speed := speed + acceleration.			"accel is 1st derivative of speed"
	m position: m position + speed.		"speed is 1st derivative of pos"
	m left < m owner left ifTrue: [speed := speed x abs @ speed y].		"bounce"
	m right > m owner right ifTrue: [speed := speed x abs negated @ speed y].
	m top < m owner top ifTrue: [speed := speed x @ speed y abs].
	m bottom > m owner bottom ifTrue: [speed := speed x @ speed y abs negated].
	(move future: 20) value.		"repeat every 20ms"
]].
move value.		"first step"

For even more fun you could control the morph with a joystick:

	j := JoystickMorph new openInWorld.

And insert this inside the move block:

	acceleration := (j leftRight @ j upDown negated) * 0.1.

Have fun!

- Bert -


-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/pkcs7-signature
Size: 4142 bytes
Desc: not available
Url : http://lists.squeakfoundation.org/pipermail/beginners/attachments/20141120/6d3735c0/smime.bin


More information about the Beginners mailing list