Log In  


Cart #52771 | 2018-05-15 | Code ▽ | Embed ▽ | License: CC4-BY-NC-SA

Hi,

I am reduced to asking for help with the above. Pressing Z fires projectiles to a target XYZ that is Z+10 units "into" the screen (denoted as white targets). This is fine when the target X matches the source X (the player, basically) but when firing to the sides (+/- 150 units) the speed is very slow and I'm somehow able to fire much faster than I should be able to.

The relevant stuff is in update_bullets().

I'm using a fairly standard algo for moving towards a target that I'm pretty sure I used before for 2D stuff but I can't make it work in "3D". I assumed you just swapped out the Y for the Z.

Can somebody help me out?

Z = Fire
Left,Right = Shift the targets 150 units to the left or right.



You probably just need to use 3D math instead (pseudo code):
vx=target_x-player_x
vy=target_y-player_y
vz=target_z-player_z
d=sqrt(vxvx+vyvy+vzvz)
vx
=bullet_speed/d
vy=bullet_speed/d
vz
=bullet_speed/d

cache vx,vy,vz upon shooting (unless you want seeker missiles ;) and add them to x,y,z every frame.

hope that helps, cheers!


Generally your problem is with the way you set up the coordinate system. It's incredibly squashed in the z-axis. Things look further away than they are.

You're shooting at a target that is just 10 units away in the z-axis and 0 units away in the x axis. That's a distance of 10 units.

But then if you move the reticule you add a whopping 150 units on the x axis. That's a huge increase in the distance the bullet is supposed to fly - over 15 times! It takes 15 times longer for a bullet to reach that target.

But due to how you visualize that coordinate system it looks like you've barely changed the distance at all. So the bullet is appears as if it's moving slowly.

The shooting frequency is the same. It's just that when the bullets move slowly, they bunch together.


@Krystman - thank you, this seems to be exactly it. I've cheated and just increased the speed by a factor of 15 to make it look right. Practically, collisions still work so I'm not too worried. Out of interest, how can I "unsquash" my Z axis?


Difficult. Generally you would multiply Z with a factor in the DRAW_3D_SPRITE() function.

But now you've already set up a whole scene around a warped coordinate system. You'd have tweak ALL of the coordinates of your scene to work with the new coordinate system. I'm not familiar enough with your scene to make these changes.

The easiest fix for me is to instead stretch the X-axis. So something like this

--weapons test

z_depth=1500
view_depth=15
ground_height=-35
start_z=15
z_clip=.2
sc_x=64
sc_y=45

player_x=0
player_y=-22
player_z=.7
camera_x=0
can_fire=true
fire_timer=0
xsquash=20

bullet_range=10

-- target xyz variables
tx1=(player_x-1)
tx2=(player_x+1)
tz=player_z+bullet_range
ty=1

bullet_list={}

function draw_3d_sprite(x,y,z,width,height,sprite_x,sprite_y,sprite_width,sprite_height,is_flip)
 if z>z_clip and z<view_depth then
  pw=width/z
  ph=height/z
  px=sc_x+(x+camera_x)/z*xsquash
  py=sc_y-(y+player_y)/z-ph
  sspr(sprite_x,sprite_y,sprite_width,sprite_height,px-pw/2,py,pw,ph,is_flip,false)
 end
end

function player_input()
 tx1=(player_x-1)
 tx2=(player_x+1)
 if btn(1) then
  -- right, shift target 150 units right.
  tx1=(player_x-1)+10
  tx2=(player_x+1)+10
 end
 if btn(0) then
  -- left, shift target 150 units left.
  tx1=(player_x-1)-10
  tx2=(player_x+1)-10
 end
 if btn(4) and can_fire then
  -- fire
  create_enemy_bullet(player_x-1,player_y,1,tx1,tz)
  create_enemy_bullet(player_x+1,player_y,1,tx2,tz)
  can_fire=false
 end
 -- fire delay
 if not can_fire then 
  fire_timer+=1
  if fire_timer>=5 then 
   can_fire=true 
   fire_timer=0
  end 
 end
end

Next time I would strongly suggest to set up a regularly-spaced grid of dots/wireframe to make sure you have a correct-looking perspective before implementing a scene.


@Krystman - I didn't know what I was doing when I started :D Mutter....still don't....mutter. I'll tweak it a bit. Thanks :)


if it can help, I recently posted a sample cart that achieve something similar (e.g. basic 3d view).
Might be of help to get you started alright:
https://www.lexaloffle.com/bbs/?pid=52525#p52541


Hi @freds72 - yeah, I've got that bookmarked :)



[Please log in to post a comment]