'How to add collision detection to 2d objects in ursina?

Hi i was wondering how to add collision detection to 2d entitys in ursina. I am a beginner so this is very difficult for me. Here's my code attached:

from ursina import *

def update():
    if held_keys['a']:    
        player1.x -= 1 * time.dt
    if held_keys['d']:    
        player1.x += 1 * time.dt
    if held_keys['w']:    
        player1.y += 1 * time.dt
    if held_keys['s']:    
        player1.y -= 1 * time.dt
        
    if held_keys['j']:    
        player2.x -= 1 * time.dt
    if held_keys['l']:    
        player2.x += 1 * time.dt
    if held_keys['i']:    
        player2.y += 1 * time.dt
    if held_keys['k']:    
        player2.y -= 1 * time.dt



root = Ursina()

player_texture1 = load_texture('assets/player1.png')
player_texture2 = load_texture('assets/player2.png')

player1 = Entity(model = 'quad', scale = (1,1), texture = player_texture1)
player2 = Entity(model = 'quad', scale = (1,1), texture = player_texture2)



root.run()


Solution 1:[1]

Add this to your code: " collider='box' ", e.g. I have a code that goes:

class Floor(Entity):
    def __init__(self, position=(0, -103, 0)):
        super().__init__(
            parent=scene,
            position=position,
            model='cube',
            texture = 'white_cube',
            color = color.rgb(34,139,34),
            scale = 200,
            double_sided=True)

To add collisions to the entity you can just add it as any other parameter:

class Floor(Entity):
    def __init__(self, position=(0, -103, 0)):
        super().__init__(
            parent=scene,
            position=position,
            model='cube',
   ------>  collider='box',  <--------
            texture = 'white_cube',
            color = color.rgb(34,139,34),
            scale = 200,
            double_sided=True)

Solution 2:[2]

If I'm understanding correctly, what worked for me was using the method player1.intersects():

from ursina import *
from ursina.shaders import lit_with_shadows_shader

app = Ursina()

window.title = 'My Game'                # The window title
window.borderless = True               # Show a border
window.fullscreen = False               # Do not go Fullscreen
window.exit_button.visible = False      # Do not show the in-game red X that loses the window
window.fps_counter.enabled = False       # Show the FPS (Frames per second) counter

player1 = Entity(model='cube',color=color.red, scale_y=1,texture="crate", collider='box',shader=lit_with_shadows_shader)
player1.position = (0,0)

player2 = Entity(model='sphere',color=color.blue, scale_y=1,texture="crate", collider='box',shader=lit_with_shadows_shader)
player2.position = (0,2)

def update():   # update gets automatically called.



    player1.x += held_keys['d'] * .1
    player1.x -= held_keys['a'] * .1
    player1.y += held_keys['w'] * .1
    player1.y -= held_keys['s'] * .1
    player2.x += held_keys['l'] * .2
    player2.x -= held_keys['j'] * .2
    player2.y += held_keys['i'] * .2
    player2.y -= held_keys['k'] * .2

    player1.color=color.red
    if(player1.intersects(player2)):
        player1.color=color.yellow


app.run()   # opens a window and starts the game.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 dacops
Solution 2 Claudio Paladini