Forums

Topic: Petit computer how do I make a bullet?

Posts 1 to 5 of 5

MyLegGuy

In petit computer I was trying to make a bullet that shoots from the player and goes to tchx and tchy, but if it bumps into background tiles it stopes and prints "hit walll". I also want it to go the direction that tchx and tchy are from you, diognal, up, down, ect. I have no idea how to do this... Also its ok if you can't see the bullet and it goes super fast for the project i'm working on.

Hello.

Nintendo Network ID: MyLegGuy2

Discostew

I don't know whether the background scrolls or not, but I'll provide some info on the basis that the screen does not scroll.

First about the direction of the bullet. This will require some math as you are wanting to go from point A to point B. There are multiple ways you can do this, but I'll take the trigonometry approach. First, you get the angle (in radians) from point A to point B by using ATAN as so...

RADA=ATAN(destY - srcY, destX - srcX)

From there, you can grab the adjacent/opposite side lengths like a right triangle (where the hypotenuse would equal a length of 1) by throwing the angle into COS/SIN. You can give a speed value to these for the bullet so it doesn't travel at a combined 1 pixel per frame (it would be 1 pixel exactly if it were traveling completely horizontal or vertical)

XLEN=COS(RADA) * speed
YLEN=SIN(RADA) * speed

This gives the distance on each axis per frame, so you just increment the current position with these values each frame to have it travel as such. When you create the bullet, you initially set it to the position of the character that is shooting it. That will have it start from that position, and then go in the direction indicated.

As far as hitting background tiles, you have to have some information of these tiles. MY concept is to create an array that holds the state of each tile (still under the idea that this background does not scroll, so we'll keep this simple). A value of 1 would indicate that this is a solid tile while a value of 0 would be open. Let's make a room that is 32 tiles wide and 24 tiles long (the size of the screen in tiles), and fill in the edges with this solid state..

DIM ROOMSTA(32,24)
FOR J=0 TO 23
FOR I=0 TO 31
ROOMSTA(I,J)=(I==0 OR I==31 OR J==0 OR J==24)
NEXT
NEXT

Now, with each frame (assuming you have a loop that has one "VSYNC 1" command in it), you get the bullet's position (I usually do this after the moving stage where the sprite's position is incremented). To be able to use this position with relation to the background tiles, you would need to convert it to tile-space. It's simple as it only requires dividing the position by the width and height of the tiles, which is 8 for both, so....

TPOSX=SPOSX/8
TPOSY=SPOSY/8

Now, it's a simple matter of placing the results into the array to retrieve the value of the tile

TSTATE=ROOMSTA(SPOSX/8,SPOSY/8)

If TSTATE is 1, then it touched a solid tile. If it is 0, then it touched an open tile. do what you will with this result, like delete the bullet. Be away that this position is one point, so it is an approximation. The bullet could be bigger than a single pixel, and a side could look like it touches a solid tile but the position is on a spot that is open. Decide how you'll want to deal with that if required.

One other thing. If the entire room has no solid tiles, then it'll be possible that the bullets will travel outside the area. Accessing coordinates outside of the range you set for the array will result in an error (like [-1,4] or [7,32]). So, before you access the array, check to see if the position is valid.

IF SPOSX<0 OR SPOSX>=256 OR SPOSY<0 OR SPOSY>=192 THEN .......

In such a case where you want the bullet to just disappear if it reaches the end, then delete it and move on, without checking tile collision.

Hope this helps.

Discostew

3DS Friend Code: 4425-1477-0127 | Nintendo Network ID: Discostew

GraphicGenius

YAY, I got in before this forum was locked!

If Facebook, Myspace, Twitter, Instagram, and Snapchat were all destroyed, 90% of teens would go insane. If you're one of the 10% that would be laughing at them, copy & paste this into your signature and hope it happens. Wait was that just a joke?

3DS Friend Code: 1478-3545-5136 | Nintendo Network ID: GreatGamer123

  • Page 1 of 1

This topic has been archived, no further posts can be added.