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 / Using "intersect object" to determine if player is colliding with the front of an object?

Author
Message
Opposing force
20
Years of Service
User Offline
Joined: 10th Aug 2005
Location: England
Posted: 25th May 2012 00:58
Hi, I can't figure this out myself so I was hoping someone could help me. I'm sure there's a simple solution and I'm just an idiot for not knowing. I'm trying to make it so a certain action takes place if the player object collides with the front of another object. I figured it could be accomplished with something like this:

I basically want the non-player object to cast a line from its current position and angle (forwards) so it knows if the player collided with its front and not the back or sides. Is this possible or can it be done with another command instead? Thanks
Pincho Paxton
23
Years of Service
User Offline
Joined: 8th Dec 2002
Location:
Posted: 25th May 2012 01:56 Edited at: 25th May 2012 02:00
You could split the object up into separate limbs, sort of like orange fruit sections. I'm sure that there is a maths method based on where the object is pointing. Some sort of pi, but I'm no good at pi.

Opposing force
20
Years of Service
User Offline
Joined: 10th Aug 2005
Location: England
Posted: 25th May 2012 02:01
Yeah, I tried to do that initially but it caused problems with collision to I abandoned it.
chafari
Valued Member
20
Years of Service
User Offline
Joined: 2nd May 2006
Location: Canary Islands
Posted: 25th May 2012 12:46
Hi there... When there was not Sparky's dll, we could do that with several intersect to test where the object was colliding ....the more you use slower will be so be careful in old machines.

Cheers.

I'm not a grumpy grandpa
Opposing force
20
Years of Service
User Offline
Joined: 10th Aug 2005
Location: England
Posted: 25th May 2012 18:39
I use Sparky's dll a lot, it's great but I'm trying to make this new project without using any plug-ins. I'm trying to simulate traffic around a city and so far I've got a basic map with spawning points. Vehicles spawn when the player gets close (but not too close) to one of the points. Car is cloned from a master mesh then positioned and rotated to match the position and angle of the invisible spawning box. They then move around the roads just fine. However, I've programmed it so they stop if they collide with the player or another object. The problem is, if another car comes up behind the first car and stops, the car in front will be responding to collision from behind as well. This means even if the player moves out of the way from the first car, it's still not moving because of the collision from the back. Cars will then continue to spawn and start to queue up.

I have to figure out how to make it so each car only responds to collision at its front. I would have thought this could be accomplished if each car model casts a ray from its front. At the moment there are no car physics, just simple movement with the "move object" command. Ideally, I'd like it so cars stop a short distance from objects in front of it like in real life. But for now I just need to get it so the vehicles know their front from their back.
chafari
Valued Member
20
Years of Service
User Offline
Joined: 2nd May 2006
Location: Canary Islands
Posted: 25th May 2012 19:37
What about making way points from corner to corner to force cars to stay in the road ? ...Then every car has a number, and just the one that has bigger number will go while the others if are to close will have to wait...there a several ways to achive that

Cheers.

I'm not a grumpy grandpa
Pincho Paxton
23
Years of Service
User Offline
Joined: 8th Dec 2002
Location:
Posted: 25th May 2012 19:45 Edited at: 25th May 2012 19:48
Well hidden boxes around the cars would fix that. Test them by changing their colours whilst visible, then hide them. The distance would be the clue. Distance front to back, distance front to front.

Opposing force
20
Years of Service
User Offline
Joined: 10th Aug 2005
Location: England
Posted: 25th May 2012 19:57
The system I have created only spawns 10 cars on screen at any one time. They then get deleted when they reach a certain distance away from the player and are recreated near the player again. This makes it seem like there are a lot more than just 10 cars. It also allows a car to re-spawn with a different mesh so there's more variety and not just the same cars spawning over and over again. I have hidden boxes at junctions as well that rotates the car models when they get near so it sort of works like a way point.
Pincho Paxton
23
Years of Service
User Offline
Joined: 8th Dec 2002
Location:
Posted: 25th May 2012 21:30
I meant hidden boxes that travel with the cars in limb positions.

