rolling ball simulation matlab

Started by
7 comments, last by deeps 19 years, 5 months ago
hi, i'm making a mini golf game, and i need to write some code to simulate not only the starting and ending position of the ball, but i want to also show the ball rolling as a simulation. i'm sure some of you have seen the one at miniclip.com, i'm trying to make something similar to that. Although i'm not brilliant at matlab, i'd like to think i'm not toOOO bad :p basically this is what i want function [pathx, pathy, pathz] = ballsim(m, c, r, z, Px, Py, Vx, Vy, dt, Tmax) Arguments: m - mass of the ball. c - drag coefficient. r - radius of the ball. z - 2D array specifying the heights of a parametric surface. Px,Py - x and y coordinates specifying initial position of the ball Note this corresponds to the initial column and row position on the surface given by z. Vx,Vy - x and y components of the initial velocity of the ball. dt - the time increment size used in the simulation. Tmax - the total time to simulate the system for. Returns: pathx, pathy, pathz - arrays of the x, y and z ordinates of the ball position for each step in the simulation. i'm thinking of adding some sort of wind, but i dont know how difficult that would be to incorporate. I'm thinking that perhaps when i calculate the sum of forces, i just minus cv^2. i'll probably be using the gradient function to compute a discrete approximation of the gradients in x and y, from the 2d array z The main simulation code will be contained in a for loop that increments our simulated 'time' from 0, in steps of dt, until we get to Tmax, so basically somethign like t = 0:dt:Tmax; To obtain an estimate of the slopes in the x and y directions at the current position, i'll use zi = interp2(z, xp, yp); and then from there i can find the x and y force due to gravity. now i can find the acceleration using a=f/m however, this is where i get stuck. Can anyone help me get to the next few steps? I'm writing the ball simulation and the ball graphing functions as two seperate ones. I have already written up one that SHOULD graph the ball path, but i can't test it till i finish the ball position one. Cheers
Advertisement
Are you using MatLab to create your game, or just as a prototyping tool? In either case, you need to keep up with the balls velocity, and add 1) Gravity 2) WindSpeed 3) Friction to the ball. Code would be a big help.
so now you have acceleration, you can get velocity using an euler integration


v = v_old + a*dt
x = x_old + v_old*dt


MATLAB has ODE solvers RK2, RK4, etc... Better off using these, though I couldn't tell you off hand how to.
i'm using matlab, because that's the only programming language i know. I'm doing a course for Java atm, so i will try and move the program to java after, but first i want to do it on matlab.

how do i use ODE solvers? what are they exactly?

thanks
Ordinary Differential Equation solvers.
so does anyone have any ideas on how i can code this?
Is using matlab really necessary? I've recently finished a course on mathematical computation that used matlab, It doesnt seem like the kind of software you would want to create a mini golf game with.

Anyway, You would want to create various functions that gives you the forces on the ball, eg friction, wind, surface. The parameters for these should be time, position, velocity, mass and anything else you can think of.

Then you have a function that sums all of these and divides by the mass to get acceleration.

You can then integrate to get velocity, then integrate again to get the position.

I think that should be all you need.
as i said before, matlab is the only language i'm half decent at. Currently learning JAVA, but i'm nowhere near good enough to even start a program! not that it matters anyway, as i can't seem to write it for matlab either atm.

Can anyone do a quick write up of the code if possible? i have some equations etc. as i have mentioned above, but i'm struggling to put it together and move on from this point.

Cheers
anyone?!!

This topic is closed to new replies.

Advertisement