Git Product home page Git Product logo

raycastings's Introduction

raycastings

  • Python3 (+pygame as 2D graphics lib)
  • C
  • JavaScript

ss

addtional feature: I implemented a custom collison system with only using math & program logic.

How my custom collison system works?

here's the specific code:

#storing the previous values of the player's x & y coordinates
prev_x = player_x
prev_y = player_y

#movment code here

#the collison system
for ang in range(0,360,STEP):
  a = radians(ang)
  row = int((player_y-(RADIUS+0.5)*sin(a))/LENGTH)
  col = int((player_x-(RADIUS+0.5)*cos(a))/LENGTH)
  if MAP[row][col] == 1:
    player_x = prev_x
    player_y = prev_y

this is the whole collsion system code.
in actual code at line number:
81-82 & 98-102

What Exactly Is It Doing?

It is essentially tracking specific points along the circumference. When these points overlap with any coordinates on the "wall", the player's X and Y coordinates are reverted to their previous positions.

Defining Points

To define the points, I implemented a for-loop that iterates from 0 to 360, covering every possible angle in a circle, with a specified "step". In this code and example, the "step" is set to 45 degrees. Therefore, the angles it will iterate through are 0, 45, 90, 135, 180, 225, 270, and 315 degrees.

Assuming the player's (X, Y) represents the center point of the circle, you can calculate the coordinates of the points along the circumference as follows:
XPoint = X - (radius + variance) * cos(angle-in-radians)
YPoint = Y - (radius + variance) * sin(angle-in-radians)

The diagram below illustrates all the points on a circle, starting from 0 degrees and incrementing by 45 degrees.

diagram

that's how points can be defined and then check if they overlaped.

Pros:

  • The lesser the step value the more delicate it gets as it will track more points. Even though currently step is set to 45 degrees it still does very well job at detecting collsion.
  • Pretty Errorless.

Cons:

  • Can be CPU intensive if tracking more and more points.

Great thanks to monkey_see_monkey_do I understood his video on raycasting only a bit, but his final code that I referred was pretty good and also made me understood a lot.

Some videos that did good job explaining raycasting are as follows:

(To be honest I didn't really watched/read much of these for making this project. I learned about it through the proccess of making it. Which can get painfull sometimes but thinking from scratch is like inventing it.)
(I might make a video on it, explaining it in my own way)

raycastings's People

Contributors

vilgacx avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.