1 Replies - 645 Views - Last Post: 26 June 2014 - 10:10 PM Rate Topic: -----

#1 irkzuptang  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 04-May 13

Game Programming Projectile shoot (angry birds like)

Posted 23 June 2014 - 11:30 AM

Hey, I'm developing an angry-birds like game. But it shoots a ball into a basket. I have a problem on the game logic. I do not know how the ball will be shut from the slingshot, to the basket on the right using projectile maybe? I don't really know.

Note that when I click my left mouse button, the ball will be shut to the right, and the basket should be moving on its x-axis while I try to shoot the ball into it.

#include "game.h"

LPDIRECT3DTEXTURE9 ball_image, basket_image;
SPRITE ball, basket;
LPDIRECT3DSURFACE9 intro, lvl1, lvl2, lvl3, win, gameOver;
LPD3DXSPRITE sprite_handler;
HRESULT result;

int ScrollX, ScrollY;			//current scroll position
int SpeedX, SpeedY;				//scroll speed

bool spawn, startGame, gameOver;
int currentLevel, gscore;

long start = GetTickCount();

int Game_Init(HWND hwnd)


	//Initialize game variables
	gameOver = false;
	startGame = false;
	currentLevel = 0;

	//create sprite handler object
	result = D3DXCreateSprite(d3ddev, &sprite_handler);
	if (result != D3D_OK)
		return 0;

		//load the truck sprite
	ball_image = LoadTexture("ball.bmp", D3DCOLOR_XRGB(255,0,255));
	if (ball_image == NULL)
		return 0;

	basket_image = LoadTexture("basket.bmp", D3DCOLOR_XRGB(255,0,255));
	if (ball_image == NULL)
		return 0;

	//load the background images
	intro = LoadSurface("instructions.jpg", NULL);
		if (intro == NULL)
			return 0;
	lvl1 = LoadSurface("Background-01.jpg", NULL);
		if (lvl1 == NULL)
			return 0;
	lvl2 = LoadSurface("Background-02.jpg", NULL);
		if (lvl2 == NULL)
			return 0;
	lvl3 = LoadSurface("Background-03.jpg", NULL);
		if (lvl3 == NULL)
			return 0;
	gameOver = LoadSurface("gameover.jpg", NULL);
		if (gameOver == NULL)
			return 0;
	win = LoadSurface("win.jpg", NULL);
		if (win == NULL)
			return 0;


	//set the truck's properties
	/*truck.x = (SCREEN_WIDTH / 2) - 90;
	truck.y = SCREEN_HEIGHT-150;
	truck.movex = 4;
	truck.width = 180;
	truck.height = 111;
	truck.curframe = 0;
	truck.lastframe = 3;
	truck.animdelay = 3;
	truck.animcount = 0;*/

	//load bounce wave file
	/*sound_intro = LoadSound("1.wav");
	if (sound_intro == NULL)
		return 0;
	sound_game = LoadSound("2.wav");
	if (sound_intro == NULL)
		return 0;
	//return okay
	return 1;

void Game_Run(HWND hwnd)
	//make sure the Direct3D device is valid
	if (d3ddev == NULL)

	//after short delay, ready for next frame?
	//this keeps the game running at a steady frame rate
	if (GetTickCount() - start >= 30)
		//reset timing
		start = GetTickCount();

		if (++truck.animcount > truck.animdelay)
			//reset counter
			truck.animcount = 0;

	if (d3ddev->BeginScene())
		if (gameOver)
		else {

		//stop rendering

	d3ddev->Present(NULL, NULL, NULL, NULL);

	//check for escape key (to exit program)
		PostMessage(hwnd, WM_DESTROY, 0, 0);


void Game_End(HWND hwnd)

	if (ball_image != NULL)

	if (intro != NULL)

	if (lvl1 != NULL)

	if (lvl2 != NULL)

	if (lvl3 != NULL)

	if (gameOver != NULL)

	if (sprite_handler != NULL)

	/*if (sound_intro != NULL)
		delete sound_intro;

	if (sound_game != NULL)
		delete sound_game;*/

