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()
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)