The provided Java code is an extension for the MIT App Inventor, specifically tailored for physics simulations. I’ll explain each public method in simple terms, suitable for someone familiar with block-based programming like in Kodular Creator, but not with Java.
Properties
-
Canvas Related
-
SetCanvas(Canvas canvas)
- Connects a Canvas component for drawing purposes.
-
GetCanvasWidth()
- Returns the width of the canvas.
-
GetCanvasHeight()
- Returns the height of the canvas.
-
SetCanvasWidth(Number newWidth)
- Sets a new width for the canvas.
-
SetCanvasHeight(Number newHeight)
- Sets a new height for the canvas.
-
Physics Simulation Control
-
Update Control
-
StartUpdates(Number updateTimeMs)
- Begins periodic physics updates every specified number of milliseconds.
-
StopUpdates()
- Stops these periodic updates.
-
-
Object Management
-
AddObject(Number id, Number x, Number y, Number width, Number height, Number mass, Number friction)
- Adds a physical object with specific properties like position, size, mass, and friction.
-
RemoveObject(Number id)
- Removes an object from the simulation.
-
ClearObjects()
- Clears all objects from the simulation.
-
-
Object Interaction
-
ApplyForce(Number id, Number forceX, Number forceY, Number durationMs)
- Applies a force to an object for a specified duration.
-
SetObjectProperties(Number id, Number x, Number y, Number width, Number height, Number mass, Number friction)
- Updates properties of an existing object.
-
GetObjectPosition(Number id)
- Returns the position of an object as a list [x, y].
-
SetGravity(Number x, Number y)
- Sets global gravity affecting all objects.
-
GetObjectVelocity(Number id)
- Returns the velocity of an object as a list [vx, vy].
-
Advanced Physics Features
-
Angular Velocity
-
UpdateAngularVelocity(Number id, Number angularVelocity)
- Updates the angular velocity of an object.
-
ApplyTorque(Number id, Number torque)
- Applies torque to an object.
-
GetAngularVelocity(Number id)
- Returns the angular velocity of an object.
-
-
Other Object Properties
-
GetObjectMass(Number id)
- Returns the mass of an object.
-
MakeObjectJump(Number id, Number jumpStrength, Number durationMs)
- Makes an object jump by applying an upward force for a short duration.
-
SetObjectVelocity(Number id, Number velocityX, Number velocityY)
- Sets the velocity of an object.
-
SetObjectSize(Number id, Number width, Number height)
- Sets the size of an object.
-
SetObjectPosition(Number id, Number x, Number y)
- Sets the position of an object.
-
Collision Detection
-
AreObjectsColliding(Number id1, Number id2)
- Checks if two objects are colliding.
Animation Control
-
StartAnimation(imagePathsList, Number frameDurationMs, Text objectId)
- Starts animating a sequence of images on a canvas object.
-
StopAnimation(Text animationId)
- Stops the specified animation.
Layer Management
-
CreateLayer(Text layerName, Number zIndex)
- Creates a new layer for drawing.
-
SetActiveLayer(Text layerName)
- Sets an active layer by its name.
-
GetActiveLayer()
- Returns the name of the active layer.
Image Manipulation
-
MirrorImageHorizontally(Text imagePath)
- Mirrors an image horizontally.
-
MirrorImageVertically(Text imagePath)
- Mirrors an image vertically.
-
GetResizedImage(Text imagePath, Number newWidth, Number newHeight)
- Resizes an image and returns its path.
Event Handling
-
CanvasSizeChanged(Number width, Number height)
- Event triggered when the Canvas size changes.
-
OnAnimationFrameChanged(Text objectId, Text imagePath)
- Triggered when an animation frame changes.
-
OnCollision(Number objectId1, Number objectId2, Text collisionSide)
- Triggered when two objects collide.
-
OnPositionChanged(Number objectId, Number x, Number y)
- Triggered when an object’s position changes.
-
OnUpdate()
- Called on every update of the physics cycle.
-
ReportError(Text errorMessage)
- Reports an error with a custom message.
-
TimedEvent(Number id)
- Triggered after
a specified delay.
Cleanup
-
ShutDown()
- Cleans up resources and shuts down the physics simulation.
Each of these methods translates a complex Java operation into a simple action or event that can be easily understood and used in a block-based programming environment like Kodular Creator.
Files
- AIX: com.bosonshiggs.physicsengine.aix (84.6 KB)
- AIA: Request in the Discord group