Cart #52771 | 2018-05-15 | Code ▽ | 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.

P#52773 2018-05-15 07:56 ( Edited 2018-05-15 18:56)

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!

P#52782 2018-05-15 12:24 ( Edited 2018-05-15 16:24)

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.

P#52783 2018-05-15 12:37 ( Edited 2018-05-15 16:46)

@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?

P#52785 2018-05-15 13:14 ( Edited 2018-05-15 17:14)

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=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.

P#52786 2018-05-15 13:53 ( Edited 2018-05-15 17:53)

@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 :)

P#52787 2018-05-15 14:12 ( Edited 2018-05-15 18:12)

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

P#52788 2018-05-15 14:23 ( Edited 2018-05-15 18:23)

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

P#52790 2018-05-15 14:56 ( Edited 2018-05-15 18:56)