Log In  

Cart #33877 | 2016-12-21 | Code ▽ | Embed ▽ | No License
25

This sample code shows how to shoot projectiles aimed at a location.
It also shows how to make an enemy react to being hit.

Use direction keys to walk around
Press (X) to shoot

P#33878 2016-12-21 17:28 ( Edited 2017-03-01 15:23)

4

Cart #33892 | 2016-12-22 | Code ▽ | Embed ▽ | License: CC4-BY-NC-SA
4

press (C) to launch a grenade ;)

edit: err actually,
press (C) to shoot
press (X) to launch a grenade

Krystman, are you using a playstation controller (O & X reversed) ? ;)

P#33893 2016-12-21 22:07 ( Edited 2016-12-22 03:15)

Ha nice!

The X was meant as the Pico-8 (x). I thought that was what btn(4) was called?

P#33963 2016-12-22 11:42 ( Edited 2016-12-22 16:42)

Nice. Love the kick back on the alien. The grenade is awesome too, gonna have to borrow that code.

Here's the function I've been using forever to get the dx,dy for an object.

function dir_calc(ax,ay, bx,by, speed)
    local angle=atan2(ay-by, ax-bx)
    local dx=cos(angle)*speed
    local dy=sin(angle)*speed

    return dx,dy
end
P#33965 2016-12-22 11:54 ( Edited 2016-12-22 16:54)

@morningtoast Yes, it's the same one I use in the sample code.

Btw, I love how trigonometry works in PICO. 1=360° is so neat and intuitive.

P#33982 2016-12-22 15:39 ( Edited 2016-12-22 20:39)
1

funny that you'd both go through trigonometry for this,
I find it more straightforward to normalize the dir vector:

function shoot(ox,oy, tx,ty, speed)
    local dx,dy=tx-ox,ty-oy
    local d=speed/sqrt(dx*dx+dy*dy)
    return dx*d,dy*d
end

I think it should execute somewhat faster too.

on the other hand once you have the angle and/or its cos&sin you could use them to rotate a turret for instance.

I got kind of used to angles being in 0..1 but I still think degrees are more intuitive. the one thing I can't get used to is sin() being inverted. I usually end up putting minuses here or there until it looks good. that's a real PITA. on bigger projects I redefine those functions as they should be, but that's unfunny. also there's no tan().

buttons 4/5 (aka o/x) default on z/x on a keyboard (have a look in keyconfig),
A/B on an xbox controller and... X/O on a ps controller.
but well, you don't even need a ps controller to get confused about that...

P#33988 2016-12-22 17:51 ( Edited 2016-12-22 23:22)

@ultrabrite Yes, I usually avoid trigonometry myself if I can. It can be very useful tho. For example, you can spread out projectiles or make enemies miss on purpose. I posted the code for those cases where winging it doesn't cut it anymore and you REALLY need that specific formula. ;D

P#33990 2016-12-22 18:19 ( Edited 2016-12-22 23:19)

ha yes, of course, spread & miss, good point :)

P#33994 2016-12-22 18:29 ( Edited 2016-12-22 23:29)

Learning the angles and trig stuff was a hurdle for me. I had no prior experience with such math so it was uphill but once I had some functions to use, I understood things better. Anything with rotation, the trig is necessary (I assume) and I haven't seen any noticeable problems in performance...but I doubt I'm pushing things much with what I make.

P#34002 2016-12-22 20:24 ( Edited 2016-12-23 01:24)

I just was reading about normalizing vectors yesterday and the dot product between two vectors. I can't speak for anyone but myself, but I really love reading about how maths can explain/'draw'/represent the world. I knew going in I'd have to become more comfortable with using maths. I didn't know how much I'd love it though. Sometimes I find myself just reading math stuff on the web for the fun of it.. I wouldn't have guessed that about myself a few years back in a million years.

Degrees, radians, 0 to 1. It's all Pi to me. And coffee.

P#34011 2016-12-23 00:48 ( Edited 2016-12-23 05:48)
2

more projectiles :)

Cart #37967 | 2017-03-01 | Code ▽ | Embed ▽ | License: CC4-BY-NC-SA
2

(X) to launch a missile mark I
(C) to launch a mark II

P#37968 2017-03-01 10:23 ( Edited 2017-03-01 15:23)

[Please log in to post a comment]

Follow Lexaloffle:          
Generated 2024-03-19 06:25:44 | 0.020s | Q:29