Log In  

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.

P#47351 2017-12-13 20:22 ( Edited 2017-12-14 14:18)

angle=atan2(target_x-shooter_x, target_y-shooter_y)

bullet_vx=cos(angle)*bullet_speed
bullet_vy=sin(angle)*bullet_speed

have a look there: angle calculation sample code by Krystman

P#47355 2017-12-13 22:14 ( Edited 2017-12-14 03:14)

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.

P#47365 2017-12-14 05:06 ( Edited 2017-12-14 10:06)

Thanks guys, This at least gives me a place to start now. X3

P#47373 2017-12-14 09:18 ( Edited 2017-12-14 14:18)

[Please log in to post a comment]

Follow Lexaloffle:          
Generated 2024-04-20 11:23:10 | 0.016s | Q:14