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.

Code Snippets / Continued Coll test tutorial

Author
Message
trager
21
Years of Service
User Offline
Joined: 5th Feb 2003
Location: United Kingdom
Posted: 10th Feb 2003 13:59
As promised here is the next part of my collision test code
This collision test is particularilly usefull for checking for collisions between objects travelling at great speed ie greater than their own radius.


This version of my previous snippet is more precise than the previous version as it tests from the back of the balls starting position to the front of the balls resting position rather than the middle of each position.

My next tutorial will show how to detect the exact position of the ball at the precise moment the ball hits the wall.

Rem * Title : simple line collision
Rem * Author : trager
Rem * Date : 9-feb-2003
set text opaque
rem draw a wall to test collision against
wall_start_x# = 100
wall_start_y# = 100
wall_end_y# = 350
wall_end_x# = 350
line wall_start_x#, wall_start_y#, wall_end_x#, wall_end_y#


rem stage 0 asks for start position
rem stage 1 ask for end position
rem stage 2 calculates the point of collision
text 0,0,"Click the start position of the balls path"
stage = 0
ink rgb(255,0,0), rgb(0,0,0)

do
rem the user defines the start and end postion of balls path using_
rem mouse clicks in the desired location

if stage = 0 and mouseclick() =1
rem get the ball start position when user clicks the mouse for the first time
ball_start_x# = mousex()
ball_start_y# = mousey()
dot ball_start_x#, ball_start_y#
stage = 1
text 0,0," "
text 0,0,"Click the end position of the balls path"
wait 500
else
if stage = 1 and mouseclick() = 1
rem get the ball end position when the user clicks the mouse for the secound time
ball_end_x# = mousex()
ball_end_y# = mousey()
dot ball_end_x#, ball_end_y#
line ball_start_x#, ball_start_y#, ball_end_x#, ball_end_y#
circle ball_start_x#, ball_start_y#, 8
circle ball_end_x#, ball_end_y#, 8

rem get the speed of the ball along the x and y axis
ball_xv# = ball_end_x# - ball_start_x#
ball_yv# = ball_end_y# - ball_start_y#
remstart
this part of the code increase the length of the line by adding 8 to the end of each line
this ensures that the collision is tested to the edge of the ball
rather than just to the centre of the balls resting position

remend
if ball_xv# 0 and ball_yv# 0
rem we can predict the values of yR# and xR# acurately if the ball fails to
rem move along one of the axis.
R# = sqrt(ball_xv#*ball_xv# + ball_yv#*ball_yv#)
yR# = ball_yv#/R#
xR# = ball_xv#/R#
else
yR# = 0
xR# = 1
if ball_xv# = 0 and ball_yv# 0
rem if the ball has vertically but not horizontally
yR# = 1
xR# = 0
endif
endif
rem once we calculated how much to add on to each end of the line we do so
ball_end_y# = ball_end_y# + yR# * 8.0
ball_end_x# = ball_end_x# + xR# * 8.0
ball_start_y# = ball_start_y# - yR# * 8.0
ball_start_x# = ball_start_x# - xR# * 8.0
rem and then add a nice green line to show the line we will be testing
rem notice the green line goes from the back of the start ball to
rem the front of the finish ball
ink rgb(0,255,0),rgb(0,0,0)
line ball_start_x#, ball_start_y#, ball_end_x#, ball_end_y#

rem get the new speed of the ball along the x and y axis
ball_xv# = ball_end_x# - ball_start_x#
ball_yv# = ball_end_y# - ball_start_y#

stage = 2
text 0,0," "
text 0,0,"Click left mouse button to get location of collision"
wait 500
else
if stage = 2 and mouseclick() = 1
rem on the third mouse click the program calculates the position of a collision
gosub show_collision

wait 500



endif
endif
endif

sync
loop

show_collision:
remstart
this part of the code finds the collision point between the wall
and the balls path and then draws a circle around it
remend



rem get the x and y difference from start to end of wall
rem the x length and y length if you like
wall_xv# = wall_end_x# - wall_start_x#
wall_yv# = wall_end_y# - wall_start_y#

rem now understanding how this next part works is not important
rem just know that this is the collision test basically it calcs s and t
rem if s and t are between 0 and 1 then there has been a collision
s# = ((0-ball_yv#) * (ball_start_x#-wall_start_x#) + ball_xv#*(ball_start_y#-wall_start_y#)) / ((0-wall_xv#)*ball_yv# + ball_xv#*wall_yv#)
t# = (wall_xv# * (ball_start_y#-wall_start_y#) - wall_yv# * (ball_start_x#-wall_start_x#)) / ((0-wall_xv#)*ball_yv# + ball_xv#*wall_yv#)