Scorpyo
23
Years of Service
User Offline
Joined: 26th Aug 2002
Location: italy
Posted: 25th May 2012 21:56
You could use the logic shown in the picture
It may not be very precise though
There are other ways, like raycasting across the car front at some distance, from one side to the other, and trigger action when the something gets in the way (intercept value going from equal to zero to > zero)
cheers
Scorpyo
Chris Tate
DBPro Master
17
Years of Service
User Offline
Joined: 29th Aug 2008
Location: London, England
Posted: 25th May 2012 23:05 Edited at: 25th May 2012 23:08
I wrote some code that you can use. In your case, you test if one car is infront of another. In my snippet (which I will put on the codebase), I am checking if a player is in front of a level object.

The code has comments

ObjectInFrontOf( iObject, iInFrontOfObject, fDistance# )



Chris Tate
DBPro Master
17
Years of Service
User Offline
Joined: 29th Aug 2008
Location: London, England
Posted: 25th May 2012 23:10
Quote: "trigger action when the something gets in the way (intercept value going from equal to zero to > zero)"


Intersect Object( ... ) => 0 is always true, even if there is no intersection. If the objects do not touch, the function will return zero; so you don't want to return true if the result equals zero.

Opposing force
20
Years of Service
User Offline
Joined: 10th Aug 2005
Location: England
Posted: 26th May 2012 01:25
Thank you guys for all your help, I really appreciate it and thanks Scorpyo and Chris Tate for those examples. I'm going to study them both and see what I can come up with. I'll be sure to post my progress in this thread. Thanks again.
Scorpyo
23
Years of Service
User Offline
Joined: 26th Aug 2002
Location: italy
Posted: 27th May 2012 23:28
Quote: "

Intersect Object( ... ) => 0 is always true, even if there is no intersection. If the objects do not touch, the function will return zero; so you don't want to return true if the result equals zero.
"


Intersect object always reports 0 if it doesn't intercept the target object (object number put in the formula) , otherwise it will report the distance of the object (i.e. > 0) from the raycast origin.
If you cast a ray across the vehicle,a bit ahead of the front,( i.e. from left to right exactly as the car width), it will report 0 until the target object comes in the way.
Therefore you can base your not hit / hit result on intercept object returning 0 or > 0 .
Chris Tate
DBPro Master
17
Years of Service
User Offline
Joined: 29th Aug 2008
Location: London, England
Posted: 28th May 2012 11:55
You are right, but 0 = 0; so the if branch will return true; so shouldn't 'equal to zero to > zero' be '> zero'?

Opposing force
20
Years of Service
User Offline
Joined: 10th Aug 2005
Location: England
Posted: 28th May 2012 13:05
I ran into a bit of a problem. I implemented the function and it works great when the player steps out in front of a vehicle and the vehicle stops. Problem is, I'm using a a FOR loop and array to control all vehicle models so I have something like this:

FOR v = 45 to 55
*All vehicle code in here*
Next v


So "V" represents all objects from 45 to 55 (which are vehicles) so if I try to use "if ObjectInFrontOf( v, v, 20 )=1" then each vehicle is detecting itself along with the others because obviously it falls within the range that "V" specifies. How would I make it so a specific object (within the V range) ignores itself and only responds to the other objects?
Pincho Paxton
23
Years of Service
User Offline
Joined: 8th Dec 2002
Location:
Posted: 28th May 2012 13:58 Edited at: 28th May 2012 14:42
Something like this?..

FOR v = 45 to 55
If V < 55
ObjectInFrontOf( v, v+1, 20.0 )=1
Endif
Next v


But it probably has to be a nested loop like this...

For v1 = 44 to 54
for v2 = v1+1 to 55

ObjectInFrontOf( v1, v2, 20.0 )=1

Next v2
Next v1



Which gives you this...

Set text opaque

For v1 = 44 to 54
for v2 = v1+1 to 55

Text 20,10,str$(V1)+" " + str$(V2)
wait 500
Next v2
Next v1


But I'm not 100% sure if I am checking all possibilities.

Scorpyo
23
Years of Service
User Offline
Joined: 26th Aug 2002
Location: italy
Posted: 28th May 2012 20:05
Chris Tate,
What I mean is this: see pic
Chris Tate
DBPro Master
17
Years of Service
User Offline
Joined: 29th Aug 2008
Location: London, England
Posted: 28th May 2012 22:16 Edited at: 28th May 2012 22:28
Yeah you are 100% right about the intersection, it was just this bit: "intercept value going from equal to zero to > zero" that is not quite right.

Since this code snippet returns true when no intersection takes place. Why, because; If 0 => 0 is true. Why? Because 0 = 0 is true.



I tend to write: If Intersect Object()

Without the > symbol, since anything other than zero is true.

Opposing force
20
Years of Service
User Offline
Joined: 10th Aug 2005
Location: England
Posted: 29th May 2012 00:55
Okay, I've solved the problem I had with a vehicle responding to itself. Thanks to Pincho Paxton for his examples which helped me come up with this.



Basically, X represents all of the objects from 1 to 100 and this includes the player object as well as the other vehicles. By having "if v<>x", the program ensures that the object the vehicle (v) is checking is not itself. In the array for vehicles there's the variable "veh(v).veh_moving" and if this is set to 0 then the vehicle is not moving and so "move object" is set to 0. (veh(v).veh_moving is to 1 as soon as the vehicle is spawned so therefore it starts out moving.

With the code as it stands, when the player steps out in front of a vehicle it will stop a short distance (20 units) from the player. Other vehicles behind it will then stop the same as it approaches the one in front of it. However, when the player moves out of the way the vehicles remain stationary and don't move off again. I tried adding this to the FOR loop:



However the vehicles then ignore and go straight through the player! I've tried everything and I just can't figure it out.
Pincho Paxton
23
Years of Service
User Offline
Joined: 8th Dec 2002
Location:
Posted: 29th May 2012 02:44 Edited at: 29th May 2012 02:47
I think that the Rem might fix it...



It should attempt to move the cars just before it stops them, but when the person moves out of the way the cars don't get stopped.

Opposing force
20
Years of Service
User Offline
Joined: 10th Aug 2005
Location: England
Posted: 29th May 2012 14:11
Just tried that but unfortunately the same thing happens as before and the cars go through the player without stopping.
Pincho Paxton
23
Years of Service
User Offline
Joined: 8th Dec 2002
Location:
Posted: 29th May 2012 15:32 Edited at: 29th May 2012 15:32
Quote: "Just tried that but unfortunately the same thing happens as before and the cars go through the player without stopping. "


They shouldn't.. did you take your other code out..

if ObjectInFrontOf( x, v, 20 )=0
veh(v).veh_moving=1
endif

Opposing force
20
Years of Service
User Offline
Joined: 10th Aug 2005
Location: England
Posted: 29th May 2012 15:41
Yeah, I did but the same thing happened.
Pincho Paxton
23
Years of Service
User Offline
Joined: 8th Dec 2002
Location:
Posted: 29th May 2012 15:46
Quote: "Yeah, I did but the same thing happened. "


I don't see how the code can get past this....

veh(v).veh_moving=1
if ObjectInFrontOf( x, v, 20 )
veh(v).veh_moving=0

... with ... veh(v).veh_moving=1

It just looks impossible. So you must have some other code that is running after this.

Opposing force
20
Years of Service
User Offline
Joined: 10th Aug 2005
Location: England
Posted: 29th May 2012 16:07 Edited at: 29th May 2012 16:08
I think it must be a problem with the for loop or something. Here's the whole code for controlling vehicles (spawning, moving then deleting)

Opposing force
20
Years of Service
User Offline
Joined: 10th Aug 2005
Location: England
Posted: 30th May 2012 18:39 Edited at: 6th Jun 2012 22:55
I think I nailed it. Since the vehicles went through the player there was no way of testing if they responded to other vehicles because they don't stop on their own. Despite the 1 to 100 range covering the player object (number 2) it seemed that it didn't want to work within the FOR loop so the solution was simple. Outside of the FOR loop I just put this in:


Now when the player steps out in front of a vehicle, it stops and the others behind do the same. If the player moves out of the way then the front vehicle starts to move again and the rest follow, just the way I want them to. Took a long time but we got there in the end. Once I have finished with this code I'm going to release it to the forum so others can use it. It should be useful for creating city games like Grand Theft Auto or Driver. Thanks again to everyone who helped me out.

EDIT
Ah, I spoke too soon. While testing it further, first vehicle stops behind player, second vehicle stops behind first vehicle but when third vehicle spawns, the second vehicle goes through first vehicle and then third vehicle goes through first vehicle when fourth vehicle spawns. I'm going to have to type all the code out manually now to cover every possibility...

Login to post a reply

Server time is: 2026-07-08 01:56:47
Your offset time is: 2026-07-08 01:56:47