❔ Checking shots trajectory in a game
I am making Worms game and when worm is using ranged weapon I somehow need to check shooting trajectory, all weapons shoot straight without any bullet drop but I still need to know if path is clear. I was thinking of making a path where shot would go and then check every tile of path if it is empty but I am not sure how to do it, I have distance between 2 worms, worms locations and I counted the angle with atan2 but I dont know what to do next
14 Replies
I am mostly thinking what to do with weird angles, I know what to do when angle is 0. 45, 90 and so
This all really depends on how your map is structured, how your entities are stored, ...
In general, you'd do something called a "ray cast", which essentially check if a ray starting from some point with a certain angle will hit anything in its way
A very simple way to do this, assuming you have collisions figured out is to just loop through all entities on your map and see if the ray originating from your worm collides with any of them
but this ray how do I actually get it?
You write it. It's not a physical or visible ray, it's just what game development calls an origin point and an angle/direction 😄
yes but when there is weird angle
this ray would be easy to make if angle points it to one side or at 45 angle from some direction
just (0,0), (1,1) ...
but I am not sure what to do when angle is like 39
or some other weird number
X could hit the O on left but angle is probably weird
even 44 could complicate stuff over distance
only solution I can think of is restricting shooting angles
only rotate it every 45 degrees
Ah like that.
So you have the angle
You can make a vector from that, a vector of unit length
You know what that is?
vector is sonynom to array right
No
This is the mathematical vector. Angle + length
I have both distance between worms and angle
but not as combined unit
I counted the distance for something else with Pytaghor
So if you have the difference of y and x coords between the 2 worms
You can divide these by the distance
Let's call them dx and dy
Now if you start from one worm and keep adding dx and dy to that, you'll step through the terrain
Eventually reaching the other worm
Ofc these are floats/doubles, when checking for a tile you temporarily round these
so it makes both x and y distance have same number of variables basicly
yes I finally made it work
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.