Creating 2D Car Racing Game in Android: 5. Adding Interaction to the Car

This part is very interesting and important, because of after adding all the stuff in the screen it is very exited thing is moving all stuff with the user interaction.
In the presiding all parts we learn how to create main menu screen, creating Open GL surface and displaying graphics (car, road… etc). Now in this part we will learn how to interact with Player Car. i.e. Moving Car forward directions by pressing accelerator, moving left and right using Phone Accelerometer Sensor.
So let’s see…..!

Topics Covered

Getting Touch Input using onTouchEvent method

To move the on screen graphics we need user interaction like touching screen. To identify user touch input we need to add TouchEventListener to our game activity. Luckily we don’t need to do this because this feature is already available for Activity class. In that case we need to just Override and implement the onTouchEvent() method in our GameActivity Class.
So open and override following method with getting x and y coordinates values of Touch point on Screen.

Defining touch input area over Controller Graphics

One of the most important thing is how will you recognize particular graphics is touched on OpenGL GLSurfaceView. Yes, By area bounding box which is acquired by particular graphics. If user will touch inside the bounding box or graphics acquired area that means user has touched that particular graphics.
See the image below.

Creating 2D Car Racing Game in Android OpenGL

Now we are going to define red bounding box areas (shown in above image) in logical form. But before that we have to define some constants for User Touch states like Pressed, Released etc.
Open the and define constants for user interaction states.

And define a variable for current state of user interaction.

To define the Accelerator and Brakes Bounding box add the below code snippet in GameActivity under the onTouchEvent() function. And set the all interaction status as per area touched.

Moving Car to the Forward direction

You might be thinking if we move the car forward (move up in y direction) then car will go off after cross the screen top. Well..! Practically we are not going to move the car. Instead of that we will scroll down the road on screen and this will created the illusion of moving a car forward direction on road.
So let’s come to the GameRenderer class and define some variables to set road texture’s Y position offset and car speed.

Now replace gl.glTranslatef() method’s second parameter the road texture Y position value with defined variable under DrawRoad() function.

Now create a function named ScrollRoad() to scroll the road in Y axis toward down direction and define the user interaction status inside the function.

Now implement the logic one by one for each state. Make sure the increment and decrement logic must be covered ease-in, ease out math. So your car will smoothly start moving and smoothly stop moving.

Call the function inside onDrawFrame() method. To see the result, run the app and try pressing Brake and Accelerator.
Creating 2D Car Racing Game in Android OpenGL

Using Accelerometer Sensor feature

Now time to move car Left and Right, But again how to input to move the car Left and Right. Yes…! Its Accelerometer Sensor of the Phone. If player will tilt the phone towards Left-Right, the car will move Left-Right.
So define the variable for X acceleration of the phone under

Now Implement the GameActivity by SensoreEventListener.

Then declare SensorManager variable. Register listener to listen phone acceleration.

Now set the X acceleration value to the Global.SENSORE_ACCELEROMETER_X.

Moving Car Left and Right

Open the and define some constants to set the car’s left and right position limit.

Now define the MoveCar() function and implement the logic to move car left and right based on Accelerometer sensor values.
Call MoveCar() function into onDrawFrame() function. Run the app and try tilting phone left-right with the accelerator pressed. And see the result. Creating 2D Car Racing Game in Android OpenGL

In the next and last part we will learn how to add background music in the game.

Download Part 5 Source

1 reply

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *