开发者

Moving an image forward, but syncing it with the rotation?

开发者 https://www.devze.com 2023-03-28 07:16 出处:网络
Okay, so I\'m making a small tank game, and I need to make my tank shoot bullets,开发者_JS百科 the tank can move, turn, the top of the tank turns on it\'s own, with the mouse. Now my problem is, how w

Okay, so I'm making a small tank game, and I need to make my tank shoot bullets,开发者_JS百科 the tank can move, turn, the top of the tank turns on it's own, with the mouse. Now my problem is, how will I make it so that it looks like the bullet shoots out of the barrel, with the good angle, and position? I'm kind of puzzled.


Easiest solution is to start the bullets from the beginning of the barrel/center of your tank and sort them behind the barrel and tank and fire them the same direction as your barrel is pointed. Requires no special rotation, but causes a slight delay until the bullet appears from behind the tank.

Or, you do some math. Put a hotspot or record the coordinate of the beginning of the barrel and when you rotate the barrel, rotate this point with it. This will then be the new point to spawn your bullets.


3D APIs such as DirectX and OpenGL have a concept called "Model Space" versus "World Space"; where Model Space is a set of coordinates expressed in relation to a single model, regardless of where its at in the world, and World Space is the set of coordinates that relate to the entire world itself. To take a coordinate from Model Space, where the coordinates only have meaning to an individual model, and convert it into World Space, where the coordinates have meaning to the entire game world, you multiply the coordinates through a matrix, called the World Matrix, which is unique to each instance of a model in the world.

So basically:

{X,Y coordinates in Model Space} * {model instance's World Matrix} = {X,Y coordinates in World Space}

You should apply this same approach to your 2D game. The end of the barrel on your tank sprite is always at the same X,Y coordinate of your tank's image. But your tank's image is undergoing some transformation when it's being displayed to the screen (a rotation around a point). You basically just need to take the X,Y coordinate of the end of the barrel on the tank image and apply that same transformation to it, and that'll give you the X,Y coordinates of where the end of the barrel is currently at on the screen.

Then from there, take the rotation angle of the tank to determine which angle the projectile should go after it's been fired.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号