void LevelSelect() {
	case 1:
		d3ddev->StretchRect(lvl1, NULL, backbuffer, NULL, D3DTEXF_NONE);
	case 2:
		d3ddev->StretchRect(lvl2, NULL, backbuffer, NULL, D3DTEXF_NONE);
	case 3:
		d3ddev->StretchRect(lvl3, NULL, backbuffer, NULL, D3DTEXF_NONE);
	case 4:

#ifndef _GAME_H
#define _GAME_H

//windows/directx headers
#include <d3d9.h>
#include <d3dx9.h>
//#include <dxerr.h>
//#include <dsound.h>
//#include <windows.h>
#include <time.h>
#include <stdio.h>
#include <stdlib.h>

//framework-specific headers
#include "dxgraphics.h"
//#include "dxaudio.h"
#include "dxinput.h"

//application title
#define APPTITLE "Slingshot Craze"

//screen setup
#define FULLSCREEN 0       //0 = windowed, 1 = fullscreen
#define SCREEN_WIDTH 800
#define SCREEN_HEIGHT 600

//macros to read the keyboard asynchronously
//#define KEY_DOWN(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 1 : 0)
//#define KEY_UP(vk_code)((GetAsyncKeyState(vk_code) & 0x8000) ? 1 : 0)

//function prototypes
int Game_Init(HWND);
void Game_Run(HWND);
void Game_End(HWND);
/*void LevelSelect();
void LoadScreen(int level);
void Instructions();
void Level(int level);
void Finish();
void GameOver();
void CheckWin();
void Reset();*/

//sprite structure
typedef struct {
    int x, movex;
	int y, movey;
    int width,height;
    int curframe,lastframe;
    int animdelay,animcount;
    int scalex, scaley;
    int rotation, rotaterate;

Is This A Good Question/Topic? 0
  • +

Replies To: Game Programming Projectile shoot (angry birds like)

#2 Swillis57  Icon User is offline

  • New D.I.C Head

Reputation: 8
  • View blog
  • Posts: 27
  • Joined: 14-January 11

Re: Game Programming Projectile shoot (angry birds like)

Posted 26 June 2014 - 10:10 PM

You're going to need some projectile motion math to get this to work. Relevant wiki -> http://en.wikipedia....ojectile_Motion

Here's the main ideas:

Position Equation (Y direction) ->
Y = initial_v_y*time + (1/2)*time*acceleration^2

Position Equation (X direction) ->
X = initial_v_x*time
(you aren't actually going to need this, just for consistency's sake).

initial_v_x is the x-component of the ball's initial velocity ->
initial_v_x = v*cos(theta)

initial_v_y is the y-component of the ball's initial velocity ->
initial_v_y = v*sin(theta)

theta is just the angle the slingshot is tilted, and you could just have the initial velocity be a percentage of how far back the sling is pulled
acceleration in the Y-equation is the force of gravity (whatever you want that to be). Note how there is no acceleration in the X-direction, I'm assuming that you don't want to bother with air resistance.

That's the general math out of the way, now for putting it into programming terms.
Since your x-directional velocity is always going to be constant, you can calculate it once (using the initial_v_x equation above). Now you have the ball's x-velocity in units per second. To actually draw this, you need it in units per frame, so you'd just divide that by your current FPS (or 60 as a standard, but using the current one smooths it out if you're way over that), and now you have the ball's units per frame movement value (deltaX). Every frame, you're going to add deltaX to the ball's current X coordinate, and there's your horizontal movement done. For the displacement in the Y-direction, it's even easier. You take the ball's current X-coordinate (not deltaX, the ball's actual position, assuming the slingshot is at x = 0) and plug it in as the time value in the Y-directional position equation. This gives you the exact y-coordinate of the ball at X distance from the slingshot.

That's pretty much basic projectile motion. As a side note: don't forget to add a check somewhere to make sure the ball is stopping when it either hits the ground (y = 0) or the bucket (you'll need collision for that), or it goes off the screen.

This post has been edited by Swillis57: 26 June 2014 - 10:11 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1