gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

Coding > Self Moving Sprites

#8560 - Snoobab - Mon Jul 14, 2003 6:55 pm

Hi Guys.

I have a question I hope one of u will be able to answer.

I want to make a space blaster (space invaders?) type game but I'm having a little trouble. I want my spaceship to fire - sprites - when I press the A button, I want those sprites to move at a constant (seeable) speed across the screen towards my enemys. The problem is that I can't get the bullet sprite to move on its own once the A button has been pressed. Here's my move sprite function:

Code:


__inline void LaunchBullet(void)
{
   if(BulletPos.y > 0)
   {
         BulletPos.y --;
   }
}



Bullet is an instance of a 2D Vector struct(Just contains x and y co-ords).

This function moves the bullet only when the A button is held down(i.e. the bullet doesn't keep on moving by itself.) I've tried various loops to try and make the bullet move on its own but none have seemed to work correctly('cause I've been doing something wrong!) Any ideas on how I could get this to work correctly?

Also, how would I implement collision detection is such a game? (Sry, this is my first GBA game attempt =) Would bounding rectangles or per-pixel be best?

Thanks a lot,

- Ed.[/code]

#8563 - niltsair - Mon Jul 14, 2003 7:07 pm

You have to use a timer. When a bullet is shot, reset the timer and start it. every few milliseconds (you have to check the time elapsed in your main game loop) check if you should move the bullet (based on the amount of time to wait in between each move).

#8570 - col - Tue Jul 15, 2003 12:27 am

niltsair wrote:
You have to use a timer. When a bullet is shot, reset the timer and start it. every few milliseconds (you have to check the time elapsed in your main game loop) check if you should move the bullet (based on the amount of time to wait in between each move).


You certainly do not 'have' to use a timer, there are numerous ways of solving this basic problem.

A common way to deal with movement in games is to lock everything to the vblank interrupt.
every frame, all the entities in the game are updated including the bullets. If you use fixed point arithmetic for the coordinates, you can have your objects move a pretty much whatever speed you like... no need for a timer.

The only problem is how to manage the active entities (bullets, baddies etc)

one simple way is to have a fixed number of each type of game entity pre-assigned and check them all every frame - if they're 'on' you update position, test for collisions etc. this works well for simple games

a more powerfull though more complex way is to use some sort of container - eg. a linked list. Every time an entity is created, add it to the list, when it is destroyed, remove it from the list. Each frame, iterate through the list of entities calling the update function for each one.


cheers

Col

#8583 - ampz - Tue Jul 15, 2003 4:12 pm

Actually, the vblank is a timer.

#8671 - NitroSR - Thu Jul 17, 2003 6:50 pm

Two words... PHYSICS SIMULATION

One of the things I love about Super Mario Brothers is that it's a essentially a cutesy physics simulations. All of the entities have positions, velocities, and respond to forces and impulses. Crack open a physics book and learn yourself some Newton. You'll find that you can make highly robust games by designing an engine that is based on physics simulation.

Of course, for a space shooter, you may need to depart a bit from pure unadulterated physics, seeing that many enemy ships follow patterns and the like, but that shouldn't be a problem now should it? ;)

Good luck!!

#8699 - kiwibonga - Fri Jul 18, 2003 8:25 am

I think it's a very simple mistake, although I could in turn be mistaken, as a long-time GBA programming newbie, I've experienced the same problems... You probably created the bullet OBJ at (Bulletpos.x,Bulletpos.y) and updated the OAM. The thing is, Bulletpos.x and Bulletpos.y are just numbers, meaning that whenever they change, you should update the OAM entry for the OBJ with their new value by actively changing its coordinates. In HAM you'd just write ham_SetObjXY(Bulletsprite,x,y) followed by ham_CopyObjToOAM()...

THEN AGAIN

If your ship is moving, therefore you probably know about this already, and I've made a fool out of myself ;)
_________________
http://www.kiwibonga.com

#8745 - XeroxBoy - Sat Jul 19, 2003 1:29 am

My thoughts are that he's only calling LaunchBullet() when you press A, not in the actual game loop. That's what seems the most likely to me, but it's impossible to tell without seeing the rest of the code.

#8755 - kiwibonga - Sat Jul 19, 2003 4:16 am

Heh... You're right... It *has* to be it...
_________________
http://www.kiwibonga.com

#8771 - Snoobab - Sat Jul 19, 2003 1:48 pm

Yeah, I was only calling LaunchBullet() when A was pressed. I've changed it a bit now and put it in the main game-loop; all works well.

Thanks a lot for your help.

#8801 - Paul Shirley - Sun Jul 20, 2003 2:33 pm

removed