Game development in React & ReactNative
- Jasbir Singh Shergill
![](https://s3.amazonaws.com/media-p.slid.es/uploads/639753/images/6468939/Profile1.jpeg)
Hey there!
My name is Jasbir
Freelancer, ex. Myntra, ex. GeekyAnts
Oh yea! 😎Â
Into Games & stuff
Some live demos
![](https://s3.amazonaws.com/media-p.slid.es/uploads/639753/images/6469135/pipers.gif)
Pipers - svg based modern snake game
RocketGame - Endless Runner
![](https://s3.amazonaws.com/media-p.slid.es/uploads/639753/images/6469148/rocketGame.gif)
Why use the web ?
All concepts of game development used here have counterparts in every platform.
Web makes your game cross-platform.
Easy to share / distribute game.
Easy to maintain / update.
Can build light-weight & high performance games.
Game Loop - the fundamental concept
![](https://media.giphy.com/media/DZD6OikL9NHxK/source.gif)
Simple game loop in React
runGameLoop() {
// update logic here
// collision detection here
this.gameLoop = requestAnimationFrame(this.runGameLoop);
}
// Start the loop
this.gameLoop = requestAnimationFrame(this.runGameLoop);
requestAnimationFrame vs setInterval
Collision detection
isColliding(a, b) {
return (
a.left <= b.right &&
b.left <= a.right &&
a.top <= b.bottom &&
b.top <= a.bottom
);
}
Bounding rectangle intersection check
inside(point, vs) {
var x = point[0], y = point[1];
var inside = false;
for (var i = 0, j = vs.length - 1; i < vs.length; j = i++) {
var xi = vs[i][0], yi = vs[i][1];
var xj = vs[j][0], yj = vs[j][1];
var intersect = ((yi > y) != (yj > y))
&& (x < (xj - xi) * (y - yi) / (yj - yi) + xi);
if (intersect) inside = !inside;
}
return inside;
}
// array of coordinates of each vertex of the polygon
var polygon = [ [ 1, 1 ], [ 1, 2 ], [ 2, 2 ], [ 2, 1 ] ];
inside([ 1.5, 1.5 ], polygon); // true
Ray casting algorithm to detect if a point lies inside a polygon
Using Lottie for modular animations
![](https://s3.amazonaws.com/media-p.slid.es/uploads/639753/images/6469419/rock.gif)
Using ThreeJS for 3D rendering
![](https://media.giphy.com/media/kn89xaebVxT20/source.gif)
Key points ThreeJS
Much more powerful as uses GPU
Wherever actual 3D graphics is required ThreeJS becomes necessary :- changing perspectives, camera positions, change in object view angles
You need sooo... much math !
External 3D assets can get large and must be optimised
BookCricket with Myntra
![](https://s3.amazonaws.com/media-p.slid.es/uploads/639753/images/6469154/bookCrick.gif)
The Hack! Making 2D lottie look like 3D
Be sure to use HTML renderer for 3D Lotties to preserve perspective transforms
Tips & Tricks for optimal performance
Single game loop, all update logic must go here itself
Keep re-renders as few as possible
Wherever possible, use CSS for animations
All computations inside game loop should be highly optimised. Keeping low time complexity should be the goal
Checkout my blog
Thank You!
![](https://media.giphy.com/media/xT9IgG50Fb7Mi0prBC/giphy.gif)
Game develop
By jasbir singh
Game develop
- 466