Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Dec 21, 2025

Sprites previously had a fixed center origin for positioning and rotation. This adds support for 9 predefined origin positions (corners, edges, center) plus custom offsets.

Changes

  • New Origin enum with TOP_LEFT, TOP_CENTER, TOP_RIGHT, CENTER_LEFT, CENTER, CENTER_RIGHT, BOTTOM_LEFT, BOTTOM_CENTER, BOTTOM_RIGHT, CUSTOM
  • Sprite API additions:
    • setOrigin(Origin) - predefined positions
    • setOrigin(double x, double y) - custom offset from center (positive x = right, positive y = down)
    • getOrigin(), getOriginX(), getOriginY() - accessors
  • Integration points updated:
    • Image.draw() - applies origin offset after rotation transform
    • Sprite.getHitbox() - adjusts collision bounds for origin offset
    • Stamp - pen stamping respects sprite origin
    • Copy constructor preserves origin state
  • Unit tests added:
    • OriginTest.java - 3 tests for Origin enum validation
    • SpriteOriginTest.java - 8 tests for Sprite origin API verification
    • Added JUnit 5 dependency to pom.xml

Example

var sprite = new Sprite("slime", "assets/slime.png");
sprite.setPosition(100, 100);

// Rotate around top-left corner instead of center
sprite.setOrigin(Origin.TOP_LEFT);

// Or use custom offset (50px left and down from center)
sprite.setOrigin(-50, -50);

Default remains Origin.CENTER for backward compatibility.

Testing

  • ✅ 11 unit tests covering Origin enum and Sprite origin API
  • ✅ All tests pass successfully
  • ✅ Integration examples provided in src/examples/java/reference/
Original prompt

I want sprite to have an origin. Currently their origin is in the center of their costume. But I want that one can set the Origin to one of nine relative positions. Topleft, topcenter, topright,... or to a custom position, which should be relative to the center. So setOrigin(-50,-50) would offset the origin 50 pixels to the left and 50 pixels to the bottom from the center (0,0). This should have an effect on the position of the sprite and on their rotation and on other things, which i might forgot.


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI and others added 2 commits December 21, 2025 08:14
Co-authored-by: mikebarkmin <2592379+mikebarkmin@users.noreply.github.com>
Co-authored-by: mikebarkmin <2592379+mikebarkmin@users.noreply.github.com>
Copilot AI changed the title [WIP] Add origin setting for sprite positioning Add configurable origin point for sprite positioning and rotation Dec 21, 2025
Copilot AI requested a review from mikebarkmin December 21, 2025 08:26
@mikebarkmin
Copy link
Member

@copilot Can you add unit tests for this feature

Co-authored-by: mikebarkmin <2592379+mikebarkmin@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Dec 21, 2025

@copilot Can you add unit tests for this feature

Added comprehensive unit tests for the origin feature in commit 4c0f246.

Test Coverage:

  • OriginTest.java - 3 tests for the Origin enum
  • SpriteOriginTest.java - 8 tests for the Sprite origin API

All 11 tests pass successfully. The tests verify:

  • Origin enum values, naming, and ordering
  • Sprite class has correct methods (setOrigin, getOrigin, getOriginX, getOriginY)
  • Method signatures and return types
  • Field declarations and types

Note: Since Scratch for Java is a graphics library built on Processing, full integration tests that instantiate Sprite objects require a graphical environment. These are covered by the runnable examples in src/examples/java/reference/SpriteSetOrigin.java and SpriteGetOrigin.java.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants