Log In  

Cart #woyimijezi-0 | 2021-08-16 | Code ▽ | Embed ▽ | No License
2

I'm making my fitst pico8 game, and I'm trying to make an AI that shoots the bullet at where the player would be at once the bullet reaches it, so avoiding bullet won't be so easy like the small saucer's shots in asteroids does.

when i used game maker, I could easily implement it by using this example:https://www.gmlscripts.com/script/intercept_course

but since pico8 uses completely different langauge and there is no arcsin() function, it was impossible to replicate it for pico8.

function asin(y)
 return atan2(sqrt(1-y*y),-y)
end

local mex=s.x+4
local mey=s.y+4
local targetx=player.x+4
local targety=player.y+4

s.shootdir=atan2(targetx-mex,targety-mey)*360

local targetspd=dst(player.x,player.y,player.x+player.dx,player.y+player.dy)
local targetdir=atan2((player.x+player.dx)-player.x,(player.y+player.dy)-player.y)*360

local sdir=s.shootdir

local beta=(targetspd/s.shootspd) * sin((targetdir-sdir)*3.14159265/180)

s.shootdir+=asin(beta)*180/3.14159265

bulletdx=cos(t.shootdir/360)*t.shootspd
bulletdy=sin(t.shootdir/360)*t.shootspd

this was my trial of replicating the code in lua. similar to game maker's direction, 0 is east and 90 is north. but it didnot work properly, the enemy shoots the bullet at strange angles.

local mex=s.x+4
local mey=s.y+4
local targetx=player.x+4
local targety=player.y+4

local effientx = ((targetx-mex)/(s.shootspd/2))*player.dx
local effienty = ((targety-mey)/(s.shootspd/2))*player.dy

s.shootdir=atan2((targetx+effientx)-mex,(targety+effienty)-mey)*360

bulletdx=cos(t.shootdir/360)*t.shootspd
bulletdy=sin(t.shootdir/360)*t.shootspd

I have tried this as well, it seemed to work fine at first glance, but after more testing I realized that the enemy don't precisely aim the bullet at the moving player.

Does anyone know or give some hint about making prediction shooting in pico8's language ?

P#96080 2021-08-16 03:10 ( Edited 2021-08-16 07:45)

can you post a sample cart?
you can also simplify angle management - no need for radian/deg conversion on pico, read up how cos/sin are using angles (eg between 0-1)

P#96085 2021-08-16 07:22

okay, I have uploaded the cart press X to thrust and see the enemy's firing pattern.

if i shouldn't convert radian and deg, how should i write it ?

I added *pi/180 because that's how I understood according to the 11th page of this tutorial : https://demoman.net/?a=trig-for-games

P#96087 2021-08-16 07:47 ( Edited 2021-08-16 07:48)

GM first method works fine dropping all the useless angle conversions.
pico angles are between 0-1, not 0-Pi or 0-360
take care to negate beta (pico sin is reversed)
there are also a couple of useless calculations:

dist(x,y,x+dx,y+dy) = dist(0,0,dx,dy)
...
(x+dx)-x = dx
P#96088 2021-08-16 08:48 ( Edited 2021-08-16 09:28)

thank you so much for helping, freds72. it now works.

s.shootdir=dir(mex,mey,targetx,targety)

local targetspd=dst(player.x,player.y,player.x+player.dx,player.y+player.dy)
local targetdir=dir(0,0,player.dx,player.dy)

local sdir=s.shootdir

local beta=(targetspd/s.shootspd) * sin((targetdir-sdir)/-360)

s.shootdir+=asin(beta)*360
P#96092 2021-08-16 09:21

[Please log in to post a comment]

Follow Lexaloffle:          
Generated 2024-03-29 11:19:24 | 0.010s | Q:19