1 decade ago by RockLeo
Hi everyone,
I'm trying to make some Emanuelle Feronato's examples of box2d and I'm having a bad time with the coordinate system that box2d use. I'm not a pro dev.
I'm trying to draw a simple line between the entity and the click point.
I'm using a laser beam code that I found in the forum:
Entity Laser
If I use "this.pos" to get the start position of the entity, for some reason, when the entity start moving the draw line goes crasy and leaves the screen.
I I use getWorldCenter (setting center of my code) the start point gets up high in the screen, not even close the entity in the "ground".
The laser end point works beatifully when the canvas is scaled to 1, but if I scale it, no good. The laser end dont get near the click point.
I'm trying to make some MouseToWorld function and EntityToWorldCoord function so I can draw this line between those points.
Thx!
I'm trying to make some Emanuelle Feronato's examples of box2d and I'm having a bad time with the coordinate system that box2d use. I'm not a pro dev.
I'm trying to draw a simple line between the entity and the click point.
I'm using a laser beam code that I found in the forum:
ig.game.spawnEntity( EntityLaser, ig.game.player.pos.x,ig.game.player.pos.y, {center:ig.game.player.body.GetWorldCenter()});
Entity Laser
EntityLaser = ig.Entity.extend({ start: {}, target: {}, gravityFactor:0, laserLength:null, maxVel:{x:1000, y:1000}, type:ig.Entity.TYPE.NONE, checkAgainst:ig.Entity.TYPE.B, collides:ig.Entity.COLLIDES.PASSIVE, init: function(x, y, settings) { this.parent(x,y,settings); //this.timer = new ig.Timer(1); }, update: function() { if (/*this.timer.delta() >= 0 || */ig.input.released('mouseLeft') || ig.input.pressed('mouseLeft')==false) { this.kill(); } this.parent(); }, draw: function() { var laserEndX = ig.input.mouse.x; var laserEndY = ig.input.mouse.y; var laserStartX = this.pos.x; var laserStartY = this.pos.y; // straight line laser only demo ig.system.context.fillStyle = "blue"; ig.system.context.font = "bold 16px Arial"; ig.system.context.fillText("laserstart="+laserStartX+" - laserend="+laserStartY, ig.input.mouse.x, ig.input.mouse.y); ig.system.context.strokeStyle = "red"; ig.system.context.lineWidth = 5; ig.system.context.beginPath(); ig.system.context.moveTo(laserStartX,laserStartY); ig.system.context.lineTo(laserEndX,laserEndY); ig.system.context.stroke(); ig.system.context.closePath(); } });
If I use "this.pos" to get the start position of the entity, for some reason, when the entity start moving the draw line goes crasy and leaves the screen.
I I use getWorldCenter (setting center of my code) the start point gets up high in the screen, not even close the entity in the "ground".
The laser end point works beatifully when the canvas is scaled to 1, but if I scale it, no good. The laser end dont get near the click point.
I'm trying to make some MouseToWorld function and EntityToWorldCoord function so I can draw this line between those points.
Thx!