ippa / jaws

Jaws - HTML5 canvas javascript 2D Game Framework
https://jawsjs.ippa.se
GNU Lesser General Public License v3.0
363 stars 75 forks source link

Sprite custom anchor points #55

Closed phantomxc closed 12 years ago

phantomxc commented 12 years ago

I believe you intended for custom sprite anchor points at one time but it doesn't appear the code is working.

lines 1145 and 1146 in jaws.js have the following:

if(!options.anchor_x == undefined) this.anchor_x = options.anchor_x;
if(!options.anchor_y == undefined) this.anchor_y = options.anchor_y; 

The following code doesn't seem to work without modification:

ttop = new jaws.Sprite({'image':'images/tanktop.png', x:100, y:100, 'anchor_x':0.5, 'anchor_y':0.75});
ippa commented 12 years ago

Hm, I don't see any reasons why it shouldn't. "anchor_y" for example, it basically just used in two places:

this.top_offset    = this.height * this.anchor_y
this.bottom_offset = this.height * (1.0 - this.anchor_y)

that's from jaws.Sprite's cacheOffset()

Are you sure you're not doing anything else strange? Or that it works but you don't notice it.

phantomxc commented 12 years ago

I've pushed an example to the following repo

https://github.com/phantomxc/Boom

in game.html you will see the rotation of the turret is using the default anchor even though I pass in an anchor_x and anchor_y.

in modifiedgame.html the only difference is the inclusion of modifiedjaws.js

I only discovered your awesome library last night so forgive me if I'm missing something :)

ippa commented 12 years ago

Right, looking deeper into jaws.Sprite.prototype.draw() I notice jaws doesn't look at anchor_x/y when rotating, only when drawing the final sprite. We need to do this.context.translate(-this.left_offset, -this.top_offset) .. Before ... jaws.context.rotate(this.angle * Math.PI / 180) for that to work.

I'm not 100% sure we want that though. Maybe we want two new properties, rotation_anchor_x, rotation_anchor_y (or similar). For example a simple platformer.. we wan't the players feet to be the anchor for drawing, so we set anchor_y = 1 ... but when he dies you want him to rotate around his own angle (which would mean anchor_x/y = 0.5).

What's your thoughts on this?

phantomxc commented 12 years ago

I'm not sure I understand why "if(!options.anchor_x == undefined)" couldn't just be modified as that never evaluates. If that evaluates, everything seems to work just fine.

However, an option named rotation_anchor_x/y clearly tells you it's purpose so I wouldn't mind that either.

ippa commented 12 years ago

Fixed in https://github.com/ippa/jaws/commit/faa7787a08a378ab0e8c5f8562f17d0ff722ac44 .. thanks for the report!