Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

DarkBASIC Professional Discussion / Sprite Collision Problem

Author
Message
Sprite Collision for Wall
11
Years of Service
User Offline
Joined: 14th Jan 2015
Location:
Posted: 14th Jan 2015 10:20
Hi everyone !, Sorry for my bad english. I need help for make 2D Sprite Collision like between player's object and wall's object. if I just use 1 object Collision so that is no probe, but when I use more than 1 object Collision wich is 2 Object like Wall vertical and Wall horizontal (Letter L) snap together, some times that the direction become wrong or the object likes does'nt Collide. In Other say's the player sometimes Through The Wall. May be this little code can give explanations for my project.

sometimes while I uses 2 direction like Rightkey and Downkey together so it can be a probe when hitting 2 object wall (Like Letter L).
Hope Someone can help me to fix this probe. Cause I wana make much object like much room and other. Thank's Before.
29 games
20
Years of Service
User Offline
Joined: 23rd Nov 2005
Location: not entirely sure
Posted: 14th Jan 2015 20:47 Edited at: 14th Jan 2015 20:47
Hi

The problem with your code is that using:

sc=sprite collision(pspr,0)

Will only allow you to resolve the collision for one wall during a single loop of the code. So if it detects collision with the top wall first then it won't detect the collision for the side wall at the same time. Because of this, the player can press against the top wall and force their way through the side wall.

I've modified your code slightly so the code detects and, more importantly, resolves the collision for each wall during each loop of the program.



Hope this helps.

Sprite Collision for Wall
11
Years of Service
User Offline
Joined: 14th Jan 2015
Location:
Posted: 14th Jan 2015 23:52
Wow .. thank you brother , I never thought of this before. This makes my mind becomes open again . I think if you use the " sprite collision ( pspsr , 0 ) " then all of object collision will be treated equally . One more thing that bothers me is if the player collision with objects from the top or bottom , then the player is still able to move left or right . But if the player collision with objects from the side ( left or right ) , the player will get stuck and can not move up or down . is there a way to fix this ?
29 games
20
Years of Service
User Offline
Joined: 23rd Nov 2005
Location: not entirely sure
Posted: 20th Jan 2015 21:26 Edited at: 20th Jan 2015 21:36
What the problem is, is the first bit of the collision resolution


if x1>sprite x(i)-sprite width(pspr) and x1<sprite x(i)+sprite width(i)
if y1>sprite y(i)-sprite height(pspr) and y1<sprite y(i)+sprite height(i)/2
y1=ey1
endif
if y1<sprite y(i)+sprite height(i) and y1>sprite y(i)+sprite height(i)/2
y1=ey1
endif
endif


Is always true no matter how the player collides with the wall. So y1 will always be set to ey1. So if the play hits the left or right side of the wall, y1 is set to ye1 which basically stops the player from moving up.

To over come this, you need to compare the player's previous position to the horizontal position and width of the wall:

if previous_x1>sprite x(i)-sprite width(pspr) and previous_x1<sprite x(i)+sprite width(i)

This way the if statement will be false when hitting the wall horizontally if the previous x position of the player mean's the player sprite is not over lapping the wall.

I've attached a sketch to show what I mean.

You might also want to change the second block of code "if y1>sprite y(i)-sprite height(pspr) and y1<sprite y(i)+sprite height(i)" in the same manner

Also, something rather interesting. Because of the maths you're using to reposition the player after the collision you don't actually need to use the "sprite collision" command at all. The maths is taking care of the collision detection for you.

Ps: any reason the code tags aren't working? I've tried editing this post about half a dozen times to no avail.

Sprite Collision for Wall
11
Years of Service
User Offline
Joined: 14th Jan 2015
Location:
Posted: 21st Jan 2015 09:04
whoa.... just like what i needed. Well i need the "sprite collision" because i wana make simple function for use to many object or sprite. BTW.. A lot of Thanks to U Mr.29 Games or what ever U Name it. If any suggest about Simple Game Logic/Programming that u can gave to me, I'll be glad to accept that.

Login to post a reply

Server time is: 2026-07-05 03:50:28
Your offset time is: 2026-07-05 03:50:28