Jump to content


Photo

Length Of Line Segment? (anyone Know Math?)


  • Please log in to reply
9 replies to this topic

#1 GdaTerry

GdaTerry

    Ultimate-lifeform

  • New Member
  • 364 posts

Posted 19 December 2009 - 06:51 PM

object=collision_line(x,y,x+xvector*speed,y+yvector*speed,swswsw,0,1)

Works fine and dandy.
I'm able to get the object that the line crosses.
Now I want to get the LENGTH of the line segment from x,y to the point at which the line collided with object. How would I get this?

Edited by GdaTerry, 21 December 2009 - 01:57 PM.

  • 0

#2 i_dont_wear_pance

i_dont_wear_pance

    GMC Member

  • New Member
  • 36 posts

Posted 19 December 2009 - 07:09 PM

Pythagorean theorem.

x2 = x+xvector*speed
y2 = y+yvector*speed
d = sqrt( (x-x2)^2 + (y-y2)^2 )
  • 0

#3 GdaTerry

GdaTerry

    Ultimate-lifeform

  • New Member
  • 364 posts

Posted 19 December 2009 - 07:11 PM

Alright, but how would I get the point at which the line crosses my object?
  • 0

#4 acrog2

acrog2

    average user

  • GMC Member
  • 1118 posts

Posted 19 December 2009 - 07:37 PM

if you want to find the distance to the object, use this code.
if object != noone
dis = point_distance(object.x,object.y,x,y)

otherwise, you need to perform a binary search. Here is a script I made to do this:
//binary search for collision_line(x1,y1,x2,y2,obj,prec,notme)
//returns distance to collision, -1 if none
//by acrog2

var mx, mn, dir, tmx;
if !collision_line(argument0,argument1,argument2,argument3, argument4,argument5,argument6) return -1
mx = point_distance(argument0,argument1,argument2,argument3)
mn = 0
dir = point_direction(argument0,argument1,argument2,argument3)
while 1{
tmx = (mx-mn)/2+mn
if collision_line(argument0+lengthdir_x(mn,dir),argument1+ lengthdir_y(mn,dir),argument0+lengthdir_x(tmx,dir), argument1+lengthdir_y(tmx,dir), argument4,argument5,argument6)
mx = tmx
else
mn = tmx
if abs(mx-mn) < 1.1
return mx
}

-acrog2
  • 0

#5 i_dont_wear_pance

i_dont_wear_pance

    GMC Member

  • New Member
  • 36 posts

Posted 19 December 2009 - 07:57 PM

Wow sorry, I shouldn't have tried to answer when half-asleep. Yeah acrog's post puts mine to shame, listen to him instead.
  • 0

#6 GdaTerry

GdaTerry

    Ultimate-lifeform

  • New Member
  • 364 posts

Posted 20 December 2009 - 02:07 AM

Hm...even that doesn't seem to work. I guess I should try to explain my problem a little more. You see, I'm just trying to set up some collision detection for my bullets in 3d.

Ok, I solved this myself, see latest post

Edited by GdaTerry, 20 December 2009 - 06:18 PM.

  • 0

#7 Tepi

Tepi

    GMC Member

  • Global Moderators
  • 4201 posts
  • Version:GM8.1

Posted 20 December 2009 - 03:04 PM

There is no native collision system for z-axis so I'm assuming your 3D objects are supposed to be cylinders with the base being the shape of the sprite. Let your bullet be shot from the position (x0,y0,z0) and travel through air along the velocity (hspeed, vspeed, zspeed). The equation that defines all positions (x,y,z) on the line are:
x = x0 + hspeed*t;
y = y0 + vspeed*t;
z = z0 + zspeed*t;
where t is a positive real number. Let the collision position be (x1,y1,z1), where we know x1 and y1. We get the equations:
x1 = x0 + hspeed*t;
y1 = y0 + vspeed*t;
where we can solve for t from either one. The t value is used in determining z1: z1 = z0 + zspeed*(x1-x0)/hspeed. That's the z-coordinate you want.

Now you just need to define the "zspeed", the z-coordinate of your velocity (can also be found by checking the difference of z-coordinates).
  • 0

#8 GdaTerry

GdaTerry

    Ultimate-lifeform

  • New Member
  • 364 posts

Posted 20 December 2009 - 04:01 PM

Well, I modified my code and editted it into my previous post. But here's the edittable anyway

Posted Image

PRE

k=x // Don't even ask :)
//Sets it so that the bullet gets destroyed after a certain time.
if distance_to_object(object0)>1600
{
instance_destroy();
}


for (a=0;a<=speed;a+=1) //For loop cycles through the speed values to find the nearest object in bullet path piece by piece.
{
if collision_point(k+xv*a, y+yv*a, swswsw, 0,1) &&z+zv*a<=32 && z+zv*a>=0

{
i=collision_point(k+xv*a, y+yv*a, swswsw, 0,1)
d=a

global.zb=z+zv*a //Just a debug value.

with (i)
{
instance_destroy(); // Destroys the enemy.
}

m=instance_create(k+xv*d,y+yv*d,splash) //This is just like...a decal or whatever. It's to show where the bullet hit.

m.z=z+zv*d //Sets the z of the decal.

instance_destroy(); //Destroys the bullet.

break; // Ok, you found someone! Stop looking ;).
}
else
{
i=noone; // If I is noone, then just continue until the loop ends or i becomes....someone o.O
continue;
}

}




Well, it seems I can get the proper distance now, and thus the proper x,y and z values. I'm now however, presented with a new problem. Sometimes the bullet just seems to go through the object at random, and I am literally stumped now as to what the problem might be.

I apologize for being such an idiot if its simple, I'm just getting my feet wet with this.


Also, I think I may have done a for loop too many, but that isn't the problem here anyway. And I used collision point at the beginning rather than collision line, as when two objects are on the same line, it takes the ID of the last one instead (atleast, that's what it seemed like it was doing).

Edit: Cleaned up my code so that it is far more readable and removed some useless lines.

Edited by GdaTerry, 20 December 2009 - 06:36 PM.

  • 0

#9 Tepi

Tepi

    GMC Member

  • Global Moderators
  • 4201 posts
  • Version:GM8.1

Posted 21 December 2009 - 01:44 PM

I think I solved it.

You're using imprecise collision checking (for bullet collision checking), and the bounding box in the mask that's used for "swswsw" (sprite7), is set to manual, about one quarter of the whole sprite. When you set it back to automatic it will detect collisions just as it's supposed to.

Alternatively you can use the fourth argument in "collision_point(k+xv*a, y+yv*a, swswsw, 0,1)" as 1, which will set the collision checking to precise (do this to all the collision checkings in the script).
  • 0

#10 GdaTerry

GdaTerry

    Ultimate-lifeform

  • New Member
  • 364 posts

Posted 21 December 2009 - 01:50 PM

Wow, I can't believe I missed that. Thanks a bunch :P it seems to have fixed it. I can't even remember changing the origins >.>
  • 0




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users