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 / 2d physics, determining rotation based on collision

Author
Message
Madscientist
16
Years of Service
User Offline
Joined: 23rd Aug 2009
Location: Between a rock and a hard place
Posted: 18th Aug 2010 04:21
I'm currently in the process of creating 2d physics in and for darkbasic. I'ts been going great until I have the problem stated in the title. If A box is tilted at an angle and collides with a surface I need to determine:
A. where to pivot my rotation from
B. Which direction the object will rotate.

Therefor I need to calculate which side of the object has more inertia and rotate the object based on that.
I need to first find where on my sprite the collision occurred and move on from there. Any suggestions?

My computer can render anything no matter the complexity of it in real time.
The memory is enormous. My computer is unique to everyone else’s. What computer do I have?

Hawkblood
16
Years of Service
User Offline
Joined: 5th Dec 2009
Location:
Posted: 18th Aug 2010 16:18
Your box is moving at a vector. The corners are moving at the same vector unless it's rotating already. When a corner comes in contact with a surface, it "stops" which will cause a new rotation to occur.... Therein lies the problem. How do you determine what part of the object (sprite?) is colliding? I am very interested in this subject, so keep us posted.

The fastest code is the code never written.
Madscientist
16
Years of Service
User Offline
Joined: 23rd Aug 2009
Location: Between a rock and a hard place
Posted: 18th Aug 2010 17:28 Edited at: 18th Aug 2010 18:17
Exactly! Right now I'm working on a function will that check lines along the bounds of my object (a box to keep it simple) to see where a collision occurs so that the point on the line will be translated into the collision point.
I'm having a strange problems with a simple line formula. The line is drawn in strange increments and parts of it are not drawn when the line is too steep or too flat.
Note: (X11,Y11) and (X12,Y12) are two corners' coordinates.




My computer surpasses all the technologies of the day. What computer do I have?

Hawkblood
16
Years of Service
User Offline
Joined: 5th Dec 2009
Location:
Posted: 18th Aug 2010 18:49
I wouldn't worry about the dots not being a solid line. But, the reason your line is not solid is because your incrementation on the X-axis is either a 1 or -1. When the slope is high, from x=0 to x=1, y goes from y=0 to y=10 (for instance). This is actually good for calculations because you don't want a lot of calculations each frame. If you are going to test each pixil location for collision, you could use what you have to start with and change the density once you know a rough collision.

The fastest code is the code never written.
Sven B
21
Years of Service
User Offline
Joined: 5th Jan 2005
Location: Belgium
Posted: 18th Aug 2010 19:47
It can be proven that the movement and rotation of an object is best calculated in the center of mass (most equations become easier to use). The differential equations for these are (in 2D):

x/dt² = F / m (second law of Newton)
d(I * w)/dt = M
usually I = constant, so: dw/dt = M / I

m = mass
F = sum of external forces (eg. gravity or)
x = the position
I = moment of inertia (for a box, this is m*(l^2+b^2)/12))
w = angular velocity
M = sum of moments (= forces * distance to center of mass - gravity always goes through the center of mass and so its moment = 0!)

These equations can be used to approximate the movement and rotation of an object.
eg.
dw/dt = M / I would be in code something like:
w = w + M/I*dt
where dt = elapsed time

First of all, these equations can only be used when the reaction forces are known (in your case, it should be possible to calculate it). If the object can collide with other objects, try searching for conservation of moments.

With these functions, you should be able to simulate the object falling to the ground in the right direction. All you need is the point of contact of the box with the ground. The reaction force can be calculated depending on what type the connection is of the box with the ground.
Collision is another matter, it involves conservation of moments. You can search for it on the internet. As for me, I don't really remember that much about it from my physics courses, but I have my book lying here somewhere and I'm sure it covered collision of objects...

Cheers!
Sven B

Madscientist
16
Years of Service
User Offline
Joined: 23rd Aug 2009
Location: Between a rock and a hard place
Posted: 19th Aug 2010 18:53 Edited at: 19th Aug 2010 18:54
Thanks for the input Sven B. I'll be sure to add those equations in once I have collision up and running.

I got a collision function working but how should I return the coordinates of where the collision occurred? Currently I'm using strings but is there a better way?
Here's my function.


My computer surpasses all the technologies of the day. What computer do I have?

Madscientist
16
Years of Service
User Offline
Joined: 23rd Aug 2009
Location: Between a rock and a hard place
Posted: 23rd Aug 2010 19:44
Solved the previous issue above and got collision to register properly but when I reposition the sprite based on where the collision hit the sprite flies to some random spot on the screen.
XCol and YCol are the exact coordinates where the collision occurred. This code basically adjusts the sprite's position because if you offset a sprite, the sprite moves based on the offset location.
This


My computer surpasses all the technologies of the day. What computer do I have?

Login to post a reply

Server time is: 2026-07-23 12:10:18
Your offset time is: 2026-07-23 12:10:18