if (s# >=0 and s# = 0 and t#
trager
21
Years of Service
User Offline
Joined: 5th Feb 2003
Location: United Kingdom
Posted: 10th Feb 2003 14:01
How come everytime I try to post code here it gets butchered?



trager
21
Years of Service
User Offline
Joined: 5th Feb 2003
Location: United Kingdom
Posted: 11th Feb 2003 14:57
IS anyone even looking at this orr finding it remotely usefull?

This version will reposition the ball to the last safe location ie the last position before it hits the wall regardless of the angle between the wall and ball.

This is handy so that your collision response happens at exactly the correct place rather than at the end of the frame.

Worth considering that I am self taught in mathematics leaving school with a U in maths, you may find that my equations are wrong but they are certainly acurate enough in this demo.

DB's sin cos and tan functions all give differnet answers to what I calculate suggesting somekind of lookup able beeing used I blame this foor the minor inconsistencies.

If you think my maths is wrong please let me know the function that has been added in this version is called "Calc_ball_pos" all of the maths are there.

enjoy.



trager
21
Years of Service
User Offline
Joined: 5th Feb 2003
Location: United Kingdom
Posted: 11th Feb 2003 15:08
A nice easy addition next time I think.

I will show which way the ball will travel and how far it goes, so the ball will have travelled the same distance regardless of any collisions.

Any other requests?

trager
21
Years of Service
User Offline
Joined: 5th Feb 2003
Location: United Kingdom
Posted: 12th Feb 2003 19:25
And here it is the code now works out the final resting position of the ball after a collision in the current frame ie calcs the rebound

And then it works out the required x and y velocities of the ball in response to the collision.

Next time I will show how to calculate which wall the ball hit first in a multi wall environment.

As well as fix a tiny little bug but you dont have to worry about that!

enjoy.

trager
21
Years of Service
User Offline
Joined: 5th Feb 2003
Location: United Kingdom
Posted: 12th Feb 2003 19:27
oh and the new part of the code is labelled "calc_return"
trager
21
Years of Service
User Offline
Joined: 5th Feb 2003
Location: United Kingdom
Posted: 13th Feb 2003 15:25
Right then, first step for including a multi wall environment is to group all of the wall data together in a nice little array.

This array is called wall and it is declared and filled with all the values required of it at the begining of the program.

Also fixed a little bug that made the test line too short in some instances.

Next installment will have 2 walls, and the code will test for a collision with both.

enjoy

DaZCWA
21
Years of Service
User Offline
Joined: 9th Feb 2003
Location:
Posted: 13th Feb 2003 16:06
Nice work dude, I know how it feels to be neglected in this forum..... I was ignored in my first ever post on the forum yer good work m8 ill have a closer look now.

DaZ

"There is only 2 ways off this island: The first is in little pieces and the seconds with me" - (Hunter/DaZ)
trager
21
Years of Service
User Offline
Joined: 5th Feb 2003
Location: United Kingdom
Posted: 13th Feb 2003 16:12
OK, this version features two walls crossed over to give 4 wall sections to make experimenting with different colision combinations easier this setup has already helped me find one bug.

The collision detection works with both walls but at the moment the program has no idea which wall is hit first so wall get priority based on their appearance in the array.

2 ways around this but I will go for the most complete ie calculated the which collision point is the closest to the balls starting position.

Its actually fairly simple so also in my next installment I will be giving proven methods for getting Kylie Minogue to propose to you!

enjoy

trager
21
Years of Service
User Offline
Joined: 5th Feb 2003
Location: United Kingdom
Posted: 13th Feb 2003 16:14
Thanks CDsoftware I guess its the risk of posting theory rather than Quake III (only with better gfx)
trager
21
Years of Service
User Offline
Joined: 5th Feb 2003
Location: United Kingdom
Posted: 13th Feb 2003 19:35
OK first a recap of what this code does.

You state a balls positions and the beginning and end of a frame the program should detect, regardless of speed, which wall the ball hits first.

It then works out the final position of the ball considering the speed of the ball point of colision and return x ans y velocities.

and here it is



The next version will check to see if the balls ricochets directly onto another wall directly after a collision to stop the ball from jumping through walls after the initial collision test.

enjoy
trager
21
Years of Service
User Offline
Joined: 5th Feb 2003
Location: United Kingdom
Posted: 21st Feb 2003 10:32
Due to lack of interest I wont be continueing this project, sorry guys.
Obear
22
Years of Service
User Offline
Joined: 13th Oct 2002
Location:
Posted: 15th Apr 2003 18:05
Bump for a good post :0

trager
21
Years of Service
User Offline
Joined: 5th Feb 2003
Location: United Kingdom
Posted: 15th Apr 2003 18:23
Thanks

There is no answer only more questions.
SonicBoom
21
Years of Service
User Offline
Joined: 26th Nov 2002
Location:
Posted: 15th Apr 2003 18:54
I'm @ work so can't really go through this tutorial now but ace work dude!

Of course I'm only seeing this post as it's had a reply so has gone to the top of the list - try not to get disheartened - I guess alot of people still prefer the RGT forum, as it has got better functionality than this one although not as good looking.

Still I (for one??) would be very interested to hear of your thoughts about ricochets through a second wall as this is one of the things that plights the DarkCollisions dll I posted some time ago in partnership with Matthew DeWalt from NuclearGlory.com - again something that enjoys far more feedback from RGT forum than from here.

I'd recommend posting at both forums but please do continue with your tutorial in some form.
pugmartin
21
Years of Service
User Offline
Joined: 3rd Jan 2003
Location: United Kingdom
Posted: 15th Apr 2003 21:03
Yep, i agree.

I believe everyone should know at least a little of this kind of stuff becuase i reckon just about EVERY game benefits from physics n wotnot. Good Work Fella!!

That picture actually is me you know...
Can anyone post me a banana?
trager
21
Years of Service
User Offline
Joined: 5th Feb 2003
Location: United Kingdom
Posted: 16th Apr 2003 00:11
Whoa
I get more response when I don't make post mad eh?

I used to post in RGT, maybe I sould again, this place is a little slow.

Im working on a big project at the moment (MoO2 clone) so I have no plans to continue this development in the immediate future but thanks for the support guys, hope it bares under closer scrutiny.

There is no answer only more questions.

Login to post a reply

Server time is: 2024-11-24 04:58:56
Your offset time is: 2024-11-24 04:58:56