straker / kontra

A lightweight JavaScript gaming micro-library, optimized for js13kGames.
https://straker.github.io/kontra/
MIT License
963 stars 100 forks source link

Sprite#sx/y does not work correctly with Sprite#anchor and Sprite#rotation #197

Closed Andrek25 closed 2 years ago

Andrek25 commented 4 years ago

I'm trying to rotate the Sprite in the center and works fine but when i use tileEngine.sy = 50 it does not work correctly. This is a example code:

let {init, GameLoop, Sprite, TileEngine} = kontra

let { canvas, context } = init('game')

tileEngine = TileEngine({
    tilewidth: 64,
    tileheight: 64,
    width: 9,
    height: 9,
    tilesets: [],
    layers: []
})

let sprite = Sprite({
    x: 100,
    y: 100,
    width: 50,
    height: 50,
    color: "red",
    anchor: {x: 0.5, y: 0.5}
})

tileEngine.addObject(sprite)
tileEngine.sy = 50

let angle = 0

loop = GameLoop({
    update() {
        angle += 1
        if (angle > 360) {
            angle = 0
        }
        sprite.rotation = angle * Math.PI / 180
        sprite.update()
    },
    render() {
        sprite.render()
    }
})

loop.start()
straker commented 4 years ago

The tile engine uses the camera position to clip the tile map image. Firefox and Safari had bugs where if the clip size was not within the images region, it wouldn't draw it. So depending on the size of your canvas the camera won't move if it's the same size or bigger than the tile map (579 px).

This is different than how the camera works for something like a Scene (where the camera is used as an offset to drawing), so I might need to better consolidate the two.

Andrek25 commented 4 years ago

Oh sorry, i didn't explain my problem correctly.

My problem is that when i rotate a Sprite over its center point using anchor: {x: 0.5, y: 0.5} it works perfectly but when i use tileEngine.sy = 50 to move the camera the center points is changed and the sprite start to rotate around other point(like the earth around the sun).

straker commented 4 years ago

Ah, I see the problem now. That is indeed a bug, more with the order of rendering than the tile engine as just setting sprite.sy = 50 has the same problem.

Andrek25 commented 4 years ago

You'll solve it before JS13k ends or is there a fast solution for this?

straker commented 4 years ago

I'm not sure there is a fast solution to this one, which also means I'm not sure if I can get a fix out in less than a week. The main problem is competing concepts of the camera. To maintain the rotation/anchor I'd have to move the camera before rotating, but that means all camera sx/sy are fixed to the x/y axis and cannot be affected by rotation. I have to think about if that's how it should be or not.

Andrek25 commented 4 years ago

You're right this need a while, take all the time you need :)

Edit: Sorry for the missclick haha.