Maybe it's too late at night for my brain to be working right, But I'm trying to implement a shooter and I need to figure out a routine that will fire bullets from player x, y to a point specified by a mouse-run crosshair. So that no matter where the mouse or player char are in relation to eachother the bullets will fire from where the player is at that 'shot' action to where the crosshair was placed in the same instance..
I don't need pico-8 code specifically, Even pseudocode would help. I can't help but think I'm overthinking this right now.. D:
** Also I already have the mouse/crosshair/player bits going.
EDITEDITEDIT: This is going for a overhead perspective shooter where in lieu of a second joystick, Uses the mouse and a crosshair to simulate 'two stick controls' like in robotron 2084 or smash tv.
You could also use vector math.
local trajectory_dx = target_x - shooter_x local trajectory_dy = target_y - shooter_y local trajectory_len = sqrt(trajectory_dx^2 + trajectory_dy^2) local len_to_speed = bullet_speed / trajectory_len bullet_vx = trajectory_dx * len_to_speed bullet_vy = trajectory_dy * len_to_speed
That's more tokens but probably runs faster than using complex trig functions like atan. Well, on a real computer, anyway. I can't remember if pico-8 gives you a pass on the usual pain of calling atan.
In a more complete environment, with classes, methods, and a vector library, this might be more succinctly expressed like so:
local trajectory = target.position - shooter.position bullet.vel = trajectory:with_length(bullet_speed)
Many vector libraries even include a helper function to create a vector pointing from one point towards another, as it's a common need, e.g.:
bullet.vel = shooter.pos:towards(target.pos) * bullet.speed
This is nice because it can pretty much be read as natural language.
You'd need to roll your own vector library for that one, though, which gets costly in tokens. For huge apps that investment pays off in how terse the code outside the lib becomes, but in pico-8 apps there usually isn't a lot of that and you're lucky if you break even.
[Please log in to post a comment]